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.

ParagraphPropertyFetcher.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.model;
  20. import static org.apache.poi.ooxml.util.XPathHelper.selectProperty;
  21. import java.util.function.Consumer;
  22. import javax.xml.namespace.QName;
  23. import javax.xml.stream.XMLStreamReader;
  24. import org.apache.poi.util.Internal;
  25. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  26. import org.apache.poi.xslf.usermodel.XSLFShape;
  27. import org.apache.poi.xslf.usermodel.XSLFSheet;
  28. import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
  29. import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
  30. import org.apache.xmlbeans.XmlException;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
  34. @Internal
  35. public final class ParagraphPropertyFetcher<T> extends PropertyFetcher<T> {
  36. public interface ParaPropFetcher<S> {
  37. void fetch (CTTextParagraphProperties props, Consumer<S> val);
  38. }
  39. static final String PML_NS = "http://schemas.openxmlformats.org/presentationml/2006/main";
  40. static final String DML_NS = "http://schemas.openxmlformats.org/drawingml/2006/main";
  41. private static final QName[] TX_BODY = { new QName(PML_NS, "txBody") };
  42. private static final QName[] LST_STYLE = { new QName(DML_NS, "lstStyle") };
  43. private final XSLFTextParagraph para;
  44. int _level;
  45. private final ParaPropFetcher<T> fetcher;
  46. public ParagraphPropertyFetcher(XSLFTextParagraph para, ParaPropFetcher<T> fetcher) {
  47. this.para = para;
  48. _level = para.getIndentLevel();
  49. this.fetcher = fetcher;
  50. }
  51. public boolean fetch(XSLFShape shape) {
  52. // this is only called when propagating to parent styles
  53. try {
  54. fetchProp(select(shape, _level));
  55. } catch (XmlException ignored) {
  56. }
  57. return isSet();
  58. }
  59. public T fetchProperty(XSLFShape shape) {
  60. final XSLFSheet sheet = shape.getSheet();
  61. if (!(sheet instanceof XSLFSlideMaster)) {
  62. fetchParagraphProp();
  63. fetchShapeProp(shape);
  64. fetchThemeProp(shape);
  65. }
  66. fetchMasterProp();
  67. return isSet() ? getValue() : null;
  68. }
  69. private void fetchParagraphProp() {
  70. fetchProp(para.getXmlObject().getPPr());
  71. }
  72. private void fetchShapeProp(XSLFShape shape) {
  73. if (!isSet()) {
  74. shape.fetchShapeProperty(this);
  75. }
  76. }
  77. private void fetchThemeProp(XSLFShape shape) {
  78. if (!isSet()) {
  79. fetchProp(getThemeProps(shape, _level));
  80. }
  81. }
  82. private void fetchMasterProp() {
  83. // defaults for placeholders are defined in the slide master
  84. // TODO: determine master shape
  85. if (!isSet()) {
  86. fetchProp(para.getDefaultMasterStyle());
  87. }
  88. }
  89. private void fetchProp(CTTextParagraphProperties props) {
  90. if (props != null) {
  91. fetcher.fetch(props, this::setValue);
  92. }
  93. }
  94. static CTTextParagraphProperties select(XSLFShape shape, int level) throws XmlException {
  95. QName[] lvlProp = { new QName(DML_NS, "lvl" + (level + 1) + "pPr") };
  96. return selectProperty(shape.getXmlObject(),
  97. CTTextParagraphProperties.class, ParagraphPropertyFetcher::parse, TX_BODY, LST_STYLE, lvlProp);
  98. }
  99. static CTTextParagraphProperties parse(XMLStreamReader reader) throws XmlException {
  100. CTTextParagraph para = CTTextParagraph.Factory.parse(reader);
  101. return (para != null && para.isSetPPr()) ? para.getPPr() : null;
  102. }
  103. static CTTextParagraphProperties getThemeProps(XSLFShape shape, int _level) {
  104. if (shape.isPlaceholder()) {
  105. return null;
  106. }
  107. // if it is a plain text box then take defaults from presentation.xml
  108. @SuppressWarnings("resource")
  109. final XMLSlideShow ppt = shape.getSheet().getSlideShow();
  110. CTTextListStyle dts = ppt.getCTPresentation().getDefaultTextStyle();
  111. if (dts == null) {
  112. return null;
  113. }
  114. switch (_level) {
  115. case 0:
  116. return dts.getLvl1PPr();
  117. case 1:
  118. return dts.getLvl2PPr();
  119. case 2:
  120. return dts.getLvl3PPr();
  121. case 3:
  122. return dts.getLvl4PPr();
  123. case 4:
  124. return dts.getLvl5PPr();
  125. case 5:
  126. return dts.getLvl6PPr();
  127. case 6:
  128. return dts.getLvl7PPr();
  129. case 7:
  130. return dts.getLvl8PPr();
  131. case 8:
  132. return dts.getLvl9PPr();
  133. default:
  134. return null;
  135. }
  136. }
  137. }