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.

XSLFPlaceholderDetails.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.ooxml.util.XPathHelper.selectProperty;
  17. import static org.apache.poi.xslf.usermodel.XSLFShape.PML_NS;
  18. import java.util.function.Consumer;
  19. import java.util.function.Function;
  20. import javax.xml.namespace.QName;
  21. import org.apache.poi.sl.usermodel.MasterSheet;
  22. import org.apache.poi.sl.usermodel.Placeholder;
  23. import org.apache.poi.sl.usermodel.PlaceholderDetails;
  24. import org.apache.xmlbeans.XmlException;
  25. import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
  26. import org.openxmlformats.schemas.presentationml.x2006.main.CTHeaderFooter;
  27. import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMaster;
  28. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  29. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderSize;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
  32. /**
  33. * XSLF Placeholder Details
  34. *
  35. * @since POI 4.0.0
  36. */
  37. public class XSLFPlaceholderDetails implements PlaceholderDetails {
  38. private final XSLFShape shape;
  39. private CTPlaceholder _ph;
  40. XSLFPlaceholderDetails(final XSLFShape shape) {
  41. this.shape = shape;
  42. }
  43. @Override
  44. public Placeholder getPlaceholder() {
  45. final CTPlaceholder ph = getCTPlaceholder(false);
  46. if (ph == null || !(ph.isSetType() || ph.isSetIdx())) {
  47. return null;
  48. }
  49. return Placeholder.lookupOoxml(ph.getType().intValue());
  50. }
  51. public XSLFSimpleShape getPlaceholderShape() {
  52. CTPlaceholder ph = getCTPlaceholder(false);
  53. if (ph == null) {
  54. return null;
  55. }
  56. XSLFSheet sheet = (XSLFSheet)shape.getSheet().getMasterSheet();
  57. return sheet.getPlaceholder(ph);
  58. }
  59. @Override
  60. public void setPlaceholder(final Placeholder placeholder) {
  61. CTPlaceholder ph = getCTPlaceholder(placeholder != null);
  62. if (ph != null) {
  63. if (placeholder != null) {
  64. ph.setType(STPlaceholderType.Enum.forInt(placeholder.ooxmlId));
  65. } else {
  66. CTApplicationNonVisualDrawingProps nvProps = getNvProps();
  67. if (nvProps != null) {
  68. nvProps.unsetPh();
  69. }
  70. }
  71. }
  72. }
  73. @Override
  74. public boolean isVisible() {
  75. final CTPlaceholder ph = getCTPlaceholder(false);
  76. if (ph == null || !ph.isSetType()) {
  77. return true;
  78. }
  79. final CTHeaderFooter hf = getHeaderFooter(false);
  80. if (hf == null) {
  81. return false;
  82. }
  83. final Placeholder pl = Placeholder.lookupOoxml(ph.getType().intValue());
  84. if (pl == null) {
  85. return true;
  86. }
  87. switch (pl) {
  88. case DATETIME:
  89. return !hf.isSetDt() || hf.getDt();
  90. case FOOTER:
  91. return !hf.isSetFtr() || hf.getFtr();
  92. case HEADER:
  93. return !hf.isSetHdr() || hf.getHdr();
  94. case SLIDE_NUMBER:
  95. return !hf.isSetSldNum() || hf.getSldNum();
  96. default:
  97. return true;
  98. }
  99. }
  100. @Override
  101. public void setVisible(final boolean isVisible) {
  102. final Placeholder ph = getPlaceholder();
  103. if (ph == null) {
  104. return;
  105. }
  106. final Function<CTHeaderFooter,Consumer<Boolean>> fun;
  107. switch (ph) {
  108. case DATETIME:
  109. fun = (hf) -> hf::setDt;
  110. break;
  111. case FOOTER:
  112. fun = (hf) -> hf::setFtr;
  113. break;
  114. case HEADER:
  115. fun = (hf) -> hf::setHdr;
  116. break;
  117. case SLIDE_NUMBER:
  118. fun = (hf) -> hf::setSldNum;
  119. break;
  120. default:
  121. return;
  122. }
  123. // only create a header, if we need to, i.e. the placeholder type is eligible
  124. final CTHeaderFooter hf = getHeaderFooter(true);
  125. if (hf == null) {
  126. return;
  127. }
  128. fun.apply(hf).accept(isVisible);
  129. }
  130. @Override
  131. public PlaceholderSize getSize() {
  132. final CTPlaceholder ph = getCTPlaceholder(false);
  133. if (ph == null || !ph.isSetSz()) {
  134. return null;
  135. }
  136. switch (ph.getSz().intValue()) {
  137. case STPlaceholderSize.INT_FULL:
  138. return PlaceholderSize.full;
  139. case STPlaceholderSize.INT_HALF:
  140. return PlaceholderSize.half;
  141. case STPlaceholderSize.INT_QUARTER:
  142. return PlaceholderSize.quarter;
  143. default:
  144. return null;
  145. }
  146. }
  147. @Override
  148. public void setSize(final PlaceholderSize size) {
  149. final CTPlaceholder ph = getCTPlaceholder(false);
  150. if (ph == null) {
  151. return;
  152. }
  153. if (size == null) {
  154. ph.unsetSz();
  155. return;
  156. }
  157. switch (size) {
  158. case full:
  159. ph.setSz(STPlaceholderSize.FULL);
  160. break;
  161. case half:
  162. ph.setSz(STPlaceholderSize.HALF);
  163. break;
  164. case quarter:
  165. ph.setSz(STPlaceholderSize.QUARTER);
  166. break;
  167. }
  168. }
  169. /**
  170. * Gets or creates a new placeholder element
  171. *
  172. * @param create if {@code true} creates the element if it hasn't existed before
  173. * @return the placeholder or {@code null} if the shape doesn't support placeholders
  174. */
  175. CTPlaceholder getCTPlaceholder(final boolean create) {
  176. if (_ph != null) {
  177. return _ph;
  178. }
  179. final CTApplicationNonVisualDrawingProps nv = getNvProps();
  180. if (nv == null) {
  181. // shape doesn't support CTApplicationNonVisualDrawingProps
  182. return null;
  183. }
  184. _ph = (nv.isSetPh() || !create) ? nv.getPh() : nv.addNewPh();
  185. return _ph;
  186. }
  187. private static final QName[] NV_CONTAINER = {
  188. new QName(PML_NS, "nvSpPr"),
  189. new QName(PML_NS, "nvCxnSpPr"),
  190. new QName(PML_NS, "nvGrpSpPr"),
  191. new QName(PML_NS, "nvPicPr"),
  192. new QName(PML_NS, "nvGraphicFramePr")
  193. };
  194. private static final QName[] NV_PROPS = {
  195. new QName(PML_NS, "nvPr")
  196. };
  197. private CTApplicationNonVisualDrawingProps getNvProps() {
  198. try {
  199. return selectProperty(shape.getXmlObject(), CTApplicationNonVisualDrawingProps.class, null, NV_CONTAINER, NV_PROPS);
  200. } catch (XmlException e) {
  201. return null;
  202. }
  203. }
  204. private CTHeaderFooter getHeaderFooter(final boolean create) {
  205. final XSLFSheet sheet = shape.getSheet();
  206. final XSLFSheet master = (sheet instanceof MasterSheet && !(sheet instanceof XSLFSlideLayout)) ? sheet : (XSLFSheet)sheet.getMasterSheet();
  207. if (master instanceof XSLFSlideMaster) {
  208. final CTSlideMaster ct = ((XSLFSlideMaster) master).getXmlObject();
  209. return (ct.isSetHf() || !create) ? ct.getHf() : ct.addNewHf();
  210. } else if (master instanceof XSLFNotesMaster) {
  211. final CTNotesMaster ct = ((XSLFNotesMaster) master).getXmlObject();
  212. return (ct.isSetHf() || !create) ? ct.getHf() : ct.addNewHf();
  213. } else {
  214. return null;
  215. }
  216. }
  217. @Override
  218. public String getText() {
  219. return null;
  220. }
  221. @Override
  222. public void setText(String text) {
  223. }
  224. }