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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 *  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 com.topologi.diffx.Docx4jDriver;
import org.docx4j.Docx4J;
import org.docx4j.XmlUtils;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.diff.Differencer;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart.AddPartBehaviour;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Body;
import org.docx4j.wml.Document;
 
import javax.xml.bind.JAXBContext;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
 
 
/**
 * This sample compares the 2 input documents, and renders
 * the result using PDF viewer.
 *
 */
public class CompareDocuments {
    
    public static JAXBContext context = org.docx4j.jaxb.Context.jc; 
    
    static boolean DOCX_SAVE = true;
 
    static boolean PDF_SAVE = false;
    
    
    /**
     * Split up the problem to try to solve more quickly
     * (you might try this when you have 500 entries or more)
     */
    static boolean DIVIDE_AND_CONQUER = false;
    
    
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
 
        String newerfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docxv2.docx";
        String olderfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.docx";
        
        // 1. Load the Packages
        WordprocessingMLPackage newerPackage = WordprocessingMLPackage.load(new File(newerfilepath));
        WordprocessingMLPackage olderPackage = WordprocessingMLPackage.load(new File(olderfilepath));
        
        Body newerBody = ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).getBody();
        Body olderBody = ((Document)olderPackage.getMainDocumentPart().getJaxbElement()).getBody();
        
        
        // 2. Do the differencing
        java.io.StringWriter sw = new java.io.StringWriter();
        javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(
                sw);
        Calendar changeDate = null;
        
        Differencer pd = null;
        if (DIVIDE_AND_CONQUER) {
 
            System.out.println("Differencing with DIVIDE_AND_CONQUER..");
            
            java.io.StringWriter swInterim = new java.io.StringWriter();
            
            Docx4jDriver.diff( XmlUtils.marshaltoW3CDomDocument(newerBody).getDocumentElement(),
                    XmlUtils.marshaltoW3CDomDocument(olderBody).getDocumentElement(),
                    swInterim);
                // The signature which takes Reader objects appears to be broken
            
            // Now, feed it through diff to wml XSLT
            pd = new Differencer();
            pd.toWML(swInterim.toString(), result, "someone", changeDate,
                    newerPackage.getMainDocumentPart().getRelationshipsPart(),
                    olderPackage.getMainDocumentPart().getRelationshipsPart() 
                    );
            
        } else {
 
            System.out.println("Differencing without dividing..");
            
            pd = new Differencer();
            pd.setRelsDiffIdentifier("blagh"); // not necessary in this case 
            pd.diff(newerBody, olderBody, result, "someone", changeDate,
                    newerPackage.getMainDocumentPart().getRelationshipsPart(),
                    olderPackage.getMainDocumentPart().getRelationshipsPart() 
                    );
        }
        
        // 3. Get the result
        String contentStr = sw.toString();
        System.out.println("Result: \n\n " + contentStr);
        Body newBody = (Body) XmlUtils.unwrap(
                XmlUtils.unmarshalString(contentStr));
        
        // 4. Display the result as a PDF
        // To do this, we'll replace the body in the newer document
        ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).setBody(newBody);
 
        if (DIVIDE_AND_CONQUER) {
            // No image support at present
        } else {
            handleRels(pd, newerPackage.getMainDocumentPart()); // TODO: that needs work, for more complex input
        }
        
        
        if (DOCX_SAVE) {
            newerPackage.save(new File(System.getProperty("user.dir") +"/OUT_CompareDocuments.docx"));
        }
        
        if (PDF_SAVE) {
            
            newerPackage.setFontMapper(new IdentityPlusMapper());    
            
            boolean saveFO = false;
            String outputfilepath = System.getProperty("user.dir") +  "/OUT_CompareDocuments.pdf";
            
            // FO exporter setup (required)
            // .. the FOSettings object
            FOSettings foSettings = Docx4J.createFOSettings();
            if (saveFO) {
                foSettings.setFoDumpFile(new File(System.getProperty("user.dir") +  "/OUT_CompareDocuments..fo"));
            }
            foSettings.setWmlPackage(newerPackage);
            // Document format: 
            // The default implementation of the FORenderer that uses Apache Fop will output
            // a PDF document if nothing is passed via 
            // foSettings.setApacheFopMime(apacheFopMime)
            // apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants or
            // FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
            
            
            // exporter writes to an OutputStream.        
            OutputStream os = new java.io.FileOutputStream(outputfilepath);
            
    
            //Don't care what type of exporter you use
            Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
            
            System.out.println("Saved " + System.getProperty("user.dir")  +  "/OUT_CompareDocuments.pdf");
        }
                
    }
 
    /**
         In the general case, you need to handle relationships.
         Although not necessary in this simple example, 
         we do it anyway for the purposes of illustration.
     * @throws InvalidFormatException 
         
     */
    private static void handleRels(Differencer pd, MainDocumentPart newMDP) throws InvalidFormatException {
        
        RelationshipsPart rp = newMDP.getRelationshipsPart(); 
        System.out.println("before: \n" + rp.getXML());        
        
        // Since we are going to add rels appropriate to the docs being 
        // compared, for neatness and to avoid duplication
        // (duplication of internal part names is fatal in Word,
        //  and export xslt makes images internal, though it does avoid duplicating
        //  a part ), 
        // remove any existing rels which point to images
        List<Relationship> relsToRemove = new ArrayList<Relationship>();
        for (Relationship r : rp.getRelationships().getRelationship() ) {
            //  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
            if (r.getType().equals(Namespaces.IMAGE)) {
                relsToRemove.add(r);
            }
        }                        
        for (Relationship r : relsToRemove) {                
            rp.removeRelationship(r);
        }
        
        // Now add the rels we composed
        Map<Relationship, Part> newRels = pd.getComposedRels();
        for (Relationship nr : newRels.keySet()) {    
            
            if (nr.getTargetMode()!=null 
                    && nr.getTargetMode().equals("External")) {
                
                newMDP.getRelationshipsPart().getRelationships().getRelationship().add(nr);
                
            } else {
                
                Part part = newRels.get(nr);
                if (part==null) {
                    System.out.println("ERROR! Couldn't find part for rel " + nr.getId() + "  " + nr.getTargetMode() );
                } else {
                    
                    if (part instanceof BinaryPart) { // ensure contents are loaded, before moving to new pkg
                        ((BinaryPart)part).getBuffer();
                    }
                    
                    newMDP.addTargetPart(part, AddPartBehaviour.RENAME_IF_NAME_EXISTS, nr.getId());
                }
            }
        }
        
        System.out.println("after: \n" + rp.getXML());
        
    }
    
    
        
 
}