1 4 package gnu.kawa.xml; 5 import gnu.mapping.Values; 6 import gnu.lists.*; 7 import gnu.xml.*; 8 9 11 12 13 14 public class Nodes extends Values 15 16 18 { 19 20 static final int POS_SIZE = 5; 21 22 int count; 23 24 int nesting = 0; 25 boolean inAttribute; 26 NodeTree curNode; 27 XMLFilter curFragment; 28 29 public void writePosition (AbstractSequence seq, int ipos) 30 { 31 count++; 32 super.writePosition(seq, ipos); 33 } 34 35 public int find (Object seq) 36 { 37 if (gapStart > 0) 39 { 40 int oindex = getIntN(gapStart - POS_SIZE + 1); 41 if (objects[oindex] == seq) 42 return oindex; 43 } 44 if (gapEnd < data.length) 46 { 47 int oindex = getIntN(gapEnd + 1); 48 if (objects[oindex] == seq) 49 return oindex; 50 } 51 return super.find(seq); 52 } 53 54 public void writeObject(Object v) 55 { 56 if (curFragment != null) 57 { 58 if (nesting == 0 59 && (v instanceof SeqPosition || v instanceof TreeList)) 60 finishFragment(); 61 else 62 { 63 curFragment.writeObject(v); 64 return; 65 } 66 } 67 if (v instanceof SeqPosition) 68 { 69 SeqPosition seq = (SeqPosition) v; 70 writePosition(seq.sequence, seq.ipos); 71 return; 72 } 73 if (v instanceof TreeList) 74 { 75 TreeList tlist = (TreeList) v; 76 writePosition(tlist, 0); 77 return; 78 } 79 handleNonNode(); 80 curFragment.writeObject(v); 81 return; 82 } 83 84 void maybeStartTextNode () 85 { 86 if (curFragment == null) 87 { 88 throw new IllegalArgumentException ("non-node where node required"); 89 } 90 } 91 92 void handleNonNode () 93 { 94 if (curFragment == null) 95 { 96 throw new ClassCastException ("atomic value where node is required"); 97 } 98 } 99 100 public void writeFloat (float v) 101 { 102 handleNonNode(); 103 curFragment.writeFloat(v); 104 } 105 106 public void writeDouble (double v) 107 { 108 handleNonNode(); 109 curFragment.writeDouble(v); 110 } 111 112 public void writeLong(long v) 113 { 114 handleNonNode(); 115 curFragment.writeLong(v); 116 } 117 118 public void writeInt(int v) 119 { 120 handleNonNode(); 121 curFragment.writeInt(v); 122 } 123 124 public void writeBoolean (boolean v) 125 { 126 handleNonNode(); 127 curFragment.writeBoolean(v); 128 } 129 130 public void write (int v) 131 { 132 maybeStartTextNode(); 133 curFragment.write(v); 134 } 135 136 137 public Consumer append (CharSequence csq, int start, int end) 138 { 139 maybeStartTextNode(); 140 curFragment.write(csq, start, end); 141 return this; 142 } 143 144 151 152 public void write(char[] buf, int off, int len) 153 { 154 maybeStartTextNode(); 155 curFragment.write(buf, off, len); 156 } 157 158 159 public void write(CharSequence str, int start, int length) 160 161 163 { 164 maybeStartTextNode(); 165 curFragment.write(str, start, length); 166 } 167 168 public void write (String str) 169 { 170 maybeStartTextNode(); 171 curFragment.write(str); 172 } 173 174 private void maybeStartNonTextNode () 175 { 176 if (curFragment != null && nesting == 0) 177 finishFragment(); 178 if (curFragment == null) 179 startFragment(); 180 nesting++; 181 } 182 183 private void maybeEndNonTextNode () 184 { 185 if (--nesting == 0) 186 finishFragment(); 187 } 188 189 public void beginGroup (Object type) 190 { 191 maybeStartNonTextNode(); 192 curFragment.beginGroup(type); 193 } 194 195 public void endGroup () 196 { 197 curFragment.endGroup(); 198 maybeEndNonTextNode(); 199 } 200 201 public void beginAttribute(Object attrType) 202 { 203 maybeStartNonTextNode(); 204 curFragment.beginAttribute(attrType); 205 inAttribute = true; 206 } 207 208 public void endAttribute() 209 { 210 if (! inAttribute) 211 return; 212 inAttribute = false; 213 curFragment.endAttribute(); 214 maybeEndNonTextNode(); 215 } 216 217 public void writeComment(char[] chars, int offset, int length) 218 { 219 maybeStartNonTextNode(); 220 curFragment.writeComment(chars, offset, length); 221 maybeEndNonTextNode(); 222 } 223 224 public void writeCDATA(char[] chars, int offset, int length) 225 { 226 maybeStartNonTextNode(); 227 curFragment.writeCDATA(chars, offset, length); 228 } 229 230 public void writeProcessingInstruction(String target, char[] content, 231 int offset, int length) 232 { 233 maybeStartNonTextNode(); 234 curFragment.writeProcessingInstruction(target, content, offset, length); 235 maybeEndNonTextNode(); 236 } 237 238 public void beginDocument() 239 { 240 maybeStartNonTextNode(); 241 curFragment.beginDocument(); 242 } 243 244 public void endDocument() 245 { 246 curFragment.endDocument(); 247 maybeEndNonTextNode(); 248 } 249 250 public void beginEntity(Object base) 251 { 252 maybeStartNonTextNode(); 253 curFragment.beginEntity(base); 254 } 255 256 public void endEntity() 257 { 258 curFragment.endEntity(); 259 maybeEndNonTextNode(); 260 } 261 262 void startFragment () 263 { 264 curNode = new NodeTree(); 265 curFragment = new XMLFilter(curNode); 266 writePosition(curNode, 0); 267 } 268 269 void finishFragment () 270 { 271 curNode = null; 272 curFragment = null; 273 } 274 275 public int size() 276 { 277 return count; 278 } 279 280 public int getLength() 281 { 282 return count; 283 } 284 285 public Object get (int index) 286 { 287 int i = POS_SIZE * index; 288 if (i >= gapStart) 289 i += gapEnd - gapStart; 290 if (i < 0 || i >= data.length) 291 throw new IndexOutOfBoundsException (); 292 if (data[i] != POSITION_PAIR_FOLLOWS) 294 throw new RuntimeException ("internal error - unexpected data"); 295 return KNode.make((NodeTree) objects[getIntN(i+1)], getIntN(i+3)); 296 } 297 298 299 307 308 public Object getPosNext(int ipos) 309 { 310 int index = posToDataIndex(ipos); 311 if (index == data.length) 312 return Sequence.eofValue; 313 if (data[index] != POSITION_PAIR_FOLLOWS) 314 throw new RuntimeException ("internal error - unexpected data"); 315 return KNode.make((NodeTree) objects[getIntN(index+1)], getIntN(index+3)); 316 } 317 318 321 public AbstractSequence getSeq (int index) 322 { 323 int i = POS_SIZE * index; 324 if (i >= gapStart) 325 i += gapEnd - gapStart; 326 if (i < 0 || i >= data.length) 327 return null; 328 if (data[i] != POSITION_PAIR_FOLLOWS) 330 throw new RuntimeException ("internal error - unexpected data"); 331 return (AbstractSequence) objects[getIntN(i+1)]; 332 } 333 334 335 public int getPos (int index) 336 { 337 int i = POS_SIZE * index; 338 if (i >= gapStart) 339 i += gapEnd - gapStart; 340 if (data[i] != POSITION_PAIR_FOLLOWS) 342 throw new RuntimeException ("internal error - unexpected data"); 343 return getIntN(i+3); 344 } 345 346 public static KNode root (NodeTree seq, int ipos) 347 { 348 int root; 349 if (seq.gapStart > TreeList.BEGIN_ENTITY_SIZE 350 && seq.data[0] == TreeList.BEGIN_ENTITY) 351 root = TreeList.BEGIN_ENTITY_SIZE << 1; 352 else 353 root = 0; 354 return KNode.make(seq, root); 355 365 } 366 } 367 | Popular Tags |