1 package com.icl.saxon; 2 import org.xml.sax.*; 3 import org.xml.sax.helpers.XMLFilterImpl ; 4 import java.util.Stack ; 5 6 7 12 13 16 public class IDFilter extends XMLFilterImpl { 17 18 private String id; 19 private int activeDepth = 0; 20 private Stack namespacePrefixes = new Stack (); 21 private Stack namespaceURIs = new Stack (); 22 23 public IDFilter (String id) { 24 this.id = id; 26 } 27 28 public void startPrefixMapping(String prefix, String uri) 29 throws SAXException { 30 if (activeDepth > 0) { 32 super.startPrefixMapping(prefix, uri); 33 } else { 34 namespacePrefixes.push(prefix); 35 namespaceURIs.push(uri); 36 } 37 } 38 39 public void endPrefixMapping(String prefix) 40 throws SAXException { 41 if (activeDepth > 0) { 43 super.endPrefixMapping(prefix); 44 } else { 45 namespacePrefixes.pop(); 46 namespaceURIs.pop(); 47 } 48 } 49 50 51 public void startElement(String localName, String uri, String qname, Attributes atts) 52 throws SAXException { 53 if (activeDepth==0) { 55 for (int a=0; a<atts.getLength(); a++) { 56 if (atts.getType(a).equals("ID") && atts.getValue(a).equals(id)) { 57 activeDepth = 1; 58 break; 59 } 60 } 61 if (activeDepth==1) { 62 for (int n=0; n<namespacePrefixes.size(); n++) { 63 super.startPrefixMapping( 64 (String )namespacePrefixes.elementAt(n), 65 (String )namespaceURIs.elementAt(n) ); 66 } 67 } 68 } else { 69 activeDepth++; 70 } 71 if (activeDepth>0) { 72 super.startElement(localName, uri, qname, atts); 73 } 74 } 75 76 public void endElement(String localName, String uri, String qname) 77 throws SAXException { 78 if (activeDepth > 0) { 80 super.endElement(localName, uri, qname); 81 activeDepth--; 82 if (activeDepth==0) { 83 for (int n=namespacePrefixes.size()-1; n>=0; n--) { 84 super.endPrefixMapping( 85 (String )namespacePrefixes.elementAt(n) ); 86 } 87 } 88 } 89 } 90 91 public void characters(char[] chars, int start, int len) 92 throws SAXException { 93 if (activeDepth > 0) { 94 super.characters(chars, start, len); 95 } 96 } 97 98 public void ignorableWhitespace(char[] chars, int start, int len) 99 throws SAXException { 100 if (activeDepth > 0) { 101 super.ignorableWhitespace(chars, start, len); 102 } 103 } 104 105 public void processingInstruction(String name, String data) 106 throws SAXException { 107 if (activeDepth > 0) { 108 super.processingInstruction(name, data); 109 } 110 } 111 } 112 | Popular Tags |