source: trunk/docx4j/src/main/java/org/docx4j/samples/CustomXmlBinding.java @ 1310

Revision 1310, 5.2 KB checked in by jharrop, 19 months ago (diff)

Move OpenDoPE preprocessing and traditional bind processing into classes of their own (since they can act on more than one CustomXmlDataStoragePart?).

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.samples;
22
23
24import java.io.FileInputStream;
25import java.util.List;
26
27import javax.xml.bind.JAXBContext;
28import javax.xml.bind.JAXBElement;
29import javax.xml.bind.JAXBException;
30import javax.xml.bind.Unmarshaller;
31
32import org.docx4j.XmlUtils;
33import org.docx4j.fonts.IdentityPlusMapper;
34import org.docx4j.jaxb.Context;
35import org.docx4j.model.datastorage.BindingHandler;
36import org.docx4j.model.datastorage.CustomXmlDataStorage;
37import org.docx4j.model.datastorage.CustomXmlDataStorageImpl;
38import org.docx4j.openpackaging.io.LoadFromZipFile;
39import org.docx4j.openpackaging.io.SaveToZipFile;
40import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
41import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart;
42import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
43import org.docx4j.wml.Body;
44
45
46/**
47 * This sample demonstrates populating content controls
48 * from a custom xml part (based on the xpaths given
49 * in the content controls).
50 *
51 * Word does this itself automatically, but if you
52 * have a Word document containing content controls,
53 * this sample demonstrates how you could
54 * populate those programmatically.  You might
55 * then use docx4j to generate a pdf or an html.
56 */
57public class CustomXmlBinding {
58       
59        public static JAXBContext context = org.docx4j.jaxb.Context.jc; 
60
61        /**
62         * @param args
63         */
64        public static void main(String[] args) throws Exception {
65               
66                String example_value_to_inject = "Flynnie";
67
68                // Convenient to read from .xml file,
69                // so it is easy to manually edit it (ie without having to unzip etc etc)
70                String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/MedicalChartSample.xml";
71//              String inputfilepath = System.getProperty("user.dir") + "/tmp/MedicalChartSample.docx";
72                        // To get the sample file, google for "Content Control Toolkit"
73                        // I've saved as xml, but docx works as well
74
75                String outputfilepath = System.getProperty("user.dir") + "/tmp/MedicalChartSample-OUT.docx";
76               
77                String itemId = "{DD6E220C-54BC-47B3-8AE8-A0A61D4934FF}".toLowerCase();
78
79                // Load the Package
80                WordprocessingMLPackage wordMLPackage;
81                if (inputfilepath.endsWith(".xml")) {
82                       
83                        JAXBContext jc = Context.jcXmlPackage;
84                        Unmarshaller u = jc.createUnmarshaller();
85                        u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
86
87                        org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package)((JAXBElement)u.unmarshal(
88                                        new javax.xml.transform.stream.StreamSource(new FileInputStream(inputfilepath)))).getValue(); 
89
90                        org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage = new org.docx4j.convert.in.FlatOpcXmlImporter( wmlPackageEl); 
91
92                        wordMLPackage = (WordprocessingMLPackage)xmlPackage.get(); 
93               
94                } else {
95                        wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
96                }               
97               
98               
99                // Get the part
100                CustomXmlDataStoragePart customXmlDataStoragePart = wordMLPackage.getCustomXmlDataStorageParts().get(itemId);
101               
102                // Get the contents             
103                CustomXmlDataStorage customXmlDataStorage = customXmlDataStoragePart.getData();
104                        // In a real program what you might do is populate this with your own data.
105                        // You could replace the whole part, or as we show below, just set some
106                        // particular value
107               
108                               
109                // Change its contents
110                ((CustomXmlDataStorageImpl)customXmlDataStorage).setNodeValueAtXPath("/ns0:chart[1]/ns0:personal[1]/ns0:name[1]", example_value_to_inject,
111                                "xmlns:ns0='http://schemas.medchart'"); 
112                /*<chart xmlns="http://schemas.medchart" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
113                  <personal>
114                    <id/>
115                    <ssn/>
116                    <name>Fred</name>
117                    <address>Fitzroy</address>
118                    <homePhone>9419</homePhone>
119                    <insurance/>
120                    <dob/>
121                    <officePhone/>
122                    <occupation/>
123                    <employer/>
124                    <photo/>
125                    <maritalstatus/>
126                  </personal> ... */
127                System.out.println(XmlUtils.w3CDomNodeToString(customXmlDataStorage.getDocument()));
128               
129                // Apply the bindings
130                BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
131               
132                // If you inspect the output, you should see your data in 2 places:
133                // 1. the custom xml part
134                // 2. (more importantly) the main document part
135                System.out.println(
136                                XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
137                                );
138               
139                // Display as a PDF
140//              wordMLPackage.setFontMapper(new IdentityPlusMapper());         
141//              org.docx4j.convert.out.pdf.PdfConversion c
142//                      = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
143//                      c.view();
144               
145                wordMLPackage.save(new java.io.File(outputfilepath) );
146                System.out.println("..done");
147        }
148               
149
150}
Note: See TracBrowser for help on using the repository browser.