| 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.List; |
|---|
| 25 | |
|---|
| 26 | import javax.xml.bind.JAXBContext; |
|---|
| 27 | |
|---|
| 28 | import org.docx4j.TraversalUtil; |
|---|
| 29 | import org.docx4j.XmlUtils; |
|---|
| 30 | import org.docx4j.TraversalUtil.Callback; |
|---|
| 31 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 32 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 33 | import org.docx4j.wml.Body; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * This sample is useful if you want to see what objects are used in your document.xml. |
|---|
| 38 | * |
|---|
| 39 | * This shows a general approach for traversing the JAXB object tree in |
|---|
| 40 | * the Main Document part. It can also be applied to headers, footers etc. |
|---|
| 41 | * |
|---|
| 42 | * It is an alternative to XSLT, and doesn't require marshalling/unmarshalling. |
|---|
| 43 | * |
|---|
| 44 | * If many cases, the method getJAXBNodesViaXPath |
|---|
| 45 | * may be more convenient. |
|---|
| 46 | * |
|---|
| 47 | * @author jharrop |
|---|
| 48 | * |
|---|
| 49 | */ |
|---|
| 50 | public class OpenMainDocumentAndTraverse extends AbstractSample { |
|---|
| 51 | |
|---|
| 52 | public static JAXBContext context = org.docx4j.jaxb.Context.jc; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * @param args |
|---|
| 56 | */ |
|---|
| 57 | public static void main(String[] args) throws Exception { |
|---|
| 58 | |
|---|
| 59 | /* |
|---|
| 60 | * You can invoke this from an OS command line with something like: |
|---|
| 61 | * |
|---|
| 62 | * java -cp dist/docx4j.jar:dist/log4j-1.2.15.jar |
|---|
| 63 | * org.docx4j.samples.OpenMainDocumentAndTraverse inputdocx |
|---|
| 64 | * |
|---|
| 65 | * Note the minimal set of supporting jars. |
|---|
| 66 | * |
|---|
| 67 | * If there are any images in the document, you will also need: |
|---|
| 68 | * |
|---|
| 69 | * dist/xmlgraphics-commons-1.4.jar:dist/commons-logging-1.1.1.jar |
|---|
| 70 | */ |
|---|
| 71 | |
|---|
| 72 | try { |
|---|
| 73 | getInputFilePath(args); |
|---|
| 74 | } catch (IllegalArgumentException e) { |
|---|
| 75 | inputfilepath = System.getProperty("user.dir") |
|---|
| 76 | + "/sample-docs/word/sample-docx.xml"; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage |
|---|
| 80 | .load(new java.io.File(inputfilepath)); |
|---|
| 81 | MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); |
|---|
| 82 | |
|---|
| 83 | org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart |
|---|
| 84 | .getJaxbElement(); |
|---|
| 85 | Body body = wmlDocumentEl.getBody(); |
|---|
| 86 | |
|---|
| 87 | new TraversalUtil(body, |
|---|
| 88 | |
|---|
| 89 | new Callback() { |
|---|
| 90 | |
|---|
| 91 | String indent = ""; |
|---|
| 92 | |
|---|
| 93 | @Override |
|---|
| 94 | public List<Object> apply(Object o) { |
|---|
| 95 | |
|---|
| 96 | String text = ""; |
|---|
| 97 | if (o instanceof org.docx4j.wml.Text) |
|---|
| 98 | text = ((org.docx4j.wml.Text) o).getValue(); |
|---|
| 99 | |
|---|
| 100 | System.out.println(indent + o.getClass().getName() + " \"" |
|---|
| 101 | + text + "\""); |
|---|
| 102 | return null; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | @Override |
|---|
| 106 | public boolean shouldTraverse(Object o) { |
|---|
| 107 | return true; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | // Depth first |
|---|
| 111 | @Override |
|---|
| 112 | public void walkJAXBElements(Object parent) { |
|---|
| 113 | |
|---|
| 114 | indent += " "; |
|---|
| 115 | |
|---|
| 116 | List children = getChildren(parent); |
|---|
| 117 | if (children != null) { |
|---|
| 118 | |
|---|
| 119 | for (Object o : children) { |
|---|
| 120 | |
|---|
| 121 | // if its wrapped in javax.xml.bind.JAXBElement, get its |
|---|
| 122 | // value |
|---|
| 123 | o = XmlUtils.unwrap(o); |
|---|
| 124 | |
|---|
| 125 | this.apply(o); |
|---|
| 126 | |
|---|
| 127 | if (this.shouldTraverse(o)) { |
|---|
| 128 | walkJAXBElements(o); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | indent = indent.substring(0, indent.length() - 4); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | @Override |
|---|
| 138 | public List<Object> getChildren(Object o) { |
|---|
| 139 | return TraversalUtil.getChildrenImpl(o); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | ); |
|---|
| 145 | |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | } |
|---|