PageRenderTime 59ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparties-extension/org.apache.poi.xwpf.converter/src/main/java/org/apache/poi/xwpf/converter/internal/xhtml/XHTMLMapper.java

https://code.google.com/
Java | 310 lines | 217 code | 49 blank | 44 comment | 14 complexity | 63df21aba902c6147d079c8d6a55bebd MD5 | raw file
  1. /**
  2. * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com> and Pascal Leclercq <pascal.leclercq@gmail.com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. package org.apache.poi.xwpf.converter.internal.xhtml;
  26. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.getRStyle;
  27. import java.io.OutputStream;
  28. import java.util.List;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import org.apache.poi.xwpf.converter.IURIResolver;
  32. import org.apache.poi.xwpf.converter.internal.XWPFElementVisitor;
  33. import org.apache.poi.xwpf.converter.internal.itext.StyleEngineForIText;
  34. import org.apache.poi.xwpf.converter.internal.itext.XWPFPictureUtil;
  35. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  36. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  37. import org.apache.poi.xwpf.usermodel.XWPFPicture;
  38. import org.apache.poi.xwpf.usermodel.XWPFPictureData;
  39. import org.apache.poi.xwpf.usermodel.XWPFRun;
  40. import org.apache.poi.xwpf.usermodel.XWPFStyle;
  41. import org.apache.poi.xwpf.usermodel.XWPFTable;
  42. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  43. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  44. import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
  45. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
  46. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
  47. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
  48. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
  49. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
  50. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
  51. import fr.opensagres.xdocreport.utils.StringEscapeUtils;
  52. import fr.opensagres.xdocreport.utils.StringUtils;
  53. import fr.opensagres.xdocreport.xhtml.extension.CSSStylePropertyConstants;
  54. import fr.opensagres.xdocreport.xhtml.extension.XHTMLConstants;
  55. import fr.opensagres.xdocreport.xhtml.extension.XHTMLPageContentBuffer;
  56. public class XHTMLMapper
  57. extends XWPFElementVisitor<XHTMLPageContentBuffer>
  58. implements XHTMLConstants, CSSStylePropertyConstants
  59. {
  60. private static final String WORD_MEDIA = "word/media/";
  61. /**
  62. * Logger for this class
  63. */
  64. private static final Logger LOGGER = Logger.getLogger( StyleEngineForIText.class.getName() );
  65. private POIXHTMLPage xhtml = null;
  66. private final IURIResolver resolver;
  67. private final int indent;
  68. private StyleEngineForXHTML styleEngine;
  69. protected OutputStream out;
  70. public XHTMLMapper( XWPFDocument document, int indent, IURIResolver resolver )
  71. {
  72. super( document );
  73. this.resolver = resolver;
  74. this.indent = indent;
  75. styleEngine = new StyleEngineForXHTML( document, false, indent, resolver );
  76. }
  77. @Override
  78. protected XHTMLPageContentBuffer startVisitDocument( OutputStream out )
  79. throws Exception
  80. {
  81. xhtml = new POIXHTMLPage( styleEngine, indent );
  82. this.out = out;
  83. xhtml.getPageBodyContentBody().startElementNotEnclosed( DIV_ELEMENT );
  84. // HTML style
  85. StringBuilder htmlStyle = XHTMLStyleUtil.getStyle( document, defaults );
  86. setAttributStyleIfNeeded( xhtml.getPageBodyContentBody(), htmlStyle );
  87. xhtml.getPageBodyContentBody().endElementNotEnclosed();
  88. return xhtml.getPageBodyContentBody();
  89. }
  90. @Override
  91. protected void endVisitDocument()
  92. throws Exception
  93. {
  94. xhtml.getPageBodyContentBody().endElement( DIV_ELEMENT );
  95. /*
  96. * Writer writer = xhtml.getWriter(); if (writer != null) { xhtml.save(writer); } else
  97. */
  98. {
  99. // OutputStream out = xhtml.getOutputStream();
  100. xhtml.save( out );
  101. }
  102. }
  103. protected XHTMLPageContentBuffer startVisitPargraph( XWPFParagraph paragraph, XHTMLPageContentBuffer parentContainer )
  104. throws Exception
  105. {
  106. styleEngine.startVisitPargraph( paragraph, null );
  107. parentContainer.startElementNotEnclosed( P_ELEMENT );
  108. if ( paragraph.getStyleID() != null )
  109. {
  110. if ( LOGGER.isLoggable( Level.FINE ) )
  111. {
  112. LOGGER.fine( "StyleID " + paragraph.getStyleID() );
  113. }
  114. parentContainer.setAttribute( CLASS_ATTR, paragraph.getStyleID() );
  115. }
  116. // HTML style
  117. StringBuilder htmlStyle =
  118. XHTMLStyleUtil.getStyle( paragraph, super.getXWPFStyle( paragraph.getStyleID() ), defaults );
  119. setAttributStyleIfNeeded( parentContainer, htmlStyle );
  120. parentContainer.endElementNotEnclosed();
  121. return parentContainer;
  122. }
  123. @Override
  124. protected void endVisitPargraph( XWPFParagraph paragraph, XHTMLPageContentBuffer parentContainer,
  125. XHTMLPageContentBuffer paragraphContainer )
  126. throws Exception
  127. {
  128. paragraphContainer.endElement( P_ELEMENT );
  129. }
  130. @Override
  131. protected void visitEmptyRun( XHTMLPageContentBuffer paragraphContainer )
  132. throws Exception
  133. {
  134. paragraphContainer.startEndElement( BR_ELEMENT );
  135. }
  136. @Override
  137. protected void visitRun( XWPFRun run, XHTMLPageContentBuffer paragraphContainer )
  138. throws Exception
  139. {
  140. // HTML style
  141. CTString rStyle = getRStyle( run );
  142. XWPFStyle runStyle = super.getXWPFStyle( rStyle != null ? rStyle.getVal() : null );
  143. StringBuilder htmlStyle =
  144. XHTMLStyleUtil.getStyle( run, runStyle, super.getXWPFStyle( run.getParagraph().getStyle() ), defaults );
  145. List<CTBr> brs = run.getCTR().getBrList();
  146. for ( @SuppressWarnings( "unused" )
  147. CTBr br : brs )
  148. {
  149. paragraphContainer.startEndElement( BR_ELEMENT );
  150. }
  151. List<CTText> texts = run.getCTR().getTList();
  152. for ( CTText ctText : texts )
  153. {
  154. paragraphContainer.startElementNotEnclosed( SPAN_ELEMENT );
  155. setAttributStyleIfNeeded( paragraphContainer, htmlStyle );
  156. paragraphContainer.endElementNotEnclosed();
  157. // Set the text by escaping it with HTML.
  158. paragraphContainer.setText( StringEscapeUtils.escapeHtml( ctText.getStringValue() ) );
  159. paragraphContainer.endElement( SPAN_ELEMENT );
  160. }
  161. super.visitPictures( run, paragraphContainer );
  162. }
  163. @Override
  164. protected XHTMLPageContentBuffer startVisitTable( XWPFTable table, XHTMLPageContentBuffer tableContainer )
  165. throws Exception
  166. {
  167. tableContainer.startElementNotEnclosed( TABLE_ELEMENT );
  168. // XWPFStyle tableStyle = super.getStyle(table.getStyleID());
  169. // HTML style
  170. // StringBuilder htmlStyle = XHTMLStyleUtil.getStyle(table, tableStyle, defaults);
  171. // setAttributStyleIfNeeded(xhtml.getPageBodyContentBody(), htmlStyle);
  172. tableContainer.endElementNotEnclosed();
  173. return tableContainer;
  174. }
  175. @Override
  176. protected void visitTableRow( XWPFTableRow row, XHTMLPageContentBuffer tableContainer )
  177. throws Exception
  178. {
  179. tableContainer.startElementNotEnclosed( TR_ELEMENT );
  180. tableContainer.endElementNotEnclosed();
  181. super.visitTableRow( row, tableContainer );
  182. tableContainer.endElement( TR_ELEMENT );
  183. }
  184. @Override
  185. protected XHTMLPageContentBuffer startVisitTableCell( XWPFTableCell tableCell, XHTMLPageContentBuffer tableContainer )
  186. {
  187. tableContainer.startElementNotEnclosed( TD_ELEMENT );
  188. CTTcPr tcPr = tableCell.getCTTc().getTcPr();
  189. // Colspan
  190. Integer colspan = null;
  191. CTDecimalNumber gridSpan = tcPr.getGridSpan();
  192. if ( gridSpan != null )
  193. {
  194. colspan = gridSpan.getVal().intValue();
  195. }
  196. if ( colspan != null )
  197. {
  198. tableContainer.setAttribute( COLSPAN_ATTR, colspan );
  199. }
  200. // HTML style
  201. StringBuilder htmlStyle = XHTMLStyleUtil.getStyle( tableCell, defaults );
  202. setAttributStyleIfNeeded( tableContainer, htmlStyle );
  203. tableContainer.endElementNotEnclosed();
  204. return tableContainer;
  205. }
  206. @Override
  207. protected void endVisitTableCell( XWPFTableCell cell, XHTMLPageContentBuffer tableContainer,
  208. XHTMLPageContentBuffer tableCellContainer )
  209. {
  210. tableContainer.endElement( TD_ELEMENT );
  211. }
  212. @Override
  213. protected void endVisitTable( XWPFTable table, XHTMLPageContentBuffer parentContainer,
  214. XHTMLPageContentBuffer tableContainer )
  215. throws Exception
  216. {
  217. tableContainer.endElement( TABLE_ELEMENT );
  218. }
  219. @Override
  220. protected void visitPicture( XWPFPicture picture, XHTMLPageContentBuffer parentContainer )
  221. throws Exception
  222. {
  223. parentContainer.startElementNotEnclosed( IMG_ELEMENT );
  224. CTPicture ctPic = picture.getCTPicture();
  225. String blipId = ctPic.getBlipFill().getBlip().getEmbed();
  226. // Src attribute
  227. XWPFPictureData pictureData = XWPFPictureUtil.getPictureData( document, blipId );
  228. if ( pictureData != null )
  229. {
  230. String src = pictureData.getFileName();
  231. if ( StringUtils.isNotEmpty( src ) )
  232. {
  233. src = resolver.resolve( WORD_MEDIA + src );
  234. parentContainer.setAttribute( SRC_ATTR, src );
  235. }
  236. }
  237. StringBuilder htmlStyle = XHTMLStyleUtil.getStyle( picture );
  238. setAttributStyleIfNeeded( parentContainer, htmlStyle );
  239. parentContainer.endElementNotEnclosed();
  240. parentContainer.endElement( IMG_ELEMENT );
  241. }
  242. private void setAttributStyleIfNeeded( XHTMLPageContentBuffer buffer, StringBuilder htmlStyle )
  243. {
  244. if ( htmlStyle.length() > 0 )
  245. {
  246. buffer.setAttribute( STYLE_ATTR, htmlStyle.toString() );
  247. }
  248. }
  249. @Override
  250. protected void visitHeader( CTHdrFtrRef headerRef )
  251. throws Exception
  252. {
  253. // TODO Auto-generated method stub
  254. }
  255. @Override
  256. protected void visitFooter( CTHdrFtrRef footerRef )
  257. throws Exception
  258. {
  259. // TODO Auto-generated method stub
  260. }
  261. }