| 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.openpackaging.parts; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | import java.io.IOException; |
|---|
| 25 | import java.lang.reflect.Method; |
|---|
| 26 | import java.math.BigInteger; |
|---|
| 27 | import java.util.ArrayList; |
|---|
| 28 | import java.util.HashMap; |
|---|
| 29 | import java.util.List; |
|---|
| 30 | import java.util.Map; |
|---|
| 31 | import java.util.UUID; |
|---|
| 32 | |
|---|
| 33 | import javax.xml.bind.JAXBContext; |
|---|
| 34 | import javax.xml.bind.JAXBException; |
|---|
| 35 | import javax.xml.namespace.NamespaceContext; |
|---|
| 36 | import javax.xml.transform.Source; |
|---|
| 37 | import javax.xml.transform.Templates; |
|---|
| 38 | import javax.xml.transform.TransformerConfigurationException; |
|---|
| 39 | import javax.xml.transform.stream.StreamSource; |
|---|
| 40 | |
|---|
| 41 | import org.apache.log4j.Logger; |
|---|
| 42 | import org.docx4j.TraversalUtil; |
|---|
| 43 | import org.docx4j.XmlUtils; |
|---|
| 44 | import org.docx4j.customXmlProperties.SchemaRefs; |
|---|
| 45 | import org.docx4j.customXmlProperties.SchemaRefs.SchemaRef; |
|---|
| 46 | import org.docx4j.jaxb.Context; |
|---|
| 47 | import org.docx4j.jaxb.NamespacePrefixMappings; |
|---|
| 48 | import org.docx4j.model.datastorage.CustomXmlDataStorage; |
|---|
| 49 | import org.docx4j.model.sdt.QueryString; |
|---|
| 50 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 51 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 52 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 53 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 54 | import org.docx4j.openpackaging.parts.WordprocessingML.DocumentPart; |
|---|
| 55 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 56 | import org.docx4j.openpackaging.parts.opendope.ConditionsPart; |
|---|
| 57 | import org.docx4j.openpackaging.parts.opendope.XPathsPart; |
|---|
| 58 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 59 | import org.docx4j.wml.Body; |
|---|
| 60 | import org.docx4j.wml.CTDataBinding; |
|---|
| 61 | import org.docx4j.wml.CTSdtContentCell; |
|---|
| 62 | import org.docx4j.wml.P; |
|---|
| 63 | import org.docx4j.wml.SdtPr; |
|---|
| 64 | import org.docx4j.wml.Tag; |
|---|
| 65 | import org.docx4j.wml.Tc; |
|---|
| 66 | import org.opendope.conditions.Condition; |
|---|
| 67 | import org.w3c.dom.Document; |
|---|
| 68 | import org.w3c.dom.Node; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * CustomXmlDataStoragePart contains user custom xml, |
|---|
| 73 | * to which content control bindings can point. |
|---|
| 74 | * |
|---|
| 75 | * This is the modern best practice approach for |
|---|
| 76 | * injecting text/data into a docx. |
|---|
| 77 | * |
|---|
| 78 | * See the Getting Started guide for a general overview, |
|---|
| 79 | * and additional references, or use the docx4j |
|---|
| 80 | * website to search for "custom xml binding". |
|---|
| 81 | * |
|---|
| 82 | * Once you have included this part, and bound content |
|---|
| 83 | * controls to it, nothing further needs to be done for |
|---|
| 84 | * Word to display your data (unless you are using |
|---|
| 85 | * conditional|repeat - see below). |
|---|
| 86 | * |
|---|
| 87 | * However, if you want your data to show up in |
|---|
| 88 | * docx4j PDF|HTML output, you need to run the |
|---|
| 89 | * BindingHandler.applyBindings method first. (TODO: do this |
|---|
| 90 | * automatically) |
|---|
| 91 | * |
|---|
| 92 | * The actual XML data is stored in a CustomXmlDataStorage |
|---|
| 93 | * object, for which accessor is get|setData. (Ths is |
|---|
| 94 | * useful if you want to generate n docx documents, each |
|---|
| 95 | * with different XML.) |
|---|
| 96 | * |
|---|
| 97 | * If this contains XML which is bound in an sdt |
|---|
| 98 | * via w:sdtPr/w:dataBinding, then its rels |
|---|
| 99 | * will point to CustomXmlDataStoragePropertiesPart |
|---|
| 100 | * which gives its datastore itemID. |
|---|
| 101 | * |
|---|
| 102 | * (The datastore itemID is used in the w:dataBinding) |
|---|
| 103 | * |
|---|
| 104 | * The Package contains a hashmap<String, CustomXmlDataStoragePart> |
|---|
| 105 | * to make it easy to get the part to which we apply the |
|---|
| 106 | * XPath. The part is registered as the document is loaded. |
|---|
| 107 | * |
|---|
| 108 | * This class supports content control extensions |
|---|
| 109 | * (ie od:condition, od:repeat). Use the |
|---|
| 110 | * OpenDoPEHandler class to process these. |
|---|
| 111 | * |
|---|
| 112 | * @author jharrop |
|---|
| 113 | * |
|---|
| 114 | */ |
|---|
| 115 | public final class CustomXmlDataStoragePart extends Part { |
|---|
| 116 | |
|---|
| 117 | private static Logger log = Logger.getLogger(CustomXmlDataStoragePart.class); |
|---|
| 118 | |
|---|
| 119 | public CustomXmlDataStoragePart(PartName partName) throws InvalidFormatException { |
|---|
| 120 | super(partName); |
|---|
| 121 | init(); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // public CustomXmlDataStoragePart() throws InvalidFormatException { |
|---|
| 125 | // super(new PartName("/customXml/item1.xml")); |
|---|
| 126 | // init(); |
|---|
| 127 | // } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * @param parts The parts present in the package to which this will be added. |
|---|
| 131 | * If for example /customXml/item1.xml already exists, this allows |
|---|
| 132 | * the name /customXml/item2.xml to be generated. |
|---|
| 133 | * @throws InvalidFormatException |
|---|
| 134 | */ |
|---|
| 135 | public CustomXmlDataStoragePart(Parts parts) throws InvalidFormatException { |
|---|
| 136 | |
|---|
| 137 | int partNum = 1; |
|---|
| 138 | if (parts!=null) { |
|---|
| 139 | while (parts.get(new PartName("/customXml/item" + partNum + ".xml"))!=null) { |
|---|
| 140 | partNum++; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | this.partName = new PartName("/customXml/item" + partNum + ".xml"); |
|---|
| 145 | log.info("Using PartName /customXml/item" + partNum + ".xml"); |
|---|
| 146 | init(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | public void init() { |
|---|
| 151 | |
|---|
| 152 | // Used if this Part is added to [Content_Types].xml |
|---|
| 153 | setContentType(new org.docx4j.openpackaging.contenttype.ContentType( |
|---|
| 154 | org.docx4j.openpackaging.contenttype.ContentTypes.OFFICEDOCUMENT_CUSTOMXML_DATASTORAGE)); |
|---|
| 155 | |
|---|
| 156 | // Used when this Part is added to a rels |
|---|
| 157 | setRelationshipType(Namespaces.CUSTOM_XML_DATA_STORAGE); |
|---|
| 158 | |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | private CustomXmlDataStorage data; |
|---|
| 162 | /** |
|---|
| 163 | * @return the data |
|---|
| 164 | */ |
|---|
| 165 | public CustomXmlDataStorage getData() { |
|---|
| 166 | return data; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * @param data the data to set |
|---|
| 171 | */ |
|---|
| 172 | public void setData(CustomXmlDataStorage data) { |
|---|
| 173 | this.data = data; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public boolean isContentEqual(Part other) throws Docx4JException { |
|---|
| 177 | |
|---|
| 178 | if (!(other instanceof CustomXmlDataStoragePart)) |
|---|
| 179 | return false; |
|---|
| 180 | |
|---|
| 181 | Document doc1 = data.getDocument(); |
|---|
| 182 | Document doc2 = ((CustomXmlDataStoragePart)other).data.getDocument(); |
|---|
| 183 | |
|---|
| 184 | return doc1.isEqualNode(doc2); |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | } |
|---|