1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.xml.namespace.NamespaceContext ; 25 import javax.xml.stream.XMLStreamConstants; 26 import javax.xml.stream.XMLStreamException; 27 import javax.xml.stream.XMLStreamReader; 28 import javax.xml.stream.util.StreamReaderDelegate; 29 30 public class ExtendedXMLStreamReader extends StreamReaderDelegate { 31 32 private SimpleNamespaceContext context = new SimpleNamespaceContext(); 33 34 public ExtendedXMLStreamReader(XMLStreamReader delegate) { 35 super(delegate); 36 } 37 38 public NamespaceContext getNamespaceContext() { 39 return context; 40 } 41 42 public int nextTag() throws XMLStreamException { 43 int eventType = next(); 44 while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) 46 || eventType == XMLStreamConstants.SPACE 48 || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION 49 || eventType == XMLStreamConstants.COMMENT 50 ) { 51 eventType = next(); 52 } 53 if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { 54 throw new XMLStreamException("expected start or end tag", getLocation()); 55 } 56 return eventType; 57 } 58 59 public int next() throws XMLStreamException { 60 int next = super.next(); 61 if (next == START_ELEMENT) { 62 context = new SimpleNamespaceContext(context, getNamespaces()); 63 } else if (next == END_ELEMENT) { 64 context = context.getParent(); 65 } 66 return next; 67 } 68 69 private Map getNamespaces() { 70 Map ns = new HashMap (); 71 for (int i = 0; i < getNamespaceCount(); i++) { 72 ns.put(getNamespacePrefix(i), getNamespaceURI(i)); 73 } 74 return ns; 75 } 76 77 public static class SimpleNamespaceContext implements ExtendedNamespaceContext { 78 79 private SimpleNamespaceContext parent; 80 private Map namespaces; 81 82 public SimpleNamespaceContext() { 83 namespaces = new HashMap (); 84 } 85 86 public SimpleNamespaceContext(SimpleNamespaceContext parent, Map namespaces) { 87 this.parent = parent; 88 this.namespaces = namespaces; 89 } 90 91 public SimpleNamespaceContext getParent() { 92 return parent; 93 } 94 95 public Iterator getPrefixes() { 96 HashSet prefixes = new HashSet (); 97 for (SimpleNamespaceContext context = this; context != null; context = context.parent) { 98 prefixes.addAll(context.namespaces.keySet()); 99 } 100 return prefixes.iterator(); 101 } 102 103 public String getNamespaceURI(String prefix) { 104 String uri = (String ) namespaces.get(prefix); 105 if (uri == null && parent != null) { 106 uri = parent.getNamespaceURI(prefix); 107 } 108 return uri; 109 } 110 111 public String getPrefix(String namespaceURI) { 112 for (SimpleNamespaceContext context = this; context != null; context = context.parent) { 113 for (Iterator it = context.namespaces.keySet().iterator(); it.hasNext();) { 114 Map.Entry entry = (Map.Entry ) it.next(); 115 if (entry.getValue().equals(namespaceURI)) { 116 return (String ) entry.getKey(); 117 } 118 } 119 } 120 return null; 121 } 122 123 public Iterator getPrefixes(String namespaceURI) { 124 HashSet prefixes = new HashSet (); 125 for (SimpleNamespaceContext context = this; context != null; context = context.parent) { 126 for (Iterator it = context.namespaces.keySet().iterator(); it.hasNext();) { 127 Map.Entry entry = (Map.Entry ) it.next(); 128 if (entry.getValue().equals(namespaceURI)) { 129 prefixes.add(entry.getKey()); 130 } 131 } 132 } 133 return prefixes.iterator(); 134 } 135 136 } 137 138 139 } 140 | Popular Tags |