Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   *  Copyright 2007-2010, Plutext Pty Ltd.
   *  This file is part of docx4j.
      docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
      you may not use this file except in compliance with the License. 
      You may obtain a copy of the License at 
     Unless required by applicable law or agreed to in writing, software 
     distributed under the License is distributed on an "AS IS" BASIS, 
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
     See the License for the specific language governing permissions and 
     limitations under the License.
 package org.docx4j.convert.out.html;
HtmlExporterNG (now removed - see ) had 2 salient features: 1. CSS .xx_pPr and .xx_rPr are created / split out for each docx paragraph style 2. Each docx style is resolved into its effective style, by the PropertyResolver When roundtripping HTML content, this had adverse consequences (neither too bad, but adding to our work): (a) It is harder to tell what has been added to the style definition in an editor (if/when that is possible) (b) Have to convert .xx_pPr and .xx_rPr back to xx Also, we're not really taking advantage of the ability of CSS to cascade. So this HtmlExporterNG2 takes a different approach. It creates a css definition for each style in use (either directly or indirectly), and in the document, on the element's class attribute, puts the set of style names in the hierarchy into . The CSS rules are written in a depth or breadth first traversal of the style graph, so that conflicts are handled correctly. TODO: handle document defaults.

 public class HtmlExporterNG2 extends  AbstractHtmlExporter {
 	protected static final int DEFAULT_OUTPUT_SIZE = 102400;
 	protected static Logger log = LoggerFactory.getLogger(HtmlExporterNG2.class);
 	public void setWmlPackage(WordprocessingMLPackage wmlPackage) {
 		this. = wmlPackage;
 	public void setHtmlSettings(HTMLSettings htmlSettings) {
 		this. = htmlSettings;
	static Templates xslt;		
org/docx4j/convert/out/html/docx2xhtmlNG2.xslt will be used by default to transform the docx to html. This method allows you to use your own xslt instead.

	public static void setXslt(Templates xslt) {
	// Implement the interface.  	
	public void output(javax.xml.transform.Result resultthrows Docx4JException {
		if (==null) {
			throw new Docx4JException("Must setWmlPackage");
		if (==null) {
			.debug("Using empty HtmlSettings");
		try {
catch (Exception e) {
			throw new Docx4JException("Failed to create HTML output"e);
	// End interface
Create an html version of the document.

result The javax.xml.transform.Result object to transform into
	public void html(WordprocessingMLPackage wmlPackage,
			javax.xml.transform.Result resultHTMLSettings htmlSettings)
			throws Exception {
	Transformer transformer = null;
		if (( != null) && (htmlSettings.getCustomXsltTemplates() == null)) {
		if ((wmlPackage != null) && (htmlSettings.getWmlPackage() == null)) {
		try {
			transformer = XmlUtils.getTransformerFactory().newTransformer();
			throw new Docx4JException("Exception creating identity transformer to output result: " + e.getMessage(), e);
		try {
			// Resolution of doctype eg
			// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
			// may be extremely slow, so instead of
			// transformer.transform(new StreamSource(new ByteArrayInputStream(bytes)), result);
			// use
		    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		    DocumentBuilder db = dbFactory.newDocumentBuilder();
		    // that feature is enough; alternatively, the below also works.  Use both for good measure.
		    db.setEntityResolver(new EntityResolver() {
		        public InputSource resolveEntity(String publicIdString systemIdthrows SAXExceptionIOException {
		            return new InputSource(new StringReader("")); // Returns a valid dummy source
		            // returning null here is no good; it seems to cause:
		        	//    XMLEntityManager.reset(XMLComponentManager) 
		        	//    XIncludeAwareParserConfiguration(XML11Configuration).resetCommon() 
		            // with the result that this entity resolver is not used!
		    org.w3c.dom.Document doc = null;
		    if (.isDebugEnabled()) {
			    byte[] bytes = outStream.toByteArray();
			    .debug(new String(bytes));
			    doc = db.parse(new ByteArrayInputStream(bytes));
		    } else {
		    	doc = db.parse(new ByteArrayInputStream(outStream.toByteArray()));
		    transformer.transform(new DOMSource(doc.getDocumentElement()), result);
catch (TransformerException e) {
			throw new Docx4JException("Exception dumping outputstream to output result: " + e.getMessage(), e);
New to GrepCode? Check out our FAQ X