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
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
 
import org.docx4j.Docx4J;
import org.docx4j.anon.Anonymize;
import org.docx4j.anon.AnonymizeResult;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
 
public class AnonSingle {
 
 
    public static void main(String[] args) throws Docx4JException {
 
        String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docxv2.docx";
 
        String outputfilepath = System.getProperty("user.dir")
                + "/OUT_Anon.docx";
 
        WordprocessingMLPackage pkg = Docx4J.load(new java.io.File(
                inputfilepath));
 
        Anonymize anon = new Anonymize(pkg);
        AnonymizeResult result = anon.go();
        
        Docx4J.save(pkg, new java.io.File(outputfilepath));
 
        // Report
        System.out.println("\n\n REPORT for " + inputfilepath + "\n\n");
        if (result.isOK()) {
            System.out.println("document successfully anonymised.");
        } else {
            System.out.println("document partially anonymised; please check " + outputfilepath);
            
            if (result.getUnsafeParts().size()>0) {
                System.out.println("The following parts may leak info:");
                for(Part p :  result.getUnsafeParts()) {
                    System.out.println(p.getPartName().getName() + ", of type " + p.getClass().getName() );
                }
            }
            
            // unsafe objects
            System.out.println(result.reportUnsafeObjects());
        }
        
        System.out.println("\n\n .. end REPORT for " + inputfilepath  + "\n\n");
        
 
    }
 
}