forked from binfalse/XMLUtils
/
TreeNode.java
419 lines (338 loc) · 8.32 KB
/
TreeNode.java
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
*
*/
package de.unirostock.sems.xmlutils.ds;
import java.util.HashMap;
import java.util.List;
import org.jdom2.Element;
import de.unirostock.sems.xmlutils.comparison.Connection;
import de.unirostock.sems.xmlutils.comparison.ConnectionManager;
/**
* The abstract class TreeNode representing a node in a document tree.
*
* @author Martin Scharm
*/
public abstract class TreeNode
{
/** The node tag name for text nodes, as we will use it in XPath expressions. */
public static final String TEXT_TAG = "text()";
/** UNCHANGED => node hasn't changes. */
public static final int UNCHANGED = 0;
/** UNMAPPED => node wasn't mapped. */
public static final int UNMAPPED = 1;
/** MOVED => node has moves. */
public static final int MOVED = 2;
/** MODIFIED => node was modified. */
public static final int MODIFIED = 4;
/** SUB_MODIFIED => the corresponding subtree was modified. */
public static final int SUB_MODIFIED = 8;
/** COPIED => node was copied. */
public static final int COPIED = 16;
/** GLUED => node was glued. */
public static final int GLUED = 32;
/** KIDSSWAPPED => the sequence of kids of this node were altered. */
public static final int KIDSSWAPPED = 64;
/** SWAPPEDKID => this is a swapped kid. */
public static final int SWAPPEDKID = 128;
/** SUBTREEUNMAPPED => the whole subtree is unmapped. */
public static final int SUBTREEUNMAPPED = 256;
/**
* DOC_NODE this is a DocumentNode.
*
* @see de.unirostock.sems.xmlutils.ds.DocumentNode
*/
public static final int DOC_NODE = 1;
/**
* TEXT_NODE this is a TextNode.
*
* @see de.unirostock.sems.xmlutils.ds.TextNode
*/
public static final int TEXT_NODE = 2;
/** The current modification state. */
protected int modified;
/** The node type. */
protected int type;
/** The XPath of this node. */
protected String xPath;
/** The parent node. */
protected DocumentNode parent;
/** The corresponding document. */
protected TreeDocument doc;
/** The level in the tree document. */
protected int level;
/** The hash of this single node */
protected String ownHash;
/**
* Instantiates a new tree node.
*
* @param type
* the node type
* @param parent
* the parent node
* @param doc
* the corresponding document
* @param level
* the level in the tree
*/
public TreeNode (int type, DocumentNode parent, TreeDocument doc, int level)
{
this.doc = doc;
this.type = type;
this.parent = parent;
this.modified = UNCHANGED;
this.level = level;
}
/**
* Returns the level of this node in its tree. Root has level 0.
*
* @return the level
*/
public int getLevel ()
{
return level;
}
/**
* Returns the modification state.
*
* @return the modification
*/
public int getModification ()
{
return modified;
}
/**
* Remove a modification.
*
* @param mod
* the modification
*/
public void rmModification (int mod)
{
this.modified &= ~mod;
}
/**
* Add a modification.
*
* @param mod
* the modification
*/
public void addModification (int mod)
{
this.modified |= mod;
}
/**
* Sets the modification.
*
* @param mod
* the new modification
*/
public void setModification (int mod)
{
this.modified = mod;
}
/**
* Checks for a certain modification.
*
* @param mod
* the modification
* @return true, if this node has modification mod
*/
public boolean hasModification (int mod)
{
return (this.modified & mod) > 0;
}
/**
* Gets the node type.
*
* @see #DOC_NODE
* @see #TEXT_NODE
*
* @return the node type
*/
public int getType ()
{
return type;
}
/**
* Gets the parent node.
*
* @return the parent node
*/
public DocumentNode getParent ()
{
return parent;
}
/**
* Gets the XPath expression corresponding to this node.
*
* @return the XPath to the node
*/
public String getXPath ()
{
return xPath;
}
/**
* Checks if this is the root of the tree.
*
* @return true, if this is root
*/
public boolean isRoot ()
{
return parent == null;
}
/**
* Resets all modifications.
*/
public void resetModifications ()
{
this.modified = UNCHANGED;
if (type == DOC_NODE)
{
List<TreeNode> kids = ((DocumentNode) this).getChildren ();
for (TreeNode kid : kids)
kid.resetModifications ();
}
}
/**
* Gets the corresponding document.
*
* @return the document
*/
public TreeDocument getDocument ()
{
return doc;
}
/**
* Checks if the network of two nodes differs.
*
* @param tn
* the node in another tree document
* @param conMgmr
* the connection manager
* @param c
* the connection
* @return true, if parents of these nodes are <strong>not</strong> connected.
*/
public boolean networkDiffers (TreeNode tn, ConnectionManager conMgmr,
Connection c)
{
DocumentNode p = getParent ();
DocumentNode tnp = tn.getParent ();
// both root?
if (p == null && tnp == null)
return false;
// one root?
if (p == null || tnp == null)
return true;
// parents connected and same child no.?
if (!conMgmr.parentsConnected (c))
{
if (p != null)
p.addModification (SUB_MODIFIED);
if (tnp != null)
tnp.addModification (SUB_MODIFIED);
return true;
}
if (p.getNoOfChild (this) != tnp.getNoOfChild (tn))
{
p.addModification (KIDSSWAPPED);
tnp.addModification (KIDSSWAPPED);
addModification (SWAPPEDKID);
tn.addModification (SWAPPEDKID);
}
return false;
}
/**
* Check if content between two nodes differs. Just compares the nodes,
* neglects everything else of the tree (e.g. network, kids etc).
*
* @param tn
* the other node to compare
* @return true, if nodes differ
*/
public boolean contentDiffers (TreeNode tn)
{
if (type != tn.type)
return true;
return !ownHash.equals (tn.ownHash);
}
/**
* Gets the tag name. For document nodes it's the actual tag name, in case of
* text nodes you'll receive <code>TreeNode.TEXT_TAG</code>.
*
* @return the tag name
*/
public abstract String getTagName ();
/**
* Gets the weight of this node.
*
* @return the weight
*/
public abstract double getWeight ();
/**
* Gets the hash of this single node (w/o its subtree).
*
* @return the hash of the node
*/
public abstract String getOwnHash ();
/**
* Gets the calculated hash of the subtree rooted in this node, in TextNodes
* it equals the own hash.
*
* @return the hash of the current subtree
*/
public abstract String getSubTreeHash ();
/**
* Evaluate the modifications of this node. Just useful for tree comparisons.
*
* @param conMgmr
* the connection manager
* @return true, if node was changed
*/
public abstract boolean evaluate (ConnectionManager conMgmr);
/**
* Dump this node. Just for debugging purposes..
*
* @param prefix
* the prefix for a line (indention)
* @return the produced dump
*/
public abstract String dump (String prefix);
/**
* Attaches the subtree rooted in this node to the node parent.
* Recursively attaches its children. Will fail for
*
* <code>
* parent == null && this.getType () == TreeNode.TEXT_NODE
* </code>
*
* That means a text node cannot become root.
*
* @param parent
* the parent element which will root this node. If null, this node
* will be root in the document
* @return the sub doc
*/
public abstract Element getSubDoc (Element parent);
/**
* Re-setup the document structure downwards. (e.g. recompute XPaths etc.)
*
* @param doc
* the document this node corresponds to
* @param numChild
* the child number of this node
*/
protected abstract void reSetupStructureDown (TreeDocument doc, int numChild);
/**
* Re-setup the document structure upwards. (e.g. recompute hashes etc.)
*/
protected abstract void reSetupStructureUp ();
/**
* Gets the node statistics of the subtree rooted in this node: tagname =>
* number nodes having this tag name.
*
* @param map
* the map to write our statistics to
*/
public abstract void getNodeStats (HashMap<String, Integer> map);
}