| 1 | package org.docx4j.samples; |
|---|
| 2 | |
|---|
| 3 | import java.util.ArrayList; |
|---|
| 4 | import java.util.Collections; |
|---|
| 5 | import java.util.HashMap; |
|---|
| 6 | import java.util.Iterator; |
|---|
| 7 | import java.util.List; |
|---|
| 8 | import java.util.Map; |
|---|
| 9 | |
|---|
| 10 | import javax.xml.XMLConstants; |
|---|
| 11 | import javax.xml.bind.Binder; |
|---|
| 12 | import javax.xml.bind.JAXBContext; |
|---|
| 13 | import javax.xml.bind.JAXBException; |
|---|
| 14 | import javax.xml.namespace.NamespaceContext; |
|---|
| 15 | import javax.xml.parsers.DocumentBuilder; |
|---|
| 16 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 17 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 18 | import javax.xml.xpath.XPath; |
|---|
| 19 | import javax.xml.xpath.XPathConstants; |
|---|
| 20 | import javax.xml.xpath.XPathExpressionException; |
|---|
| 21 | import javax.xml.xpath.XPathFactory; |
|---|
| 22 | |
|---|
| 23 | import org.apache.commons.lang.text.StrTokenizer; |
|---|
| 24 | import org.docx4j.XmlUtils; |
|---|
| 25 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 26 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 27 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 28 | import org.docx4j.openpackaging.parts.XmlPart.XmlNamespaceContext; |
|---|
| 29 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 30 | import org.w3c.dom.Node; |
|---|
| 31 | import org.w3c.dom.NodeList; |
|---|
| 32 | |
|---|
| 33 | public class XPathNavigation { |
|---|
| 34 | |
|---|
| 35 | public static JAXBContext context = org.docx4j.jaxb.Context.jc; |
|---|
| 36 | |
|---|
| 37 | // private static XPathFactory xPathFactory; |
|---|
| 38 | // private static XPath xPath; |
|---|
| 39 | // |
|---|
| 40 | // static { |
|---|
| 41 | // |
|---|
| 42 | // // Crimson doesn't support setTextContent; this.writeDocument also fails. |
|---|
| 43 | // // We've already worked around the problem with setTextContent, |
|---|
| 44 | // // but rather than do the same for writeDocument, |
|---|
| 45 | // // let's just stop using it. |
|---|
| 46 | // System.setProperty("javax.xml.parsers.DocumentBuilderFactory", |
|---|
| 47 | // "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); |
|---|
| 48 | // |
|---|
| 49 | // xPathFactory = XPathFactory.newInstance(); |
|---|
| 50 | // xPath = xPathFactory.newXPath(); |
|---|
| 51 | // } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * @param args |
|---|
| 56 | */ |
|---|
| 57 | public static void main(String[] args) throws Exception { |
|---|
| 58 | |
|---|
| 59 | //String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Images.docx"; |
|---|
| 60 | String inputfilepath = System.getProperty("user.dir") + "//tmp//modelo.docx"; |
|---|
| 61 | // String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/fo-200912.xml"; |
|---|
| 62 | |
|---|
| 63 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 64 | MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 65 | |
|---|
| 66 | // Setup the binder |
|---|
| 67 | //Binder<Node> binder = documentPart.getBinder(); |
|---|
| 68 | |
|---|
| 69 | // javax.xml.parsers.DocumentBuilderFactory dbf |
|---|
| 70 | // = DocumentBuilderFactory.newInstance(); |
|---|
| 71 | // dbf.setNamespaceAware(true); |
|---|
| 72 | // org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument(); |
|---|
| 73 | // binder.marshal(documentPart.getJaxbElement(), doc); |
|---|
| 74 | // System.out.println(XmlUtils.w3CDomNodeToString(doc) ); |
|---|
| 75 | |
|---|
| 76 | System.out.println( "Binder established" ); |
|---|
| 77 | |
|---|
| 78 | /* updateXML is not enough to establish the initial association. |
|---|
| 79 | * |
|---|
| 80 | * There are 2 ways to establish it. |
|---|
| 81 | * |
|---|
| 82 | * Something like the following should work, but didn't quite |
|---|
| 83 | * |
|---|
| 84 | javax.xml.parsers.DocumentBuilderFactory dbf |
|---|
| 85 | = DocumentBuilderFactory.newInstance(); |
|---|
| 86 | dbf.setNamespaceAware(true); |
|---|
| 87 | org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument(); |
|---|
| 88 | binder.marshal(documentPart.getJaxbElement(), doc); |
|---|
| 89 | * |
|---|
| 90 | * this does populate doc, but doesn't seem to establish the association?? |
|---|
| 91 | * |
|---|
| 92 | |
|---|
| 93 | * The way which does work is the other way around ie |
|---|
| 94 | * unmarshall from the XML document, so that's what we do. |
|---|
| 95 | * |
|---|
| 96 | * You can do: |
|---|
| 97 | * |
|---|
| 98 | Node node = binder.updateXML(documentPart.getJaxbElement()); |
|---|
| 99 | node = binder.getXMLNode(documentPart.getJaxbElement()); |
|---|
| 100 | |
|---|
| 101 | * OR |
|---|
| 102 | * |
|---|
| 103 | Node node = binder.getXMLNode(documentPart.getJaxbElement()); |
|---|
| 104 | |
|---|
| 105 | * BUT NOT |
|---|
| 106 | |
|---|
| 107 | Node node = binder.updateXML(documentPart.getJaxbElement()); |
|---|
| 108 | |
|---|
| 109 | * |
|---|
| 110 | */ |
|---|
| 111 | |
|---|
| 112 | List<Object> list = documentPart.getJAXBNodesViaXPath("//w:p", true); |
|---|
| 113 | |
|---|
| 114 | System.out.println("got " + list.size() ); |
|---|
| 115 | |
|---|
| 116 | /* |
|---|
| 117 | * Note https://jaxb.dev.java.net/issues/show_bug.cgi?id=459 |
|---|
| 118 | * (Binder.updateXML() fails if called twice) |
|---|
| 119 | * |
|---|
| 120 | * So we can't use XPath after modifying the document |
|---|
| 121 | * via JAXB, unless we get the marshall stuff working. |
|---|
| 122 | * |
|---|
| 123 | */ |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | } |
|---|