| 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.samples; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | import java.io.FileOutputStream; |
|---|
| 26 | |
|---|
| 27 | import javax.xml.bind.JAXBContext; |
|---|
| 28 | import javax.xml.bind.Marshaller; |
|---|
| 29 | |
|---|
| 30 | import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator; |
|---|
| 31 | import org.docx4j.jaxb.Context; |
|---|
| 32 | import org.docx4j.jaxb.NamespacePrefixMapperUtils; |
|---|
| 33 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 34 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | public 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 | } |
|---|