PageRenderTime 28ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/docx4j-export-fo/src/main/java/org/docx4j/convert/out/fo/FOExporterVisitorDelegate.java

http://github.com/plutext/docx4j
Java | 180 lines | 115 code | 23 blank | 42 comment | 9 complexity | 91f3b7dd689d031566039101b56720d3 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. Licensed to Plutext Pty Ltd under one or more contributor license agreements.
  3. * This file is part of docx4j.
  4. docx4j is licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package org.docx4j.convert.out.fo;
  15. import java.util.List;
  16. import org.docx4j.convert.out.FOSettings;
  17. import org.docx4j.convert.out.common.AbstractVisitorExporterDelegate;
  18. import org.docx4j.convert.out.common.ConversionSectionWrapper;
  19. import org.docx4j.convert.out.common.XsltCommonFunctions;
  20. import org.docx4j.model.fields.FormattingSwitchHelper;
  21. import org.docx4j.openpackaging.exceptions.Docx4JException;
  22. import org.docx4j.openpackaging.parts.Part;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Element;
  25. public class FOExporterVisitorDelegate extends AbstractVisitorExporterDelegate<FOSettings, FOConversionContext> {
  26. protected FOExporterVisitorDelegate() {
  27. super(FOExporterVisitorGenerator.GENERATOR_FACTORY);
  28. }
  29. private static String XSL_FO = "http://www.w3.org/1999/XSL/Format";
  30. @Override
  31. protected Element createDocumentRoot(FOConversionContext conversionContext, Document document) throws Docx4JException {
  32. return document.createElementNS(XSL_FO, "root");
  33. }
  34. @Override
  35. protected void appendDocumentHeader(FOConversionContext conversionContext,
  36. Document document, Element documentRoot) throws Docx4JException {
  37. LayoutMasterSetBuilder.appendLayoutMasterSetFragment( conversionContext, documentRoot);
  38. }
  39. @Override
  40. protected Element createSectionRoot(FOConversionContext conversionContext,
  41. Document document,
  42. ConversionSectionWrapper sectionWrapper, Element currentParent) throws Docx4JException {
  43. Element pageSequence = document.createElementNS(XSL_FO, "page-sequence");
  44. int pageNumberInitial = sectionWrapper.getPageNumberInformation().getPageStart();
  45. String pageFormat = sectionWrapper.getPageNumberInformation().getPageFormat();
  46. pageSequence.setAttribute("master-reference", sectionWrapper.getId());
  47. pageSequence.setAttribute("id", "section_" + sectionWrapper.getId());
  48. pageFormat = FormattingSwitchHelper.getFoPageNumberFormat(pageFormat);
  49. if (pageNumberInitial > -1) {
  50. pageSequence.setAttribute("initial-page-number", Integer.toString(pageNumberInitial));
  51. }
  52. if (pageFormat != null) {
  53. pageSequence.setAttribute("format", pageFormat);
  54. }
  55. return pageSequence;
  56. }
  57. @Override
  58. protected void appendSectionHeader(FOConversionContext conversionContext, Document document,
  59. ConversionSectionWrapper sectionWrapper,
  60. Element currentParent) throws Docx4JException {
  61. Element staticContent = null;
  62. // <!-- First Page Header -->
  63. // <fo:static-content flow-name="xsl-region-before-firstpage">
  64. if (XsltCommonFunctions.hasFirstHeader(conversionContext)) {
  65. appendPartContent(
  66. conversionContext, document,
  67. currentParent,
  68. "xsl-region-before-firstpage",
  69. sectionWrapper.getHeaderFooterPolicy().getFirstHeader(),
  70. sectionWrapper.getHeaderFooterPolicy().getFirstHeader().getJaxbElement().getContent());
  71. }
  72. // <!-- First Page Footer -->
  73. // <fo:static-content flow-name="xsl-region-after-firstpage">
  74. if (XsltCommonFunctions.hasFirstFooter(conversionContext)) {
  75. appendPartContent(
  76. conversionContext, document,
  77. currentParent,
  78. "xsl-region-after-firstpage",
  79. sectionWrapper.getHeaderFooterPolicy().getFirstFooter(),
  80. sectionWrapper.getHeaderFooterPolicy().getFirstFooter().getJaxbElement().getContent());
  81. }
  82. // <!-- Even Page Header -->
  83. // <fo:static-content flow-name="xsl-region-before-evenpage">
  84. if (XsltCommonFunctions.hasEvenHeader(conversionContext)) {
  85. appendPartContent(
  86. conversionContext, document,
  87. currentParent,
  88. "xsl-region-before-evenpage",
  89. sectionWrapper.getHeaderFooterPolicy().getEvenHeader(),
  90. sectionWrapper.getHeaderFooterPolicy().getEvenHeader().getJaxbElement().getContent());
  91. }
  92. // <!-- Even Page Footer -->
  93. // <fo:static-content flow-name="xsl-region-after-evenpage">
  94. if (XsltCommonFunctions.hasEvenFooter(conversionContext)) {
  95. appendPartContent(
  96. conversionContext, document,
  97. currentParent,
  98. "xsl-region-after-evenpage",
  99. sectionWrapper.getHeaderFooterPolicy().getEvenFooter(),
  100. sectionWrapper.getHeaderFooterPolicy().getEvenFooter().getJaxbElement().getContent());
  101. }
  102. // <!-- Default/Odd Page Header -->
  103. // <fo:static-content flow-name="xsl-region-before-firstpage">
  104. if (XsltCommonFunctions.hasDefaultHeader(conversionContext)) {
  105. appendPartContent(
  106. conversionContext, document,
  107. currentParent,
  108. "xsl-region-before-default",
  109. sectionWrapper.getHeaderFooterPolicy().getDefaultHeader(),
  110. sectionWrapper.getHeaderFooterPolicy().getDefaultHeader().getJaxbElement().getContent());
  111. }
  112. // <!-- Default/Odd Page Footer -->
  113. // <fo:static-content flow-name="xsl-region-after-firstpage">
  114. if (XsltCommonFunctions.hasDefaultFooter(conversionContext)) {
  115. appendPartContent(
  116. conversionContext, document,
  117. currentParent,
  118. "xsl-region-after-default",
  119. sectionWrapper.getHeaderFooterPolicy().getDefaultFooter(),
  120. sectionWrapper.getHeaderFooterPolicy().getDefaultFooter().getJaxbElement().getContent());
  121. }
  122. // TODO
  123. // <xsl:if
  124. // test="java:org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.hasEndnotesPart($wmlPackage)">
  125. //
  126. // <fo:block space-before="44pt" font-weight="bold" font-size="14pt">
  127. // <xsl:text>Endnotes</xsl:text>
  128. // </fo:block>
  129. //
  130. // <xsl:apply-templates
  131. // select="java:org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getEndnotes($wmlPackage)" />
  132. // </xsl:if>
  133. }
  134. protected void appendPartContent(FOConversionContext conversionContext,
  135. Document document,
  136. Element currentParent,
  137. String name, Part part, List<Object> content) throws Docx4JException {
  138. Element flow = document.createElementNS(XSL_FO, "static-content");
  139. currentParent.appendChild(flow);
  140. flow.setAttribute("flow-name", name);
  141. appendPartContent(conversionContext, document, part, content, flow);
  142. }
  143. @Override
  144. protected Element createSectionBody(FOConversionContext conversionContext, Document document,
  145. ConversionSectionWrapper sectionWrapper,
  146. Element currentParent) throws Docx4JException {
  147. Element ret = document.createElementNS(XSL_FO, "flow");
  148. ret.setAttribute("flow-name", "xsl-region-body");
  149. return ret;
  150. }
  151. }