source: trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/CustomXmlDataStoragePart.java @ 1400

Revision 1400, 5.8 KB checked in by jharrop, 16 months ago (diff)

AlteredParts? WIP, inc basic unit testing (to be fleshed out next).

Line 
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
21package org.docx4j.openpackaging.parts;
22
23
24import java.io.IOException;
25import java.lang.reflect.Method;
26import java.math.BigInteger;
27import java.util.ArrayList;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
31import java.util.UUID;
32
33import javax.xml.bind.JAXBContext;
34import javax.xml.bind.JAXBException;
35import javax.xml.namespace.NamespaceContext;
36import javax.xml.transform.Source;
37import javax.xml.transform.Templates;
38import javax.xml.transform.TransformerConfigurationException;
39import javax.xml.transform.stream.StreamSource;
40
41import org.apache.log4j.Logger;
42import org.docx4j.TraversalUtil;
43import org.docx4j.XmlUtils;
44import org.docx4j.customXmlProperties.SchemaRefs;
45import org.docx4j.customXmlProperties.SchemaRefs.SchemaRef;
46import org.docx4j.jaxb.Context;
47import org.docx4j.jaxb.NamespacePrefixMappings;
48import org.docx4j.model.datastorage.CustomXmlDataStorage;
49import org.docx4j.model.sdt.QueryString;
50import org.docx4j.openpackaging.exceptions.Docx4JException;
51import org.docx4j.openpackaging.exceptions.InvalidFormatException;
52import org.docx4j.openpackaging.io.SaveToZipFile;
53import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
54import org.docx4j.openpackaging.parts.WordprocessingML.DocumentPart;
55import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
56import org.docx4j.openpackaging.parts.opendope.ConditionsPart;
57import org.docx4j.openpackaging.parts.opendope.XPathsPart;
58import org.docx4j.openpackaging.parts.relationships.Namespaces;
59import org.docx4j.wml.Body;
60import org.docx4j.wml.CTDataBinding;
61import org.docx4j.wml.CTSdtContentCell;
62import org.docx4j.wml.P;
63import org.docx4j.wml.SdtPr;
64import org.docx4j.wml.Tag;
65import org.docx4j.wml.Tc;
66import org.opendope.conditions.Condition;
67import org.w3c.dom.Document;
68import 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 */
115public 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}
Note: See TracBrowser for help on using the repository browser.