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 java.util.ArrayList;
22 import java.util.List;
23
24 import net.sourceforge.jeuclid.elements.JEuclidElementFactory;
25
26 import org.xml.sax.Attributes;
27 import org.xml.sax.SAXParseException;
28
29
30
31
32
33
34 public class BiTreeCreationHelper {
35
36
37 private IBiNode currentBiNode;
38
39 private IBiNode root;
40
41 private final List<Integer> startPositions;
42
43
44
45
46 public BiTreeCreationHelper() {
47 this.startPositions = new ArrayList<Integer>();
48 }
49
50
51
52
53
54
55 public final IBiNode getRoot() {
56 return this.root;
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public final void createBiNode(final int totalOffset,
76 final int childOffset, final String namespaceURI,
77 final String eName, final Attributes attrs) throws SAXParseException {
78
79 if (JEuclidElementFactory.getJEuclidElementConstructor(namespaceURI,
80 eName) == null) {
81 throw new NonIncrementalElementException(eName);
82 }
83 BiNode biNode;
84
85 this.startPositions.add(totalOffset);
86
87
88 if (this.root == null) {
89 this.root = new BiNode(childOffset, namespaceURI, eName, attrs);
90 this.currentBiNode = this.root;
91 } else {
92 biNode = new BiNode(childOffset, namespaceURI, eName, attrs);
93
94
95 if (this.currentBiNode.getType() == BiType.EMPTY) {
96 this.currentBiNode.addSibling(biNode);
97 } else {
98
99 ((BiNode) this.currentBiNode).addChild(biNode);
100 }
101
102 this.currentBiNode = biNode;
103 }
104 }
105
106
107
108
109
110
111
112 public final void closeBiNode(final int length) {
113 BiNode parent;
114 final int last = this.startPositions.size() - 1;
115
116 this.currentBiNode.setLength(length - this.startPositions.get(last));
117
118
119 parent = this.currentBiNode.getParent();
120 if (parent != null) {
121 this.currentBiNode = parent;
122 }
123
124 this.startPositions.remove(last);
125 }
126
127
128
129
130
131
132 public final boolean allowNewTextNode() {
133 return ((BiNode) this.currentBiNode).getChild() == null;
134 }
135
136
137
138
139
140
141
142
143
144 public final void createTextNode(final int length, final String t) {
145 ((BiNode) this.currentBiNode).addChild(new TextNode(length, t));
146 }
147
148
149
150
151
152
153
154 public final void createEmtpyNode(final int length) {
155
156 if (this.root == null) {
157 this.root = new EmptyNode(length);
158 this.currentBiNode = this.root;
159 } else {
160 ((BiNode) this.currentBiNode).addChild(new EmptyNode(length));
161 }
162 }
163 }