1 4 package gnu.xml; 5 import gnu.lists.*; 6 import gnu.mapping.Symbol; 7 8 10 11 public class NamedChildrenFilter extends FilterConsumer 12 { 13 String namespaceURI; 14 String localName; 15 16 int level; 17 int matchLevel; 18 19 public static NamedChildrenFilter 20 make (String namespaceURI, String localName, Consumer out) 21 { 22 return new NamedChildrenFilter(namespaceURI, localName, out); 23 } 24 25 public NamedChildrenFilter (String namespaceURI, String 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 type) 47 { 48 if (skipping && level == 1 ) 52 { 53 String curNamespaceURI; 54 String 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(); } 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 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
|