| 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.Collection; |
|---|
| 25 | import java.util.HashMap; |
|---|
| 26 | import java.util.Iterator; |
|---|
| 27 | |
|---|
| 28 | import javax.xml.bind.JAXBElement; |
|---|
| 29 | |
|---|
| 30 | import org.apache.log4j.Logger; |
|---|
| 31 | import org.docx4j.XmlUtils; |
|---|
| 32 | import org.docx4j.openpackaging.contenttype.ContentTypeManager; |
|---|
| 33 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 34 | import org.docx4j.openpackaging.packages.OpcPackage; |
|---|
| 35 | import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart; |
|---|
| 36 | import org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart; |
|---|
| 37 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 38 | import org.docx4j.openpackaging.parts.JaxbXmlPart; |
|---|
| 39 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 40 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; |
|---|
| 41 | import org.docx4j.openpackaging.parts.opendope.JaxbCustomXmlDataStoragePart; |
|---|
| 42 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 43 | import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; |
|---|
| 44 | import org.docx4j.relationships.Relationship; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | public class CustomXmlInfo extends AbstractSample { |
|---|
| 48 | |
|---|
| 49 | private static Logger log = Logger.getLogger(CustomXmlInfo.class); |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * @param args |
|---|
| 53 | */ |
|---|
| 54 | public static void main(String[] args) throws Exception { |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | try { |
|---|
| 59 | getInputFilePath(args); |
|---|
| 60 | } catch (IllegalArgumentException e) { |
|---|
| 61 | inputfilepath = System.getProperty("user.dir") + "/sample-docs/databinding/invoice.docx"; |
|---|
| 62 | // inputfilepath = System.getProperty("user.dir") + "/sample-docs/databinding/CountryRegions.xml"; |
|---|
| 63 | |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | // Open a document from the file system |
|---|
| 68 | // 1. Load the Package - .docx or Flat OPC .xml |
|---|
| 69 | org.docx4j.openpackaging.packages.OpcPackage opcPackage = org.docx4j.openpackaging.packages.OpcPackage.load(new java.io.File(inputfilepath)); |
|---|
| 70 | |
|---|
| 71 | // List the parts by walking the rels tree |
|---|
| 72 | RelationshipsPart rp = opcPackage.getRelationshipsPart(); |
|---|
| 73 | StringBuilder sb = new StringBuilder(); |
|---|
| 74 | traverseRelationships(opcPackage, rp, sb, " "); |
|---|
| 75 | |
|---|
| 76 | approach2(opcPackage, sb); |
|---|
| 77 | |
|---|
| 78 | System.out.println(sb.toString()); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | public static void printInfo(Part p, StringBuilder sb, String indent) throws Docx4JException { |
|---|
| 83 | |
|---|
| 84 | String relationshipType = ""; |
|---|
| 85 | if (p.getSourceRelationships().size()>0 ) { |
|---|
| 86 | relationshipType = p.getSourceRelationships().get(0).getType(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | // sb.append("\n" + indent + "Part " + p.getPartName() + " [" + p.getClass().getName() + "] " + relationshipType ); |
|---|
| 90 | |
|---|
| 91 | if (p instanceof CustomXmlDataStoragePart) { |
|---|
| 92 | sb.append("\n" + indent + p.getClass().getName() + ": " + indent + p.getPartName().getName() ); |
|---|
| 93 | sb.append("\n" + indent + "root element: " + ((CustomXmlDataStoragePart)p).getData().getDocument().getDocumentElement().getLocalName() ); |
|---|
| 94 | } else if ( p instanceof JaxbCustomXmlDataStoragePart) { |
|---|
| 95 | sb.append("\n" + indent + p.getClass().getName() + ": " + indent + p.getPartName().getName() ); |
|---|
| 96 | } else if (p instanceof CustomXmlDataStoragePropertiesPart) { |
|---|
| 97 | CustomXmlDataStoragePropertiesPart pp = (CustomXmlDataStoragePropertiesPart)p; |
|---|
| 98 | sb.append("\n" + indent + p.getPartName().getName() |
|---|
| 99 | + " item id: " + pp.getItemId() ); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * This HashMap is intended to prevent loops. |
|---|
| 105 | */ |
|---|
| 106 | public static HashMap<Part, Part> handled = new HashMap<Part, Part>(); |
|---|
| 107 | |
|---|
| 108 | public static void traverseRelationships(org.docx4j.openpackaging.packages.OpcPackage wordMLPackage, |
|---|
| 109 | RelationshipsPart rp, |
|---|
| 110 | StringBuilder sb, String indent) throws Docx4JException { |
|---|
| 111 | |
|---|
| 112 | for ( Relationship r : rp.getRelationships().getRelationship() ) { |
|---|
| 113 | |
|---|
| 114 | if (r.getType().contains("customXml")) { |
|---|
| 115 | sb.append("\n" + indent + "For Relationship Id=" + r.getId() |
|---|
| 116 | + " Source is " + rp.getSourceP().getPartName() |
|---|
| 117 | + ", Target is " + r.getTarget() |
|---|
| 118 | + " of type " + r.getType() ); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | if (r.getTargetMode() != null |
|---|
| 122 | && r.getTargetMode().equals("External") ) { |
|---|
| 123 | continue; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | Part part = rp.getPart(r); |
|---|
| 127 | |
|---|
| 128 | printInfo(part, sb, indent); |
|---|
| 129 | if (handled.get(part)!=null) { |
|---|
| 130 | sb.append(" [additional reference] "); |
|---|
| 131 | continue; |
|---|
| 132 | } |
|---|
| 133 | handled.put(part, part); |
|---|
| 134 | if (part.getRelationshipsPart()==null) { |
|---|
| 135 | // sb.append(".. no rels" ); |
|---|
| 136 | } else { |
|---|
| 137 | traverseRelationships(wordMLPackage, part.getRelationshipsPart(), sb, indent + " "); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | public static void approach2(OpcPackage pkg, StringBuilder sb) { |
|---|
| 146 | |
|---|
| 147 | sb.append("\n\n Approach 2:"); |
|---|
| 148 | |
|---|
| 149 | HashMap<PartName, Part> parts = pkg.getParts().getParts(); |
|---|
| 150 | HashMap<String, Object> tmp = new HashMap<String, Object> (); |
|---|
| 151 | |
|---|
| 152 | Collection col = parts.values(); |
|---|
| 153 | Iterator iterator = col.iterator(); |
|---|
| 154 | while( iterator.hasNext() ) { |
|---|
| 155 | Part entry = (Part)iterator.next(); |
|---|
| 156 | |
|---|
| 157 | if (entry instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePart |
|---|
| 158 | || entry instanceof JaxbCustomXmlDataStoragePart) { |
|---|
| 159 | sb.append("\nFound a CustomXmlDataStoragePart, named " + entry.getPartName().getName() ); |
|---|
| 160 | if (entry.getRelationshipsPart()==null) { continue; } |
|---|
| 161 | sb.append("\n.. it has a rels part"); |
|---|
| 162 | // Look in its rels for rel of @Type customXmlProps (eg @Target="itemProps1.xml") |
|---|
| 163 | Relationship r = entry.getRelationshipsPart().getRelationshipByType( |
|---|
| 164 | Namespaces.CUSTOM_XML_DATA_STORAGE_PROPERTIES); |
|---|
| 165 | if (r==null) { |
|---|
| 166 | sb.append("\n.. but that doesn't point to a customXmlProps part"); |
|---|
| 167 | continue; |
|---|
| 168 | } |
|---|
| 169 | CustomXmlDataStoragePropertiesPart customXmlProps = |
|---|
| 170 | (CustomXmlDataStoragePropertiesPart)entry.getRelationshipsPart().getPart(r); |
|---|
| 171 | if (customXmlProps==null) { |
|---|
| 172 | sb.append("\n.. but the target seems to be missing?"); |
|---|
| 173 | continue; |
|---|
| 174 | } |
|---|
| 175 | String itemId = customXmlProps.getItemId().toLowerCase(); |
|---|
| 176 | sb.append("\nIdentified/registered ds:itemId " + itemId); |
|---|
| 177 | if (tmp.get(itemId.toLowerCase())!=null) { |
|---|
| 178 | sb.append("\nDuplicate CustomXML itemId " + itemId + "; check your source docx!"); |
|---|
| 179 | } |
|---|
| 180 | tmp.put(itemId, entry ); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | } |
|---|