File: XMLProcessorImpl.java

package info (click to toggle)
libxt-java 0.19991105-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,908 kB
  • ctags: 2,762
  • sloc: java: 12,823; makefile: 52; xml: 46
file content (694 lines) | stat: -rw-r--r-- 18,125 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
package com.jclark.xsl.sax;

import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.util.Hashtable;
import org.xml.sax.*;
import com.jclark.xsl.om.*;
import com.jclark.xsl.tr.Result;
import com.jclark.xsl.tr.LoadContext;

public class XMLProcessorImpl implements XMLProcessorEx {

  static private abstract
  class NodeImpl implements Node {
    ContainerNodeImpl parent;
    RootNodeImpl root;
    int index;
    NodeImpl nextSibling;

    NodeImpl() {
      this.index = 0;
      this.parent = null;
      this.nextSibling = null;
    }
    NodeImpl(int index, ContainerNodeImpl parent) {
      this.index = index;
      this.parent = parent;
      this.root = parent.root;
      this.nextSibling = null;
      if (parent.lastChild == null)
	parent.firstChild = parent.lastChild = this;
      else {
	parent.lastChild.nextSibling = this;
	parent.lastChild = this;
      }
    }
    public Node getParent() {
      return parent;
    }
    public SafeNodeIterator getFollowingSiblings() {
      return new NodeIteratorImpl(nextSibling);
    }
    public URL getURL() {
      return parent.getURL();
    }
    public int getLineNumber() {
      return parent.getLineNumber();
    }
    boolean canStrip() {
      return false;
    }
    public Node getAttribute(Name name) {
      return null;
    }
    public String getAttributeValue(Name name) {
      return null;
    }
    public SafeNodeIterator getAttributes() {
      return new NodeIteratorImpl(null);
    }
    public Name getName() {
      return null;
    }
    public NamespacePrefixMap getNamespacePrefixMap() {
      return parent.nsMap;
    }
    public int compareTo(Node node) {
      NodeImpl ni = (NodeImpl)node;
      if (root == ni.root)
	return index - ((NodeImpl)node).index;
      return root.compareRootTo(ni.root);
    }
    public Node getElementWithId(String name) {
      return root.getElementWithId(name);
    }
    public String getUnparsedEntityURI(String name) {
      return root.getUnparsedEntityURI(name);
    }
    public boolean isId(String name) {
      return false;
    }
    public String getGeneratedId() {
      int d = root.getDocumentIndex();
      if (d == 0)
	return "N" + String.valueOf(index);
      else
	return "N" + String.valueOf(d) + "_" + String.valueOf(index);
    }
    public Node getRoot() {
      return root;
    }
  }

  static private
  class NodeIteratorImpl implements SafeNodeIterator {
    private NodeImpl nextNode;

    NodeIteratorImpl(NodeImpl nextNode) {
      this.nextNode = nextNode;
    }

    public Node next() {
      NodeImpl tem = nextNode;
      if (tem != null)
	nextNode = tem.nextSibling;
      return tem;
    }
  }

  static private abstract
  class ContainerNodeImpl extends NodeImpl {
    NodeImpl firstChild;
    NodeImpl lastChild;
    NamespacePrefixMap nsMap;

    ContainerNodeImpl(NamespacePrefixMap nsMap) {
      this.nsMap = nsMap;
    }

    ContainerNodeImpl(int index, ContainerNodeImpl parent) {
      super(index, parent);
      nsMap = parent.nsMap;
    }

    public SafeNodeIterator getChildren() {
      return new NodeIteratorImpl(firstChild);
    }

    public String getData() {
      return null;
    }

    boolean preserveSpace() {
      return false;
    }

    public NamespacePrefixMap getNamespacePrefixMap() {
      return nsMap;
    }

    void addId(String id, NodeImpl node) {
      parent.addId(id, node);
    }
  }

  static private
  class RootNodeImpl extends ContainerNodeImpl {
    private String systemId;
    private int documentIndex;
    private Hashtable idTable = new Hashtable();
    private Hashtable unparsedEntityURITable = new Hashtable();

    RootNodeImpl(String systemId, int documentIndex, NamespacePrefixMap nsMap) {
      super(nsMap);
      this.systemId = systemId;
      this.documentIndex = documentIndex;
      this.root = this;
    }
    public byte getType() {
      return Node.ROOT;
    }
    public URL getURL() {
      if (systemId != null) {
	try {
	  return new URL(systemId);
	}
	catch (MalformedURLException e) { }
      }
      return null;
    }
    public int getLineNumber() {
      return 1;
    }

    public Node getElementWithId(String name) {
      return (Node)idTable.get(name);
    }

    void addId(String id, NodeImpl node) {
      if (idTable.get(id) == null)
	idTable.put(id, node);
    }

    public String getUnparsedEntityURI(String name) {
      return (String)unparsedEntityURITable.get(name);
    }

    int compareRootTo(RootNodeImpl r) {
      if (systemId == null) {
	if (r.systemId == null)
	  return documentIndex - r.documentIndex;
	return -1;
      }
      else if (r.systemId == null)
	return 1;
      int n = systemId.compareTo(r.systemId);
      if (n != 0)
	return n;
      return documentIndex - r.documentIndex;
    }
    int getDocumentIndex() {
      return documentIndex;
    }

  }

  static private class NullLocator implements Locator {
    public String getPublicId() { return null; }
    public String getSystemId() { return null; }
    public int getLineNumber() { return -1; }
    public int getColumnNumber() { return -1; }
  }

  static private
  class ElementNodeImpl extends ContainerNodeImpl {
    private Name name;
    private Object[] atts;
    private int lineNumber;
    private String systemId;

    ElementNodeImpl(String name, AttributeList attList, Locator loc,
		    int index, ContainerNodeImpl parent) throws XSLException {
      super(index, parent);
      lineNumber = loc.getLineNumber();
      systemId = loc.getSystemId();
      int nAtts = attList.getLength();
      if (nAtts > 0) {
	int nNsAtts = 0;
	for (int i = 0; i < nAtts; i++) {
	  String tem = attList.getName(i);
	  if (tem.startsWith("xmlns")) {
	    nNsAtts++;
	    if (tem.length() == 5) {
	      String ns = attList.getValue(i);
	      if (ns.length() == 0)
		nsMap = nsMap.unbindDefault();
	      else
		nsMap = nsMap.bindDefault(ns);
	    }
	    else if (tem.charAt(5) == ':')
	      nsMap = nsMap.bind(tem.substring(6),
				 attList.getValue(i));
	  }
	}
	int n = nAtts - nNsAtts;
	if (n > 0) {
	  Object[] vec = new Object[n*2];
	  int j = 0;
	  for (int i = 0; i < nAtts; i++) {
	    String tem = attList.getName(i);
	    if (!tem.startsWith("xmlns")) {
	      vec[j++] = nsMap.expandAttributeName(tem, this);
	      // FIXME resolve relative URL
	      vec[j++] = attList.getValue(i);
	    }
	    if (attList.getType(i).length() == 2)
	      parent.addId(attList.getValue(i), this);
	  }
	  // Assign here to avoid inconsistent state if exception
	  // is thrown.
	  atts = vec;
	}
      }
      this.name = nsMap.expandElementTypeName(name, this);
    }

    public Name getName() {
      return name;
    }

    public byte getType() {
      return Node.ELEMENT;
    }

    public SafeNodeIterator getAttributes() {
      return new SafeNodeIterator() {
	private int i = 0;
	public Node next() {
	  if (atts == null)
	    return null;
	  int i2 = i*2;
	  if (i2 == atts.length)
	    return null;
	  return new AttributeNodeImpl(index + ++i,
				       ElementNodeImpl.this,
				       (Name)atts[i2],
				       (String)atts[i2 + 1]);
	}
      };
    }

    public Node getAttribute(Name name) {
      if (atts != null) {
	for (int i = 0; i < atts.length; i += 2)
	  if (atts[i].equals(name))
	    return new AttributeNodeImpl(this.index + (i >> 1) + 1,
					 this,
					 name,
					 (String)atts[i + 1]);
      }
      return null;
    }

    public String getAttributeValue(Name name) {
      if (atts != null) {
	for (int i = 0; i < atts.length; i += 2)
	  if (atts[i].equals(name))
	    return (String)atts[i + 1];
      }
      return null;
    }

    public int getLineNumber() {
      return lineNumber;
    }

    public URL getURL() {
      if (systemId != null) {
	try {
	  return new URL(systemId);
	}
	catch (MalformedURLException e) { }
      }
      return null;
    }

    public boolean isId(String name) {
      return this.equals(getElementWithId(name));
    }
  }

  static private
  class PreserveElementNodeImpl extends ElementNodeImpl {
    PreserveElementNodeImpl(String name, AttributeList atts, Locator loc,
			    int index, ContainerNodeImpl parent)  throws XSLException {
      super(name, atts, loc, index, parent);
    }
    boolean preserveSpace() {
      return true;
    }
  }

  static private
  class AttributeNodeImpl extends NodeImpl {
    private Name name;
    private String value;
    AttributeNodeImpl(int index, ContainerNodeImpl parent, Name name, String value) {
      // Don't use super(parent) because it add's this node to the children.
      this.index = index;
      this.parent = parent;
      this.root = parent.root;
      this.name = name;
      this.value = value;
    }
    public byte getType() {
      return Node.ATTRIBUTE;
    }
    public String getData() {
      return value;
    }
    public Name getName() {
      return this.name;
    }
    public SafeNodeIterator getChildren() {
      return new NodeIteratorImpl(null);
    }
    public int hashCode() {
      return index;
    }
    public boolean equals(Object obj) {
      return (obj != null
	      && obj instanceof AttributeNodeImpl
	      && ((AttributeNodeImpl)obj).index == index);
    }
  }

  static private
  class TextNodeImpl extends NodeImpl {
    private String data;
     
    public TextNodeImpl(char[] buf, int off, int len, int index, ContainerNodeImpl parent) {
      super(index, parent);
      data = new String(buf, off, len);
    }
    public byte getType() {
      return Node.TEXT;
    }
    public String getData() {
      return data;
    }
    public SafeNodeIterator getChildren() {
      return new NodeIteratorImpl(null);
    }

  }

  static private
  class StripTextNodeImpl extends TextNodeImpl {
    public StripTextNodeImpl(char[] buf, int off, int len, int index, ContainerNodeImpl parent) {
      super(buf, off, len, index, parent);
    }
    boolean canStrip() {
      return true;
    }
  }

  static private
  class CommentNodeImpl extends NodeImpl {
    private String data;
     
    public CommentNodeImpl(String data, int index, ContainerNodeImpl parent) {
      super(index, parent);
      this.data = data;
    }
    public byte getType() {
      return Node.COMMENT;
    }
    public String getData() {
      return data;
    }
    public SafeNodeIterator getChildren() {
      return new NodeIteratorImpl(null);
    }

  }

  static private
  class ProcessingInstructionNodeImpl extends NodeImpl {
    private Name name;
    private String data;
    private String systemId;
     
    // FIXME should include location

    public ProcessingInstructionNodeImpl(String name, String data, Locator loc, int index, ContainerNodeImpl parent) {
      super(index, parent);
      systemId = loc.getSystemId();
      this.name = parent.getNamespacePrefixMap().getNameTable().createName(name);
      this.data = data;
    }

    public Name getName() {
      return name;
    }

    public byte getType() {
      return Node.PROCESSING_INSTRUCTION;
    }

    public String getData() {
      return data;
    }

    public SafeNodeIterator getChildren() {
      return new NodeIteratorImpl(null);
    }
    public URL getURL() {
      if (systemId != null) {
	try {
	  return new URL(systemId);
	}
	catch (MalformedURLException e) { }
      }
      return null;
    }

  }


  static public
  interface Builder extends DocumentHandler, CommentHandler, DTDHandler {
    public Node getRootNode();
  }

  static private
  class BuilderImpl implements Builder {
    char[] dataBuf = new char[1024];
    int dataBufUsed = 0;
    RootNodeImpl rootNode;
    ContainerNodeImpl currentNode;
    int currentIndex = 1;
    boolean includeProcessingInstructions;
    boolean includeComments;
    LoadContext context;
    Locator locator = new NullLocator();

    BuilderImpl(LoadContext context, String systemId, int documentIndex,  NamespacePrefixMap nsMap) {
      this.context = context;
      includeProcessingInstructions = context.getIncludeProcessingInstructions();
      includeComments = context.getIncludeComments();
      currentNode = rootNode = new RootNodeImpl(systemId,
						documentIndex,
						nsMap);
    }

    public void startDocument() { }

    public void endDocument() { }

    public void setDocumentLocator(Locator locator) {
      this.locator = locator;
    }

    public void startElement(String name, AttributeList atts) throws SAXException {
      flushData();
      ElementNodeImpl element;
      boolean preserve;
      String space = atts.getValue("xml:space");
      if (space == null)
	preserve = currentNode.preserveSpace();
      else if (space.equals("default"))
	preserve = false;
      else if (space.equals("preserve"))
	preserve = true;
      else
	preserve = currentNode.preserveSpace();
      try {
	if (preserve)
	  element = new PreserveElementNodeImpl(name, atts, locator,
						currentIndex++, currentNode);
	else
	  element = new ElementNodeImpl(name, atts, locator,
					currentIndex++, currentNode);
      }
      catch (XSLException e) {
	throw new SAXException(e);
      }
      currentIndex += atts.getLength();
      currentNode = element;
    }

    public void characters(char ch[], int start, int length) {
      int need = length + dataBufUsed;
      if (need > dataBuf.length) {
	int newLength = dataBuf.length << 1;
	while (need > newLength)
	  newLength <<= 1;
	char[] tem = dataBuf;
	dataBuf = new char[newLength];
	if (dataBufUsed > 0)
	  System.arraycopy(tem, 0, dataBuf, 0, dataBufUsed);
      }
      for (; length > 0; length--)
	dataBuf[dataBufUsed++] = ch[start++];
    }

    public void ignorableWhitespace(char ch[], int start, int length) {
      if (dataBufUsed > 0
	  || currentNode.preserveSpace()
	  || !context.getStripSource(currentNode.getName()))
	characters(ch, start, length);
    }

    public void endElement(String name) {
      flushData();
      currentNode = currentNode.parent;
    }

    public void processingInstruction(String target, String data) {
      if (target == null)
	comment(data);
      else {
	if (includeProcessingInstructions) {
	  flushData();
	  new ProcessingInstructionNodeImpl(target, data, locator, currentIndex++, currentNode);
	}
      }
    }

    public void comment(String contents) {
      if (includeComments) {
	flushData();
	new CommentNodeImpl(contents, currentIndex++, currentNode);
      }
    }

    public void unparsedEntityDecl(String name,
				   String publicId,
				   String systemId,
				   String notationName) {
      rootNode.unparsedEntityURITable.put(name, systemId);
    }

    public void notationDecl(String name,
			     String publicId,
			     String systemId) {
    }

    public Node getRootNode() {
      return rootNode;
    }

    private static boolean isWhitespace(char[] buf, int len) {
      for (int i = 0; i < len; i++) {
	switch (buf[i]) {
	case ' ':
	case '\n':
	case '\r':
	case '\t':
	  break;
	default:
	  return false;
	}
      }
      return true;
    }

    private final void flushData() {
      if (dataBufUsed > 0) {
	if (!isWhitespace(dataBuf, dataBufUsed)
	    || currentNode.preserveSpace()
	    || !context.getStripSource(currentNode.getName()))
	  new TextNodeImpl(dataBuf, 0, dataBufUsed, currentIndex++, currentNode);
	dataBufUsed = 0;
      }
    }
  }

  public Node load(InputSource source,
		   int documentIndex,
		   LoadContext context,
		   NameTable nameTable)
    throws IOException, XSLException {
    try {
      Builder builder = new BuilderImpl(context,
					source.getSystemId(),
					documentIndex,
					nameTable.getEmptyNamespacePrefixMap());
      parser.setDocumentHandler(builder);
      parser.setDTDHandler(builder);
      parser.parse(source);
      return builder.getRootNode();
    }
    catch (SAXParseException e) {
      throw new XSLException(e);
    }
    catch (SAXException e) {
      Exception wrapped = e.getException();
      if (wrapped == null)
	throw new XSLException(e.getMessage());
      if (wrapped instanceof XSLException)
	throw (XSLException)e.getException();
      throw new XSLException(wrapped);
    }
  }

  public Node load(URL url, int documentIndex, LoadContext context, NameTable nameTable) throws IOException, XSLException {
    return load(new InputSource(url.toString()),
		documentIndex,
	        context,
		nameTable);
  }

  static
  public Builder createBuilder(String systemId, int documentIndex, LoadContext context, NameTable nameTable) {
    return new BuilderImpl(context,
			   systemId,
			   documentIndex,
			   nameTable.getEmptyNamespacePrefixMap());
  }

  public Result createResult(Node baseNode,
			     int documentIndex,
			     LoadContext loadContext,
			     Node[] rootNode) throws XSLException {
    URL baseURL = null;
    if (baseNode != null)
      baseURL = baseNode.getURL();
    String base;
    if (baseURL == null)
      base = null;
    else
      base = baseURL.toString();
    XMLProcessorImpl.Builder builder
      = XMLProcessorImpl.createBuilder(base,
				       documentIndex,
				       loadContext,
				       baseNode.getNamespacePrefixMap().getNameTable());
    rootNode[0] = builder.getRootNode();
    return new MultiNamespaceResult(builder, errorHandler);
  }

  private Parser parser;
  private ErrorHandler errorHandler;

  public XMLProcessorImpl(Parser parser) {
    this.parser = parser;
  }
  
  public void setErrorHandler(ErrorHandler errorHandler) {
    this.errorHandler = errorHandler;
  }
}