feiyu02
2024-08-13 b8cc591541b88dd2bb93f111f8e8075842dce7ca
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
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.model.fields.ComplexFieldLocator;
import org.docx4j.model.fields.FieldRef;
import org.docx4j.model.fields.FieldsPreprocessor;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.P;
import org.docx4j.wml.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
/**
 * List all field instructions found in docx (main document part,
 * headers, footers).
 * 
 * Shortcoming: Doesn't handle fields which cross paragraph boundaries eg TOC field,
 * 
 *
 * @author jharrop
 *
 */
public class FieldsDiagnostics {
 
    private static Logger log = LoggerFactory.getLogger(FieldsDiagnostics.class);
 
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        
        // A docx or a dir containing docx files
        String inputpath = System.getProperty("user.dir") + "/dd/";
 
        StringBuilder sb = new StringBuilder(); 
        
        File dir = new File(inputpath);
        
        if (dir.isDirectory()) {
            
            String[] files = dir.list();
            
            for (int i = 0; i<files.length; i++  ) {
                
                if (files[i].endsWith("docx")) {
                    sb.append("\n\n" + files[i] + "\n");
                    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(inputpath + "/" + files[i]));
                    pkgReport(wordMLPackage, sb);
                }
            }
            
        } else if (inputpath.endsWith("docx")) {
            sb.append("\n\n" + inputpath + "\n");
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(inputpath ));
            pkgReport(wordMLPackage, sb);            
        }
        
        System.out.println(sb.toString());
        
    }
    
    private static void pkgReport(WordprocessingMLPackage wordMLPackage, StringBuilder sb) throws Docx4JException {
 
 
        
        FieldsPreprocessor.complexifyFields(wordMLPackage.getMainDocumentPart() );
 
        System.out.println(wordMLPackage.getMainDocumentPart().getXML() );
        
//        System.out.println("\n" + wordMLPackage.getMainDocumentPart().getPartName() + "\n");
        listFieldsInPart(wordMLPackage.getMainDocumentPart().getPartName().getName(), 
                wordMLPackage.getMainDocumentPart().getContent(), sb );
 
//        { // Headers, footers
//
//            RelationshipsPart rp = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
//            for ( Relationship r : rp.getJaxbElement().getRelationship()  ) {
//
//                if (r.getType().equals(Namespaces.HEADER)
//                        || r.getType().equals(Namespaces.FOOTER)) {
//
//                    JaxbXmlPart part = (JaxbXmlPart)rp.getPart(r);
//
//                    FieldsPreprocessor.complexifyFields(part );
//
//                    System.out.println("\n" + part.getPartName() + "\n");
//                    listFieldsInPart(part.getPartName().getName(),
//                            ((ContentAccessor)part).getContent(), sb );
//
//                }
//            }
//
//
//        }
        
 
    }
 
    private static void listFieldsInPart(String partName, List<Object> contentList, StringBuilder sb) throws Docx4JException {
 
        // find fields
        ComplexFieldLocator fl = new ComplexFieldLocator();
        new TraversalUtil(contentList, fl);
        
        if (fl.getStarts().size()==0) {
            sb.append("\n\n" + partName + ": no fields detected.");
            return;
        }
 
        sb.append("\n\n" + partName + ": " + fl.getStarts().size() + " paragraphs containing the following fields: ");
 
        // canonicalise and setup fieldRefs
        List<FieldRef> fieldRefs = new ArrayList<FieldRef>();
        for( P p : fl.getStarts() ) {
            FieldsPreprocessor.canonicalise(p, fieldRefs);
        }
 
        // Populate
        for (FieldRef fr : fieldRefs) {
            recurse(sb, fr, "    ");
        }
 
    }
    
    private static void recurse(StringBuilder sb, FieldRef fr, String indent) {
        
        for(Object o : fr.getInstructions() ) {
            if (o instanceof FieldRef) {
                recurse(sb, ((FieldRef)o), indent + "    ");
            } else {
                o = XmlUtils.unwrap(o);
                if (o instanceof Text) {
                    String instr = ((Text)o).getValue();
                    sb.append("\n" + indent +  instr);
//                    if (instr.contains("MERGE")) {
//                        sb.append(" --> " + MailMerger.getDatafieldNameFromInstr(instr));
//                    }
                } else {
                    sb.append("\n" + indent + XmlUtils.unwrap(o).getClass().getName() );
                }
            }
        }
        
    }
 
 
}