You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSLFSlideMaster.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf.usermodel;
  16. import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.util.HashMap;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. import org.apache.poi.POIXMLDocumentPart;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.sl.usermodel.MasterSheet;
  24. import org.apache.poi.sl.usermodel.Placeholder;
  25. import org.apache.poi.util.Beta;
  26. import org.apache.xmlbeans.XmlException;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTColorMapping;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
  29. import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterTextStyles;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument;
  33. /**
  34. * Slide master object associated with this layout.
  35. * <p>
  36. * Within a slide master slide are contained all elements
  37. * that describe the objects and their corresponding formatting
  38. * for within a presentation slide.
  39. * <p>
  40. * Within a slide master slide are two main elements.
  41. * The cSld element specifies the common slide elements such as shapes and
  42. * their attached text bodies. Then the txStyles element specifies the
  43. * formatting for the text within each of these shapes. The other properties
  44. * within a slide master slide specify other properties for within a presentation slide
  45. * such as color information, headers and footers, as well as timing and
  46. * transition information for all corresponding presentation slides.
  47. */
  48. @Beta
  49. public class XSLFSlideMaster extends XSLFSheet
  50. implements MasterSheet<XSLFShape,XSLFTextParagraph> {
  51. private CTSlideMaster _slide;
  52. private Map<String, XSLFSlideLayout> _layouts;
  53. /**
  54. * @since POI 3.14-Beta1
  55. */
  56. protected XSLFSlideMaster(PackagePart part) throws IOException, XmlException {
  57. super(part);
  58. SldMasterDocument doc =
  59. SldMasterDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
  60. _slide = doc.getSldMaster();
  61. }
  62. @Override
  63. public CTSlideMaster getXmlObject() {
  64. return _slide;
  65. }
  66. @Override
  67. protected String getRootElementName(){
  68. return "sldMaster";
  69. }
  70. @Override
  71. public XSLFSlideMaster getMasterSheet() {
  72. return null;
  73. }
  74. private Map<String, XSLFSlideLayout> getLayouts(){
  75. if(_layouts == null){
  76. _layouts = new HashMap<>();
  77. for (POIXMLDocumentPart p : getRelations()) {
  78. if (p instanceof XSLFSlideLayout){
  79. XSLFSlideLayout layout = (XSLFSlideLayout)p;
  80. _layouts.put(layout.getName().toLowerCase(Locale.ROOT), layout);
  81. }
  82. }
  83. }
  84. return _layouts;
  85. }
  86. /**
  87. *
  88. * @return all slide layouts referencing this master
  89. */
  90. public XSLFSlideLayout[] getSlideLayouts() {
  91. return getLayouts().values().toArray(new XSLFSlideLayout[_layouts.size()]);
  92. }
  93. /**
  94. * Get the slide layout by type.
  95. *
  96. * @param type The layout type. Cannot be null.
  97. *
  98. * @return the layout found or null on failure
  99. */
  100. public XSLFSlideLayout getLayout(SlideLayout type){
  101. for(XSLFSlideLayout layout : getLayouts().values()){
  102. if(layout.getType() == type) {
  103. return layout;
  104. }
  105. }
  106. return null;
  107. }
  108. /**
  109. * Get the slide layout by name.
  110. *
  111. * @param name The layout name (case-insensitive). Cannot be null.
  112. *
  113. * @return the layout found or null on failure
  114. */
  115. public XSLFSlideLayout getLayout(String name) {
  116. return getLayouts().get(name.toLowerCase(Locale.ROOT));
  117. }
  118. @SuppressWarnings(value = "unused")
  119. protected CTTextListStyle getTextProperties(Placeholder textType) {
  120. CTTextListStyle props;
  121. CTSlideMasterTextStyles txStyles = getXmlObject().getTxStyles();
  122. switch (textType){
  123. case TITLE:
  124. case CENTERED_TITLE:
  125. case SUBTITLE:
  126. props = txStyles.getTitleStyle();
  127. break;
  128. case BODY:
  129. props = txStyles.getBodyStyle();
  130. break;
  131. default:
  132. props = txStyles.getOtherStyle();
  133. break;
  134. }
  135. return props;
  136. }
  137. /**
  138. * Render this sheet into the supplied graphics object
  139. *
  140. */
  141. @Override
  142. protected boolean canDraw(XSLFShape shape) {
  143. return !(shape instanceof XSLFSimpleShape) || !shape.isPlaceholder();
  144. }
  145. @Override
  146. public XSLFBackground getBackground() {
  147. CTBackground bg = _slide.getCSld().getBg();
  148. if(bg != null) {
  149. return new XSLFBackground(bg, this);
  150. } else {
  151. return null;
  152. }
  153. }
  154. @Override
  155. boolean isSupportTheme() {
  156. return true;
  157. }
  158. @Override
  159. CTColorMapping getColorMapping() {
  160. return _slide.getClrMap();
  161. }
  162. }