1 22 23 package org.xquark.util; 24 25 import java.util.*; 26 27 import org.xml.sax.*; 28 29 public class SAXLoader implements ContentHandler { 30 private static final String RCSRevision = "$Revision: 1.2 $"; 31 private static final String RCSName = "$Name: $"; 32 33 private NamespaceContextStack contexts = new NamespaceContextStack(); 34 private ElementHandler currentHandler = null; 35 private LinkedList handlers = new LinkedList(); 36 private HashMap currentMap = new HashMap(); 37 private Locator locator = null; 38 39 public SAXLoader(ElementHandler handler) { 40 this.currentHandler = handler; 41 } 42 43 public void setElementHandler(ElementHandler handler) { 44 this.currentHandler = handler; 45 } 46 47 public String getPrefixMapping(String prefix) { 48 return getNamespaceURI(prefix); 49 } 50 51 public HashMap getElementPrefixMap() { 52 if (currentMap.isEmpty()) return null; 53 return (HashMap)currentMap.clone(); 54 } 55 56 public Locator getDocumentLocator() { 57 return locator; 58 } 59 60 public final void characters(char[] ch, int start, int length) 61 throws SAXException { 62 currentHandler.characters(ch, start, length); 63 } 64 65 public void endDocument() throws SAXException { 66 currentHandler = null; 67 } 68 69 public final void endElement(String namespaceURI, String localName, String qName) 70 throws SAXException { 71 contexts.popContext(); 72 currentHandler = (ElementHandler)handlers.removeFirst(); 73 int index = localName.indexOf(':'); 74 if (index != -1) localName = localName.substring(index+1); 75 currentHandler.endElement(namespaceURI, localName); 76 } 77 78 public void endPrefixMapping(String prefix) throws SAXException { 79 } 80 81 public final void ignorableWhitespace(char[] ch, int start, int length) 82 throws SAXException { 83 currentHandler.ignorableWhitespace(ch, start, length); 84 } 85 86 public final void processingInstruction(String target, String data) 87 throws SAXException { 88 currentHandler.processingInstruction(target, data); 89 } 90 91 public void setDocumentLocator(Locator locator) { 92 this.locator = locator; 93 } 94 95 public final void skippedEntity(String name) 96 throws SAXException { 97 currentHandler.skippedEntity(name); 98 } 99 100 public void startDocument() throws SAXException { 101 contexts.reset(); 102 currentMap.put("xml", "http://www.w3.org/XML/1998/namespace"); 103 } 104 105 public final void startElement(String namespaceURI, String localName, 106 String qName, Attributes atts) 107 throws SAXException { 108 handlers.addFirst(currentHandler); 109 currentHandler = currentHandler.startElement(namespaceURI, localName, atts); 110 currentMap.clear(); 111 contexts.pushContext(); 112 } 113 114 public void startPrefixMapping(String prefix, String uri) throws SAXException { 115 contexts.declarePrefix(prefix, uri); 116 currentMap.put(prefix, uri); 117 } 118 119 public NamespaceContext getNamespaceContext() { 120 return contexts.getNamespaceContext(); 121 } 122 123 public String getNamespaceURI(String prefix) { 124 return contexts.getNamespaceURI(prefix); 125 } 126 127 public String getPrefix(String uri) { 128 return contexts.getPrefix(uri); 129 } 130 131 public List getDeclaredPrefixes() { 132 return contexts.getDeclaredPrefixes(); 133 } 134 135 public Collection getPrefixes() { 136 return contexts.getPrefixes(); 137 } 138 139 public Collection getPrefixes(String uri) { 140 return contexts.getPrefixes(uri); 141 } 142 } 143 | Popular Tags |