source: trunk/docx4j/src/main/java/org/docx4j/samples/ExportInPackageFormat.java @ 1232

Revision 1232, 2.6 KB checked in by jharrop, 20 months ago (diff)

Make it possible to run these samples from the command line.

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
24
25import java.io.FileOutputStream;
26
27import javax.xml.bind.JAXBContext;
28import javax.xml.bind.Marshaller;
29
30import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator;
31import org.docx4j.jaxb.Context;
32import org.docx4j.jaxb.NamespacePrefixMapperUtils;
33import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
34import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
35
36
37public class ExportInPackageFormat extends AbstractSample {
38
39        /**
40         * @param args
41         */
42        public static void main(String[] args) throws Exception {
43
44                try {
45                        getInputFilePath(args);
46                } catch (IllegalArgumentException e) {
47                        inputfilepath = System.getProperty("user.dir") + "/sample-docs/sample-docx.xml";
48                }
49               
50                // Do we want to save output?
51                boolean save = true;
52                // If so, whereto?
53                outputfilepath = inputfilepath + ".xml";
54               
55                // Open a document from the file system
56                // 1. Load the Package
57                WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
58               
59                // Create a org.docx4j.wml.Package object
60                FlatOpcXmlCreator worker = new FlatOpcXmlCreator(wmlPackage);
61                org.docx4j.xmlPackage.Package pkg = worker.get();
62       
63        // Now marshall it
64                JAXBContext jc = Context.jcXmlPackage;
65                Marshaller marshaller=jc.createMarshaller();
66               
67                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
68                NamespacePrefixMapperUtils.setProperty(marshaller, 
69                                NamespacePrefixMapperUtils.getPrefixMapper());                 
70               
71
72               
73                //org.w3c.dom.Document doc = org.docx4j.XmlUtils.neww3cDomDocument();   
74                if (save) {
75                        marshaller.marshal(pkg, new FileOutputStream(outputfilepath));                         
76                        System.out.println( "\n\n .. written to " + outputfilepath);
77                } else {
78                        // Display its contents
79                        System.out.println( "\n\n OUTPUT " );
80                        System.out.println( "====== \n\n " );   
81                        marshaller.marshal(pkg, System.out);                           
82                }
83               
84               
85                               
86        }
87       
88       
89
90}
Note: See TracBrowser for help on using the repository browser.