feiyu02
2024-09-25 0516cba27e632f20efac2752787f38f0c87baafa
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
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.RangeFinder;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.CTBookmark;
import org.docx4j.wml.CTMarkupRange;
import org.docx4j.wml.ContentAccessor;
 
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
 
/**
 * Perform certain bookmark integrity checks, and optionally, write a fixed output docx 
 */
public class BookmarksDuplicateCheck {
        
//    protected static Logger log = LoggerFactory.getLogger(BookmarksDuplicateCheck.class);
    
    /**
     * Whether to attempt 
     */
    private static boolean remediate = true;
    
    private static org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
    
 
    public static void main(String[] args) throws Exception {
        
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                .load(new java.io.File(System.getProperty("user.dir")
                        + "/your.docx"));
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
        
        // Before..
        // System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
 
        org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
                .getJaxbElement();
        Body body = wmlDocumentEl.getBody();
        
        BookmarksDuplicateCheck bti = new BookmarksDuplicateCheck();
 
        List<Object> faulty = bti.inspectBookmarks(body.getContent());
        
        if (remediate) {
            
            for (Object o : faulty) {
                
                if (o instanceof CTBookmark) {
                    CTBookmark start = (CTBookmark)o;
                    Object parent = start.getParent();
                    if (parent instanceof ContentAccessor) {
                        if (remove( ((ContentAccessor)parent).getContent(), o)) {
                            
                        } else {
                            System.out.println("Couldn't find start " + start.getName() );
                        }
                    } else {
                        System.out.println("TODO: handle parent:" + parent.getClass().getName());
                    }
                }
 
                if (o instanceof CTMarkupRange /* ends */
                        && (!(o instanceof CTBookmark) /* exclude starts - note inheritance hierarchy */ )) {
                    CTMarkupRange end = (CTMarkupRange)o;
                    Object parent = end.getParent();
                    if (parent instanceof ContentAccessor) {
                        if (remove( ((ContentAccessor)parent).getContent(), o)) {
                            
                        } else {
                            System.out.println("Couldn't find end " + end.getId().longValue() );
                        }
                    } else {
                        System.out.println("TODO: handle parent:" + parent.getClass().getName());
                    }
                }
                
            }
            
            if (faulty.size()==0) {
                System.out.println("Nothing to fix");
            } else {
                // System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
                wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_BookmarksRemediated.docx"));
                
            }
            
        }
        
    }
    
    private static boolean remove(List list, Object deletion) {
        
        int i = getIndex(list, deletion);
        if (i>=0) {
            Object o = list.remove(i);
            return (o!=null);
        }
        return false;
    }
    
    private static int getIndex(List list, Object deletion) {
 
        int i = 0;
        for (Object o : list) {
            
            if (o==deletion
                    || XmlUtils.unwrap(o)==deletion) {
                return i;
            }    
            i++;
        }
        return -1;
    }
 
    private  List<Object> inspectBookmarks(List<Object> paragraphs) throws Exception {
        
        Set<String> names = new HashSet<String>();
        Set<BigInteger> startIds = new HashSet<BigInteger>();
        Set<BigInteger> endIds = new HashSet<BigInteger>();
        
        List<Object> faulty = new ArrayList<Object>(); 
 
        RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
        new TraversalUtil(paragraphs, rt);
 
        System.out.println("Checking starts " );
        
        for (CTBookmark bm : rt.getStarts()) {
            
            BigInteger id = bm.getId();
            String name = bm.getName();
            
            if (name==null && id == null) {
                System.out.println("Name and ID missing!");
                faulty.add(bm);
                
            } else if (name!=null && id != null) {
                
                if (!names.add(name)) {
                    System.out.println("Already have " + name);
                    faulty.add(bm);
                }
                if (!startIds.add(id)) {
                    System.out.println("Already have " + id.longValue());
                    faulty.add(bm);
                }
                
            } else if (name==null)  {
                System.out.println("Name missing for id " + id.longValue());
                if (!startIds.add(id)) {
                    System.out.println(".. and already have " + id.longValue());
                    faulty.add(bm);
                }
                
            } else if (id==null)  {
                System.out.println("ID missing for name " + name);
                if (!names.add(name)) {
                    System.out.println(".. and already have " + name);
                    faulty.add(bm);
                }
                
            }            
        }
        
        System.out.println("Checking ends " );
        
        for (CTMarkupRange bm : rt.getEnds()) {
            
            BigInteger id = bm.getId();
            
            if (id == null) {
                System.out.println("ID missing!");
                faulty.add(bm);
                
            } else if (id != null) {
                
                if (!endIds.add(id)) {
                    System.out.println("Already have " + id.longValue());
                    faulty.add(bm);
                }
                
            }            
        }
        
 
        System.out.println("Matching ends" );
        for (BigInteger i : startIds) {
            
            if (!endIds.contains(i)) {
                System.out.println("  Missing end for start " + i.longValue());    
                faulty.add(find(rt.getStarts(), i)); // so remove the corresponding start
            }
        }
        
        System.out.println("Matching starts" );
        for (BigInteger i : endIds) {
            
            if (!startIds.contains(i)) {
                System.out.println("  Missing start for end " + i.longValue());                
                faulty.add(find1(rt.getEnds(), i)); // so remove the corresponding end
            }
        }
        
        System.out.println("Total faulty objects: " + faulty.size());                
        
        return faulty;
        
    }
    
    private CTBookmark find(List<CTBookmark> starts, BigInteger id) {
        
        for (CTBookmark bm : starts) {
            if (bm.getId()==id) {
                return bm;
            }
        }
        return null; //shouldn't happen
    }
 
    private CTMarkupRange find1(List<CTMarkupRange> ends, BigInteger id) {
        
        for (CTMarkupRange bm : ends) {
            if (bm.getId()==id) {
                return bm;
            }
        }
        return null; //shouldn't happen
    }
    
}