KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > xml > NamedChildrenFilter


1 // Copyright (c) 2002 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.xml;
5 import gnu.lists.*;
6 import gnu.mapping.Symbol;
7
8 /** A FilterConsumer that only passes through matching children.
9  */

10
11 public class NamedChildrenFilter extends FilterConsumer
12 {
13   String JavaDoc namespaceURI;
14   String JavaDoc localName;
15
16   int level;
17   int matchLevel;
18
19   public static NamedChildrenFilter
20   make (String JavaDoc namespaceURI, String JavaDoc localName, Consumer out)
21   {
22     return new NamedChildrenFilter(namespaceURI, localName, out);
23   }
24
25   public NamedChildrenFilter (String JavaDoc namespaceURI, String JavaDoc localName,
26                   Consumer out)
27   {
28     super(out);
29     this.namespaceURI = namespaceURI;
30     this.localName = localName;
31     skipping = true;
32   }
33
34   public void beginDocument()
35   {
36     level++;
37     super.beginDocument();
38   }
39
40   public void endDocument()
41   {
42     level--;
43     super.endDocument();
44   }
45
46   public void beginGroup(Object JavaDoc type)
47   {
48     if (skipping && level == 1 // && axis is child::
49
// || axis is descdendent-or-self::
50
// || level >= 1 && axis is descdendent
51
)
52       {
53     String JavaDoc curNamespaceURI;
54     String JavaDoc curLocalName;
55     if (type instanceof Symbol)
56       {
57         Symbol qname = (Symbol) type;
58         curNamespaceURI = qname.getNamespaceURI();
59         curLocalName = qname.getLocalName();
60       }
61     else
62       {
63         curNamespaceURI = "";
64         curLocalName = type.toString().intern(); // FIXME
65
}
66     if ((localName == curLocalName || localName == null)
67         && (namespaceURI == curNamespaceURI || namespaceURI == null))
68       {
69         skipping = false;
70         matchLevel = level;
71       }
72       }
73
74     super.beginGroup(type);
75     level++;
76   }
77
78   public void endGroup()
79   {
80     level--;
81     super.endGroup();
82     if (! skipping && matchLevel == level)
83       skipping = true;
84   }
85
86   public void writeObject(Object JavaDoc val)
87   {
88     if (val instanceof SeqPosition)
89       {
90     SeqPosition pos = (SeqPosition) val;
91     if (pos.sequence instanceof TreeList)
92       {
93         ((TreeList) pos.sequence).consumeNext(pos.ipos, this);
94         return;
95       }
96       }
97     if (val instanceof Consumable)
98       ((Consumable) val).consume(this);
99     else
100       super.writeObject(val);
101   }
102 }
103
Popular Tags