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.Document;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.Text;
24
25
26
27
28
29
30
31 public final class TextNode extends AbstractBiNode {
32
33
34 private String text;
35
36
37 private final String nl = System.getProperty("line.separator");
38
39
40 private final String ladder = "#";
41
42
43
44
45
46
47
48
49
50 public TextNode(final int length, final String t) {
51 this.setLength(length);
52 this.text = t;
53 }
54
55
56
57
58
59
60 public BiType getType() {
61 return BiType.TEXT;
62 }
63
64
65
66
67
68 public void insert(final BiTree biTree, final int offset,
69 final int length, final int totalOffset) throws ReparseException {
70 throw new ReparseException();
71 }
72
73
74
75
76
77 public void remove(final BiTree biTree, final int offset,
78 final int length, final int totalOffset) throws ReparseException {
79 throw new ReparseException();
80 }
81
82
83
84
85
86 @Override
87 public void forwardToSibling(final boolean insert, final BiTree biTree,
88 final int offset, final int length, final int totalOffset)
89 throws ReparseException {
90 throw new UnsupportedOperationException("forwardToSibling "
91 + "at textnode not allowed");
92 }
93
94
95
96
97
98
99 public String getText() {
100 final String ret;
101
102 if (this.text == null) {
103 if (this.getNode() == null) {
104 ret = null;
105 } else {
106 ret = this.getNode().getTextContent();
107 }
108 } else {
109 ret = this.text;
110 }
111
112 return ret;
113 }
114
115
116
117
118
119
120
121
122 public Node createDOMSubtree(final Document doc) {
123 Text textNode;
124
125 textNode = doc.createTextNode(this.text);
126 this.text = null;
127
128 this.setNode(textNode);
129 return textNode;
130 }
131
132
133 @Override
134 public TextPosition searchNode(final Node node, final int totalOffset) {
135 return super.searchNode(node, totalOffset);
136 }
137
138 @Override
139 public String toString() {
140 final StringBuffer sb = new StringBuffer(32);
141
142 sb.append("[TEXT length:");
143 sb.append(this.getLength());
144 sb.append(" '");
145 sb.append(this.getText().replaceAll(this.nl, this.ladder));
146 sb.append("']");
147
148 return sb.toString();
149 }
150
151
152 public String toString(final int level) {
153 final StringBuffer sb = new StringBuffer();
154
155 sb.append(this.formatLength());
156 sb.append(':');
157 for (int i = 0; i <= level; i++) {
158 sb.append(' ');
159 }
160
161 sb.append('\'');
162 sb.append(this.getText().replaceAll(this.nl, this.ladder));
163 sb.append('\'');
164
165 return sb.toString();
166 }
167 }