1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.biparser;
20
21 import org.w3c.dom.Node;
22
23
24
25
26
27
28
29 public abstract class AbstractBiNode implements IBiNode {
30
31
32 private IBiNode previous;
33
34
35 private IBiNode sibling;
36
37
38 private Node node;
39
40
41 private int length;
42
43
44
45
46
47
48 public final IBiNode getPrevious() {
49 return this.previous;
50 }
51
52
53
54
55
56
57
58 public final void setPrevious(final IBiNode prev) {
59 this.previous = prev;
60 }
61
62
63
64
65
66
67 public final BiNode getParent() {
68
69 if (this.previous != null && this.previous.getSibling() == this) {
70 return this.previous.getParent();
71 } else {
72
73 return (BiNode) this.previous;
74 }
75 }
76
77
78
79
80
81
82 public final IBiNode getSibling() {
83 return this.sibling;
84 }
85
86
87
88
89
90
91
92 public final void setSibling(final IBiNode sibl) {
93 if (sibl != null) {
94 sibl.setPrevious(this);
95 }
96
97 this.sibling = sibl;
98 }
99
100
101
102
103
104
105
106
107 public final void addSibling(final IBiNode sibl) {
108 if (this.sibling == null) {
109
110 this.setSibling(sibl);
111 } else {
112
113 this.sibling.addSibling(sibl);
114 }
115 }
116
117
118
119
120
121
122 public final Node getNode() {
123 return this.node;
124 }
125
126
127
128
129
130
131
132 public final void setNode(final Node n) {
133 this.node = n;
134 }
135
136
137
138
139
140
141 public final int getLength() {
142 return this.length;
143 }
144
145
146
147
148
149
150
151 public final void setLength(final int len) {
152 this.length = len;
153 }
154
155
156
157
158
159
160
161 public final void changeLengthRec(final int change) {
162 if (change == 0) {
163 return;
164 }
165
166 this.length += change;
167
168 if (this.getParent() != null) {
169 this.getParent().changeLengthRec(change);
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public void forwardToSibling(final boolean insert, final BiTree biTree,
193 final int offset, final int len, final int totalOffset)
194 throws ReparseException, NonIncrementalElementException {
195
196 if (this.getSibling() == null) {
197
198 throw new ReparseException();
199 } else {
200 if (insert) {
201 this.getSibling().insert(biTree, offset, len, totalOffset);
202 } else {
203 this.getSibling().remove(biTree, offset, len, totalOffset);
204 }
205 }
206 }
207
208
209
210
211
212
213
214
215
216
217
218 public TextPosition searchNode(final Node n, final int totalOffset) {
219 if (this.node != null && this.node.equals(n)) {
220 return new TextPosition(totalOffset, this.length);
221 }
222
223 return null;
224 }
225
226
227
228
229
230
231 public String formatLength() {
232 int i;
233 final int max = 3;
234 final StringBuffer sb = new StringBuffer();
235
236 for (i = 1; i <= max && 1 > this.getLength() / Math.pow(10, max - i); i++) {
237
238 if (i == 1 && this.getLength() == 0) {
239 continue;
240 }
241 sb.append(' ');
242 }
243
244 sb.append(this.length);
245
246 return sb.toString();
247 }
248 }