01: // Copyright (c) 2002 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.xml;
05:
06: import gnu.lists.*;
07: import gnu.mapping.Symbol;
08:
09: /** A FilterConsumer that only passes through matching children.
10: */
11:
12: public class NamedChildrenFilter extends FilterConsumer {
13: String namespaceURI;
14: String localName;
15:
16: int level;
17: int matchLevel;
18:
19: public static NamedChildrenFilter make(String namespaceURI,
20: String localName, Consumer out) {
21: return new NamedChildrenFilter(namespaceURI, localName, out);
22: }
23:
24: public NamedChildrenFilter(String namespaceURI, String localName,
25: Consumer out) {
26: super (out);
27: this .namespaceURI = namespaceURI;
28: this .localName = localName;
29: skipping = true;
30: }
31:
32: public void startDocument() {
33: level++;
34: super .startDocument();
35: }
36:
37: public void endDocument() {
38: level--;
39: super .endDocument();
40: }
41:
42: public void startElement(Object type) {
43: if (skipping && level == 1 // && axis is child::
44: // || axis is descdendent-or-self::
45: // || level >= 1 && axis is descdendent
46: ) {
47: String curNamespaceURI;
48: String curLocalName;
49: if (type instanceof Symbol) {
50: Symbol qname = (Symbol) type;
51: curNamespaceURI = qname.getNamespaceURI();
52: curLocalName = qname.getLocalName();
53: } else {
54: curNamespaceURI = "";
55: curLocalName = type.toString().intern(); // FIXME
56: }
57: if ((localName == curLocalName || localName == null)
58: && (namespaceURI == curNamespaceURI || namespaceURI == null)) {
59: skipping = false;
60: matchLevel = level;
61: }
62: }
63:
64: super .startElement(type);
65: level++;
66: }
67:
68: public void endElement() {
69: level--;
70: super .endElement();
71: if (!skipping && matchLevel == level)
72: skipping = true;
73: }
74:
75: public void writeObject(Object val) {
76: if (val instanceof SeqPosition) {
77: SeqPosition pos = (SeqPosition) val;
78: if (pos.sequence instanceof TreeList) {
79: ((TreeList) pos.sequence).consumeNext(pos.ipos, this );
80: return;
81: }
82: }
83: if (val instanceof Consumable)
84: ((Consumable) val).consume(this);
85: else
86: super.writeObject(val);
87: }
88: }
|