feiyu02
2022-11-15 23bd719cebe5feeff4e48fde925b0b39755eea93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  Copyright 2007-2008, Plutext Pty Ltd.
 *   
 *  This file is part of docx4j.
 
    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
 
    You may obtain a copy of the License at 
 
        http://www.apache.org/licenses/LICENSE-2.0 
 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.
 
 */
 
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
 
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.OpcPackage;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.opendope.JaxbCustomXmlDataStoragePart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.samples.AbstractSample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
 
 
/**
 * This sample is similar to PartsList, but
 * it provides info specific to your CustomXML parts.
 * 
 * @author jharrop
 *
 */
public class ContentControlsInfoParts extends AbstractSample {
    
    private static Logger log = LoggerFactory.getLogger(ContentControlsInfoParts.class);
 
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        
 
        
        try {
            getInputFilePath(args);
        } catch (IllegalArgumentException e) {
//            inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice.docx";
//            inputfilepath = System.getProperty("user.dir") + "/sample-docs/databinding/CountryRegions.xml";
            inputfilepath = System.getProperty("user.dir") + "/OUT_VariableReplace.docx";
 
        }
        
            
        // Open a document from the file system
        // 1. Load the Package - .docx or Flat OPC .xml
        OpcPackage opcPackage = OpcPackage.load(new java.io.File(inputfilepath));
        
        // List the parts by walking the rels tree
        RelationshipsPart rp = opcPackage.getRelationshipsPart();
        StringBuilder sb = new StringBuilder();
        traverseRelationships(opcPackage, rp, sb, "    ");
        
        approach2(opcPackage, sb);
        
        System.out.println(sb.toString());
    }
    
    
    public static void  printInfo(Part p, StringBuilder sb, String indent) throws Docx4JException {
        
        String relationshipType = "";
        if (p.getSourceRelationships().size()>0 ) {
            relationshipType = p.getSourceRelationships().get(0).getType();
        }
        
//        sb.append("\n" + indent + "Part " + p.getPartName() + " [" + p.getClass().getName() + "] " + relationshipType );
                
        if (p instanceof CustomXmlDataStoragePart)  {
            sb.append("\n" + indent + p.getClass().getName() + ": " + indent + p.getPartName().getName()  );    
            sb.append("\n" + indent + "root element: " + ((CustomXmlDataStoragePart)p).getData().getDocument().getDocumentElement().getLocalName() );
        } else if ( p instanceof JaxbCustomXmlDataStoragePart) {
                sb.append("\n" + indent + p.getClass().getName() + ": " + indent + p.getPartName().getName()  );
        } else if (p instanceof CustomXmlDataStoragePropertiesPart) {    
            CustomXmlDataStoragePropertiesPart pp = (CustomXmlDataStoragePropertiesPart)p;
            sb.append("\n" + indent +  p.getPartName().getName() 
                    +  "  item id: " + pp.getItemId()   );
        }
    }
    
    /**
     * This HashMap is intended to prevent loops.
     */
    public static HashMap<Part, Part> handled = new HashMap<Part, Part>();
    
    public static void traverseRelationships(OpcPackage wordMLPackage,
            RelationshipsPart rp, 
            StringBuilder sb, String indent) throws Docx4JException {
        
        for ( Relationship r : rp.getRelationships().getRelationship() ) {
            
            if (r.getType().contains("customXml")) {
            sb.append("\n" + indent + "For Relationship Id=" + r.getId() 
                    + " Source is " + rp.getSourceP().getPartName() 
                    + ", Target is " + r.getTarget() 
                    + " of type " + r.getType() );
            }
            
            if (r.getTargetMode() != null
                    && r.getTargetMode().equals("External") ) {                
                continue;                
            }
            
            Part part = rp.getPart(r);
                                    
            printInfo(part, sb, indent);
            if (handled.get(part)!=null) {
                sb.append(" [additional reference] ");
                continue;
            }
            handled.put(part, part);
            if (part.getRelationshipsPart()==null) {
                // sb.append(".. no rels" );                        
            } else {
                traverseRelationships(wordMLPackage, part.getRelationshipsPart(), sb, indent + "    ");
            }
                    
        }
        
        
    }
    
    public static void approach2(OpcPackage pkg, StringBuilder sb) {
        
        sb.append("\n\n Approach 2:");
        
        HashMap<PartName, Part> parts = pkg.getParts().getParts();
        HashMap<String, Object> tmp = new HashMap<String, Object> ();
        
        Collection col = parts.values();
        Iterator iterator = col.iterator();
        while( iterator.hasNext() ) {
            Part entry = (Part)iterator.next();
            
            if (entry instanceof CustomXmlDataStoragePart
                    || entry instanceof JaxbCustomXmlDataStoragePart) {
                sb.append("\nFound a CustomXmlDataStoragePart, named " + entry.getPartName().getName() );
                if (entry.getRelationshipsPart()==null) { continue; }
                sb.append("\n.. it has a rels part");
                // Look in its rels for rel of @Type customXmlProps (eg @Target="itemProps1.xml")
                Relationship r = entry.getRelationshipsPart().getRelationshipByType(
                        Namespaces.CUSTOM_XML_DATA_STORAGE_PROPERTIES);
                if (r==null) {
                    sb.append("\n.. but that doesn't point to a  customXmlProps part");
                    continue;
                }
                CustomXmlDataStoragePropertiesPart customXmlProps = 
                    (CustomXmlDataStoragePropertiesPart)entry.getRelationshipsPart().getPart(r);
                if (customXmlProps==null) {
                    sb.append("\n.. but the target seems to be missing?");
                    continue;
                }
                String itemId = customXmlProps.getItemId().toLowerCase();
                sb.append("\nIdentified/registered ds:itemId " + itemId);
                if (tmp.get(itemId.toLowerCase())!=null) {
                    sb.append("\nDuplicate CustomXML itemId " + itemId + "; check your source docx!");
                }
                tmp.put(itemId, entry );
            }
        }            
        
        
        
        
    }
    
}