| 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 | import java.util.HashMap; |
|---|
| 25 | |
|---|
| 26 | import javax.xml.bind.JAXBContext; |
|---|
| 27 | |
|---|
| 28 | import org.docx4j.XmlUtils; |
|---|
| 29 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 30 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 31 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 32 | import org.docx4j.wml.Document; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | public class UnmarshallFromTemplate { |
|---|
| 36 | |
|---|
| 37 | public static JAXBContext context = org.docx4j.jaxb.Context.jc; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * @param args |
|---|
| 41 | */ |
|---|
| 42 | public static void main(String[] args) throws Exception { |
|---|
| 43 | |
|---|
| 44 | String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/unmarshallFromTemplateExample.docx"; |
|---|
| 45 | |
|---|
| 46 | boolean save = true; |
|---|
| 47 | String outputfilepath = System.getProperty("user.dir") + "/test-out.docx"; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | // Open a document from the file system |
|---|
| 51 | // 1. Load the Package |
|---|
| 52 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 53 | |
|---|
| 54 | // 2. Fetch the document part |
|---|
| 55 | MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 56 | |
|---|
| 57 | org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart |
|---|
| 58 | .getJaxbElement(); |
|---|
| 59 | |
|---|
| 60 | //xml --> string |
|---|
| 61 | String xml = XmlUtils.marshaltoString(wmlDocumentEl, true); |
|---|
| 62 | |
|---|
| 63 | HashMap<String, String> mappings = new HashMap<String, String>(); |
|---|
| 64 | |
|---|
| 65 | mappings.put("colour", "green"); |
|---|
| 66 | mappings.put("icecream", "chocolate"); |
|---|
| 67 | |
|---|
| 68 | //valorize template |
|---|
| 69 | Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings); |
|---|
| 70 | |
|---|
| 71 | //change JaxbElement |
|---|
| 72 | documentPart.setJaxbElement((Document) obj); |
|---|
| 73 | |
|---|
| 74 | // Save it |
|---|
| 75 | if (save) { |
|---|
| 76 | SaveToZipFile saver = new SaveToZipFile(wordMLPackage); |
|---|
| 77 | saver.save(outputfilepath); |
|---|
| 78 | System.out.println( "Saved output to:" + outputfilepath ); |
|---|
| 79 | } else { |
|---|
| 80 | // Display the Main Document Part. |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | } |
|---|