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 net.sourceforge.jeuclid.elements.generic.DocumentElement;
22
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27
28
29
30
31
32 public class BiTree {
33
34
35 private Document doc;
36
37
38 private IBiNode root;
39
40
41 private String text;
42
43
44
45
46 public BiTree() {
47
48 }
49
50
51
52
53 public final void createDOMTree() {
54 Node subtree;
55
56 this.doc = new DocumentElement();
57
58 subtree = this.getDOMTree(this.doc);
59
60 if (subtree != null) {
61 this.doc.appendChild(subtree);
62 }
63 }
64
65
66
67
68
69
70
71
72 public final Node getDOMTree(final Document d) {
73 Node treeRoot;
74
75 if (this.root.getType() == BiType.EMPTY) {
76 treeRoot = this.root.getSibling().createDOMSubtree(d);
77 } else {
78 treeRoot = this.root.createDOMSubtree(d);
79 }
80
81 return treeRoot;
82 }
83
84
85
86
87
88
89 public final IBiNode getRoot() {
90 return this.root;
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public final void insert(final int offset, final int length, final String t)
109 throws ReparseException, NonIncrementalElementException {
110 this.text = t;
111 this.root.insert(this, offset, length, 0);
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public final void remove(final int offset, final int length, final String t)
130 throws ReparseException, NonIncrementalElementException {
131 this.text = t;
132 this.root.remove(this, offset, length, 0);
133 }
134
135
136
137
138
139
140
141 public final void setRoot(final IBiNode r) {
142
143 if (r == null) {
144 this.doc = null;
145 } else {
146 r.setPrevious(null);
147 }
148
149 this.root = r;
150 }
151
152
153
154
155
156
157 public String getText() {
158 return this.text;
159 }
160
161
162
163
164
165
166 public Node getDocument() {
167 return this.doc;
168 }
169
170
171
172
173
174
175
176
177
178 public TextPosition searchNode(final Node node) {
179 if (this.root == null) {
180 return null;
181 } else {
182 return this.root.searchNode(node, 0);
183 }
184 }
185
186
187
188
189
190
191 @Override
192 public String toString() {
193 if (this.root == null) {
194 return "root is null";
195 } else {
196 return this.root.toString(0);
197 }
198 }
199
200
201
202
203
204
205 public String toStringDOM() {
206 return this.toStringDOM(0, this.doc.getDocumentElement());
207 }
208
209 private String toStringDOM(final int level, final Node n) {
210 int i;
211 NodeList nl;
212 final StringBuilder sb = new StringBuilder(128);
213
214 if (n == null) {
215 return "node is null";
216 }
217
218 for (i = 0; i < level; i++) {
219 sb.append(" ");
220 }
221
222 sb.append(" ");
223 if (n.getNodeType() == Node.TEXT_NODE) {
224 sb.append(n.getTextContent());
225 } else {
226 sb.append(n.getNodeName());
227 }
228 sb.append("\n");
229
230 nl = n.getChildNodes();
231 for (i = 0; i < nl.getLength(); i++) {
232 sb.append(this.toStringDOM(level + 1, nl.item(i)));
233 }
234
235 return sb.toString();
236 }
237 }