| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.samples; |
|---|
| 22 | |
|---|
| 23 | import java.io.FileInputStream; |
|---|
| 24 | import java.io.FileOutputStream; |
|---|
| 25 | import java.io.OutputStream; |
|---|
| 26 | |
|---|
| 27 | import javax.xml.bind.JAXBContext; |
|---|
| 28 | import javax.xml.bind.JAXBElement; |
|---|
| 29 | import javax.xml.bind.Marshaller; |
|---|
| 30 | import javax.xml.bind.Unmarshaller; |
|---|
| 31 | |
|---|
| 32 | import org.docx4j.XmlUtils; |
|---|
| 33 | import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator; |
|---|
| 34 | import org.docx4j.convert.out.html.AbstractHtmlExporter; |
|---|
| 35 | import org.docx4j.jaxb.Context; |
|---|
| 36 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 37 | import org.w3c.dom.Attr; |
|---|
| 38 | import org.w3c.dom.Document; |
|---|
| 39 | import org.w3c.dom.NamedNodeMap; |
|---|
| 40 | import org.w3c.dom.Node; |
|---|
| 41 | import org.w3c.dom.NodeList; |
|---|
| 42 | |
|---|
| 43 | public class CreateXmlCss { |
|---|
| 44 | |
|---|
| 45 | public final static String HTML_TOP = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" |
|---|
| 46 | +"\n<html>" |
|---|
| 47 | +"\n<head>" |
|---|
| 48 | +"\n<style type=\"text/css\">"; |
|---|
| 49 | |
|---|
| 50 | public final static String CSS_FOR_TABLES = "\n\n/* TABLE STYLES */" |
|---|
| 51 | + "\n.table { display:table; table-layout:fixed; border-color: #600; border-style: solid; border-width: 0 0 1px 1px; border-spacing: 0; border-collapse: collapse;}" |
|---|
| 52 | + "\n.tr { display:table-row;}" |
|---|
| 53 | + "\n.td { display:table-cell; border-color: #600; border-style: solid; margin: 0; padding: 4px; border-width: 1px 1px 0 0; background-color: #FFC;}\n"; |
|---|
| 54 | |
|---|
| 55 | public final static String HTML_MIDDLE = "</style>" |
|---|
| 56 | + "\n</head>\n<body>"; |
|---|
| 57 | |
|---|
| 58 | public final static String HTML_TAIL = "\n </body>" |
|---|
| 59 | + "\n <script type=\"text/javascript\" src=\"javeline_xpath.js\"></script>" |
|---|
| 60 | + "\n <script type=\"text/javascript\" src=\"wml_fix.js\"></script>" |
|---|
| 61 | + "\n</html>"; |
|---|
| 62 | |
|---|
| 63 | public static void main(String[] args) |
|---|
| 64 | throws Exception { |
|---|
| 65 | |
|---|
| 66 | boolean save = true; |
|---|
| 67 | |
|---|
| 68 | // If you want to be able to display non-external images, |
|---|
| 69 | // or you are extending wml_fix.js to do client-side |
|---|
| 70 | // style resolution, you'll want to embed the entire |
|---|
| 71 | // Flat OPC, rather than just document.xml |
|---|
| 72 | boolean useFlatOPC = true; |
|---|
| 73 | |
|---|
| 74 | // String inputfilepath = System.getProperty("user.dir") + "/sample-docs/numbering-multilevel.docx"; |
|---|
| 75 | //String inputfilepath = System.getProperty("user.dir") + "/test3.docx"; |
|---|
| 76 | // String inputfilepath = "/home/dev/workspace/docx4all/sample-docs/docx4all-CurrentDocxFeatures.docx"; |
|---|
| 77 | String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/StyleResolution.xml"; |
|---|
| 78 | |
|---|
| 79 | System.out.println(inputfilepath); |
|---|
| 80 | WordprocessingMLPackage wordMLPackage; |
|---|
| 81 | org.docx4j.xmlPackage.Package flatOPC = null; |
|---|
| 82 | if (inputfilepath.endsWith(".xml")) { |
|---|
| 83 | |
|---|
| 84 | JAXBContext jc = Context.jcXmlPackage; |
|---|
| 85 | Unmarshaller u = jc.createUnmarshaller(); |
|---|
| 86 | u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); |
|---|
| 87 | |
|---|
| 88 | flatOPC = (org.docx4j.xmlPackage.Package)((JAXBElement)u.unmarshal( |
|---|
| 89 | new javax.xml.transform.stream.StreamSource(new FileInputStream(inputfilepath)))).getValue(); |
|---|
| 90 | |
|---|
| 91 | org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage = new org.docx4j.convert.in.FlatOpcXmlImporter( flatOPC); |
|---|
| 92 | |
|---|
| 93 | wordMLPackage = (WordprocessingMLPackage)xmlPackage.get(); |
|---|
| 94 | |
|---|
| 95 | } else { |
|---|
| 96 | wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // Convert it to HTML (ie no self-closing tags) |
|---|
| 100 | Object o; |
|---|
| 101 | Document doc; |
|---|
| 102 | if (useFlatOPC) { |
|---|
| 103 | if (flatOPC==null) { |
|---|
| 104 | // We must have started with a .docx |
|---|
| 105 | FlatOpcXmlCreator worker = new FlatOpcXmlCreator(wordMLPackage); |
|---|
| 106 | o = worker.get(); |
|---|
| 107 | } else { |
|---|
| 108 | o = flatOPC; |
|---|
| 109 | } |
|---|
| 110 | doc = XmlUtils.marshaltoW3CDomDocument(o, Context.jcXmlPackage); |
|---|
| 111 | } else { |
|---|
| 112 | o = wordMLPackage.getMainDocumentPart().getJaxbElement(); |
|---|
| 113 | doc = XmlUtils.marshaltoW3CDomDocument(o); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | StringBuffer buff = new StringBuffer(); |
|---|
| 117 | serialise(doc, buff, " " ); |
|---|
| 118 | |
|---|
| 119 | // Generate CSS |
|---|
| 120 | String css_other = AbstractHtmlExporter.getCssForStyles(wordMLPackage); |
|---|
| 121 | |
|---|
| 122 | // Join it all together |
|---|
| 123 | String result = HTML_TOP + CSS_FOR_TABLES + css_other + HTML_MIDDLE + buff.toString() + HTML_TAIL; |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | OutputStream os; |
|---|
| 127 | if (save) { |
|---|
| 128 | os = new java.io.FileOutputStream(inputfilepath + ".html"); |
|---|
| 129 | } else { |
|---|
| 130 | os = System.out; |
|---|
| 131 | } |
|---|
| 132 | os.write(result.getBytes()); |
|---|
| 133 | os.close(); |
|---|
| 134 | if (save) { |
|---|
| 135 | System.out.println("Saved: " + inputfilepath + ".html "); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public static void serialise( Node sourceNode, StringBuffer result, String indent ) { |
|---|
| 141 | |
|---|
| 142 | //log.debug("node type" + sourceNode.getNodeType()); |
|---|
| 143 | |
|---|
| 144 | switch (sourceNode.getNodeType() ) { |
|---|
| 145 | |
|---|
| 146 | case Node.DOCUMENT_NODE: // type 9 |
|---|
| 147 | |
|---|
| 148 | // recurse on each child |
|---|
| 149 | NodeList nodes = sourceNode.getChildNodes(); |
|---|
| 150 | if (nodes != null) { |
|---|
| 151 | for (int i=0; i<nodes.getLength(); i++) { |
|---|
| 152 | serialise((Node)nodes.item(i), result, indent); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | break; |
|---|
| 156 | case Node.ELEMENT_NODE: |
|---|
| 157 | |
|---|
| 158 | // Copy of the node itself |
|---|
| 159 | result.append("\n" + indent + "<" + sourceNode.getNodeName() ); |
|---|
| 160 | |
|---|
| 161 | // .. its attributes |
|---|
| 162 | NamedNodeMap atts = sourceNode.getAttributes(); |
|---|
| 163 | for (int i = 0 ; i < atts.getLength() ; i++ ) { |
|---|
| 164 | Attr attr = (Attr)atts.item(i); |
|---|
| 165 | |
|---|
| 166 | // if (attr.getNamespaceURI()!=null |
|---|
| 167 | // && attr.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/")) { |
|---|
| 168 | // result.append(" " + attr.getLocalName() + "=\"" + attr.getValue() + "\""); |
|---|
| 169 | // } else { |
|---|
| 170 | // result.append(" " + attr.getName() + "=\"" + attr.getValue() + "\""); |
|---|
| 171 | // } |
|---|
| 172 | result.append(" " + attr.getName() + "=\"" + attr.getValue() + "\""); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | result.append(">"); |
|---|
| 176 | |
|---|
| 177 | if (sourceNode.getChildNodes() == null |
|---|
| 178 | || sourceNode.getChildNodes().getLength() ==0) { |
|---|
| 179 | // this is where we force tags to be |
|---|
| 180 | // non self-closing |
|---|
| 181 | result.append("</" + sourceNode.getNodeName() + ">" ); |
|---|
| 182 | } else { |
|---|
| 183 | // recurse on each child |
|---|
| 184 | NodeList children = sourceNode.getChildNodes(); |
|---|
| 185 | boolean hasNonTextChild = false; |
|---|
| 186 | if (children != null) { |
|---|
| 187 | for (int i=0; i<children.getLength(); i++) { |
|---|
| 188 | //treeCopy( (DTMNodeProxy)children.item(i), newChild); |
|---|
| 189 | if (children.item(i).getNodeType() != Node.TEXT_NODE) { |
|---|
| 190 | hasNonTextChild = true; |
|---|
| 191 | } |
|---|
| 192 | serialise( (Node)children.item(i), result, indent+ " "); |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | if (hasNonTextChild) { |
|---|
| 196 | result.append("\n" + indent); |
|---|
| 197 | } |
|---|
| 198 | result.append("</" + sourceNode.getNodeName() + ">" ); |
|---|
| 199 | } |
|---|
| 200 | break; |
|---|
| 201 | |
|---|
| 202 | case Node.TEXT_NODE: |
|---|
| 203 | //Node textNode = destParent.getOwnerDocument().createTextNode(sourceNode.getNodeValue()); |
|---|
| 204 | result.append(sourceNode.getNodeValue()); |
|---|
| 205 | break; |
|---|
| 206 | |
|---|
| 207 | // case Node.CDATA_SECTION_NODE: |
|---|
| 208 | // writer.write("<![CDATA[" + |
|---|
| 209 | // node.getNodeValue() + "]]>"); |
|---|
| 210 | // break; |
|---|
| 211 | // |
|---|
| 212 | // case Node.COMMENT_NODE: |
|---|
| 213 | // writer.write(indentLevel + "<!-- " + |
|---|
| 214 | // node.getNodeValue() + " -->"); |
|---|
| 215 | // writer.write(lineSeparator); |
|---|
| 216 | // break; |
|---|
| 217 | // |
|---|
| 218 | // case Node.PROCESSING_INSTRUCTION_NODE: |
|---|
| 219 | // writer.write("<?" + node.getNodeName() + |
|---|
| 220 | // " " + node.getNodeValue() + |
|---|
| 221 | // "?>"); |
|---|
| 222 | // writer.write(lineSeparator); |
|---|
| 223 | // break; |
|---|
| 224 | // |
|---|
| 225 | // case Node.ENTITY_REFERENCE_NODE: |
|---|
| 226 | // writer.write("&" + node.getNodeName() + ";"); |
|---|
| 227 | // break; |
|---|
| 228 | // |
|---|
| 229 | // case Node.DOCUMENT_TYPE_NODE: |
|---|
| 230 | // DocumentType docType = (DocumentType)node; |
|---|
| 231 | // writer.write("<!DOCTYPE " + docType.getName()); |
|---|
| 232 | // if (docType.getPublicId() != null) { |
|---|
| 233 | // System.out.print(" PUBLIC \"" + |
|---|
| 234 | // docType.getPublicId() + "\" "); |
|---|
| 235 | // } else { |
|---|
| 236 | // writer.write(" SYSTEM "); |
|---|
| 237 | // } |
|---|
| 238 | // writer.write("\"" + docType.getSystemId() + "\">"); |
|---|
| 239 | // writer.write(lineSeparator); |
|---|
| 240 | // break; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | } |
|---|