1 22 23 package org.xquark.util; 24 25 26 import java.util.Collection ; 27 import java.util.List ; 28 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.SAXException ; 31 32 public class NamespaceContextHandler extends HandlerDecorator implements NamespaceContext 33 { 34 private static final String RCSRevision = "$Revision: 1.3 $"; 35 private static final String RCSName = "$Name: $"; 36 37 protected NamespaceContextStack contextStack = new NamespaceContextStack(); 38 39 private static final byte UNKNOWN = 0; 40 private static final byte START_ELEMENT = 1; 41 private static final byte END_ELEMENT = 2; 42 private static final byte START_PREFIX = 3; 43 private static final byte END_PREFIX = 4; 44 45 private byte status = UNKNOWN; 46 47 48 public NamespaceContextHandler() 49 { 50 super(); 51 } 52 53 public void startPrefixMapping(String prefix, String uri) 54 throws SAXException 55 { 56 contextStack.declarePrefix(prefix, uri); 57 super.startPrefixMapping(prefix, uri); 58 status = START_PREFIX; 59 } 60 61 public void endPrefixMapping(String prefix) 62 throws SAXException 63 { 64 if (status == END_ELEMENT || status == START_PREFIX) { 68 contextStack.popContext(); 69 contextStack.pushContext(); 70 } 71 super.endPrefixMapping(prefix); 72 status = END_PREFIX; 73 } 74 75 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 76 throws SAXException 77 { 78 super.startElement(namespaceURI, localName, qName, atts); 79 contextStack.pushContext(); 80 status = START_ELEMENT; 81 } 82 83 public void endElement(String namespaceURI, String localName, String qName) 84 throws SAXException 85 { 86 contextStack.popContext(); 87 super.endElement(namespaceURI, localName, qName); 88 status = END_ELEMENT; 89 } 90 91 public NamespaceContext getNamespaceContext() 92 { 93 return contextStack.getNamespaceContext(); 94 } 95 96 public String getNamespaceURI(String prefix) 97 { 98 return contextStack.getNamespaceURI(prefix); 99 } 100 101 public Collection getNamespaceURIs() { 102 return contextStack.getNamespaceURIs(); 103 } 104 105 public String getPrefix(String uri) 106 { 107 return contextStack.getPrefix(uri); 108 } 109 110 public List getDeclaredPrefixes() 111 { 112 return contextStack.getDeclaredPrefixes(); 113 } 114 115 public Collection getPrefixes() 116 { 117 return contextStack.getPrefixes(); 118 } 119 120 public Collection getPrefixes(String uri) 121 { 122 return contextStack.getPrefixes(uri); 123 } 124 125 } 126 | Popular Tags |