1 /*
2 * Copyright 2009 - 2010 JEuclid, http://jeuclid.sf.net
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* $Id $ */
18
19 package net.sourceforge.jeuclid.biparser;
20
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Node;
23
24 /**
25 * this class is used to store specific information about a empty node. the node
26 * cannot have children, but a sibling
27 *
28 * @version $Revision: 0b66106c7ff7 $
29 */
30 public final class EmptyNode extends AbstractBiNode {
31
32 /**
33 * create a new EmptyNode.
34 *
35 * @param length
36 * of EmptyNode
37 */
38 public EmptyNode(final int length) {
39 this.setLength(length);
40 }
41
42 /**
43 * get the type of node.
44 *
45 * @return EMPTY
46 */
47 public BiType getType() {
48 return BiType.EMPTY;
49 }
50
51 /**
52 * insert characters in EmptyNode, reparse if characters contain '<' or '>'.
53 * else change length of EmptyNode
54 * {@inheritDoc}
55 */
56 public void insert(final BiTree biTree, final int offset, final int length,
57 final int totalOffset) throws ReparseException,
58 NonIncrementalElementException {
59 int position;
60 String insert;
61
62 // System.out.println("insert " + toString() + " offset=" +
63 // offset + " length=" + length);
64
65 // start position in this node
66 if (offset <= this.getLength()) {
67
68 position = totalOffset + offset;
69 insert = biTree.getText().substring(position, position + length);
70
71 if (insert.contains("<") || insert.contains(">")) {
72 // reparsing
73 throw new ReparseException();
74 }
75
76 this.changeLengthRec(length);
77
78 } else {
79 // start position outside this node
80 this.forwardToSibling(true, biTree, offset - this.getLength(),
81 length, totalOffset + this.getLength());
82 }
83 }
84
85 /**
86 * remove characters from EmptyNode, reparse if length gets 0. {@inheritDoc}
87 */
88 public void remove(final BiTree biTree, final int offset, final int length,
89 final int totalOffset) throws ReparseException,
90 NonIncrementalElementException {
91 // System.out.println("remove " + toString() + " offset=" +
92 // offset + " length=" + length);
93
94 // start position in this node
95 if (offset <= this.getLength()) {
96
97 if (offset == 0 && length >= this.getLength()) {
98 // remove this node
99 throw new ReparseException();
100
101 } else {
102 // change length
103 // end position in this node
104 if (offset + length <= this.getLength()) {
105 this.changeLengthRec(-length);
106
107 } else {
108 // end position outside this node
109 this.changeLengthRec(offset - this.getLength());
110
111 // forward remainder to sibling
112 this.forwardToSibling(false, biTree, 0, offset + length
113 - this.getLength(), totalOffset + this.getLength());
114 }
115 }
116 } else {
117 // start position outside this node
118 this.forwardToSibling(false, biTree, offset - this.getLength(),
119 length, totalOffset + this.getLength());
120 }
121 }
122
123 /**
124 * don't create a DOM-tree from EmptyNode (EmptyNode has no DOM node).
125 *
126 * @param doc
127 * Document to create DOM-tree
128 * @return null
129 */
130 public Node createDOMSubtree(final Document doc) {
131 return null;
132 }
133
134 /** {@inheritDoc} */
135 @Override
136 public TextPosition searchNode(final Node node, final int totalOffset) {
137 // forward to sibling
138 if (this.getSibling() != null) {
139 return this.getSibling().searchNode(node,
140 totalOffset + this.getLength());
141 }
142
143 return null;
144 }
145
146 @Override
147 public String toString() {
148 final StringBuffer sb = new StringBuffer(32);
149
150 sb.append("[EMTPY length: ");
151 sb.append(this.getLength());
152 sb.append(']');
153
154 return sb.toString();
155 }
156
157 /** {@inheritDoc} */
158 public String toString(final int level) {
159 final StringBuffer sb = new StringBuffer(32);
160 final String nl = System.getProperty("line.separator");
161
162 sb.append(this.formatLength());
163 sb.append(':');
164 for (int i = 0; i <= level; i++) {
165 sb.append(' ');
166 }
167
168 sb.append("EMTPY");
169
170 if (this.getSibling() != null) {
171 sb.append(nl);
172 sb.append(this.getSibling().toString(level));
173 }
174
175 return sb.toString();
176 }
177 }