| 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.JAXBElement; |
|---|
| 27 | |
|---|
| 28 | import org.apache.log4j.Logger; |
|---|
| 29 | import org.docx4j.XmlUtils; |
|---|
| 30 | import org.docx4j.openpackaging.contenttype.ContentTypeManager; |
|---|
| 31 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 32 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 33 | import org.docx4j.openpackaging.parts.JaxbXmlPart; |
|---|
| 34 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; |
|---|
| 35 | import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; |
|---|
| 36 | import org.docx4j.relationships.Relationship; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | public class PartsList extends AbstractSample { |
|---|
| 40 | |
|---|
| 41 | private static Logger log = Logger.getLogger(PartsList.class); |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * @param args |
|---|
| 45 | */ |
|---|
| 46 | public static void main(String[] args) throws Exception { |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | try { |
|---|
| 51 | getInputFilePath(args); |
|---|
| 52 | } catch (IllegalArgumentException e) { |
|---|
| 53 | // inputfilepath = System.getProperty("user.dir") |
|---|
| 54 | // + "/sample-docs/test-docs/header-footer/header_sections_some-linked.xml"; |
|---|
| 55 | // inputfilepath = System.getProperty("user.dir") + "/sample-docs/xlsx/pivot.xlsm"; |
|---|
| 56 | inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.xml"; |
|---|
| 57 | // inputfilepath = System.getProperty("user.dir") + "/sample-docs/pptx/lines.pptx"; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | // Open a document from the file system |
|---|
| 62 | // 1. Load the Package - .docx or Flat OPC .xml |
|---|
| 63 | org.docx4j.openpackaging.packages.OpcPackage opcPackage = org.docx4j.openpackaging.packages.OpcPackage.load(new java.io.File(inputfilepath)); |
|---|
| 64 | |
|---|
| 65 | //printContentTypes(opcPackage); |
|---|
| 66 | |
|---|
| 67 | // List the parts by walking the rels tree |
|---|
| 68 | RelationshipsPart rp = opcPackage.getRelationshipsPart(); |
|---|
| 69 | StringBuilder sb = new StringBuilder(); |
|---|
| 70 | printInfo(rp, sb, ""); |
|---|
| 71 | traverseRelationships(opcPackage, rp, sb, " "); |
|---|
| 72 | |
|---|
| 73 | System.out.println(sb.toString()); |
|---|
| 74 | |
|---|
| 75 | // SaveToZipFile saver = new SaveToZipFile(opcPackage); |
|---|
| 76 | // saver.save(System.getProperty("user.dir") + "/out.docx"); |
|---|
| 77 | |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public static void printContentTypes(org.docx4j.openpackaging.packages.OpcPackage p) { |
|---|
| 81 | |
|---|
| 82 | ContentTypeManager ctm = p.getContentTypeManager(); |
|---|
| 83 | |
|---|
| 84 | ctm.listTypes(); |
|---|
| 85 | |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | public static void printInfo(Part p, StringBuilder sb, String indent) { |
|---|
| 89 | |
|---|
| 90 | String relationshipType = ""; |
|---|
| 91 | if (p.getSourceRelationships().size()>0 ) { |
|---|
| 92 | relationshipType = p.getSourceRelationships().get(0).getType(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | sb.append("\n" + indent + "Part " + p.getPartName() + " [" + p.getClass().getName() + "] " + relationshipType ); |
|---|
| 96 | |
|---|
| 97 | // System.out.println("//" + p.getPartName() ); |
|---|
| 98 | // System.out.println("public final static String XX ="); |
|---|
| 99 | // System.out.println("\"" + relationshipType + "\";"); |
|---|
| 100 | |
|---|
| 101 | if (p instanceof JaxbXmlPart) { |
|---|
| 102 | Object o = ((JaxbXmlPart)p).getJaxbElement(); |
|---|
| 103 | if (o instanceof javax.xml.bind.JAXBElement) { |
|---|
| 104 | sb.append(" containing JaxbElement:" + XmlUtils.JAXBElementDebug((JAXBElement)o) ); |
|---|
| 105 | } else { |
|---|
| 106 | sb.append(" containing JaxbElement:" + o.getClass().getName() ); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * This HashMap is intended to prevent loops. |
|---|
| 113 | */ |
|---|
| 114 | public static HashMap<Part, Part> handled = new HashMap<Part, Part>(); |
|---|
| 115 | |
|---|
| 116 | public static void traverseRelationships(org.docx4j.openpackaging.packages.OpcPackage wordMLPackage, |
|---|
| 117 | RelationshipsPart rp, |
|---|
| 118 | StringBuilder sb, String indent) { |
|---|
| 119 | |
|---|
| 120 | // TODO: order by rel id |
|---|
| 121 | |
|---|
| 122 | // if (rp.getRelationships().getRelationship().size()==0) { |
|---|
| 123 | // System.out.println("In rels part .. empty"); |
|---|
| 124 | // } |
|---|
| 125 | |
|---|
| 126 | for ( Relationship r : rp.getRelationships().getRelationship() ) { |
|---|
| 127 | |
|---|
| 128 | log.info("\nFor Relationship Id=" + r.getId() |
|---|
| 129 | + " Source is " + rp.getSourceP().getPartName() |
|---|
| 130 | + ", Target is " + r.getTarget() |
|---|
| 131 | + " type " + r.getType() + "\n"); |
|---|
| 132 | |
|---|
| 133 | if (r.getTargetMode() != null |
|---|
| 134 | && r.getTargetMode().equals("External") ) { |
|---|
| 135 | |
|---|
| 136 | sb.append("\n" + indent + "external resource " + r.getTarget() |
|---|
| 137 | + " of type " + r.getType() ); |
|---|
| 138 | continue; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | Part part = rp.getPart(r); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | printInfo(part, sb, indent); |
|---|
| 145 | if (handled.get(part)!=null) { |
|---|
| 146 | sb.append(" [additional reference] "); |
|---|
| 147 | continue; |
|---|
| 148 | } |
|---|
| 149 | handled.put(part, part); |
|---|
| 150 | if (part.getRelationshipsPart(false)==null) { |
|---|
| 151 | // sb.append(".. no rels" ); |
|---|
| 152 | } else { |
|---|
| 153 | traverseRelationships(wordMLPackage, part.getRelationshipsPart(false), sb, indent + " "); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | } |
|---|