1 17 package org.apache.servicemix.jbi.jaxp; 18 19 23 import java.util.ArrayList ; 24 25 import javax.xml.namespace.NamespaceContext ; 26 import javax.xml.namespace.QName ; 27 import javax.xml.stream.XMLStreamException; 28 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.CDATASection ; 31 import org.w3c.dom.Comment ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.EntityReference ; 35 import org.w3c.dom.NamedNodeMap ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.Text ; 38 39 public class W3CDOMStreamReader extends DOMStreamReader { 40 private Node content; 41 42 private Document document; 43 44 private W3CNamespaceContext context; 45 46 49 public W3CDOMStreamReader(Element element) { 50 super(new ElementFrame(element, null)); 51 52 this.document = element.getOwnerDocument(); 53 } 54 55 60 public Document getDocument() { 61 return document; 62 } 63 64 68 protected void newFrame(ElementFrame frame) { 69 Element element = getCurrentElement(); 70 frame.uris = new ArrayList (); 71 frame.prefixes = new ArrayList (); 72 frame.attributes = new ArrayList (); 73 74 if (context == null) 75 context = new W3CNamespaceContext(); 76 77 context.setElement(element); 78 79 NamedNodeMap nodes = element.getAttributes(); 80 81 String nsURI = element.getNamespaceURI(); 82 String ePrefix = element.getPrefix(); 83 if (ePrefix == null) { 84 ePrefix = ""; 85 } 86 87 for (int i = 0; i < nodes.getLength(); i++) { 88 Node node = nodes.item(i); 89 String prefix = node.getPrefix(); 90 String localName = node.getLocalName(); 91 String value = node.getNodeValue(); 92 String name = node.getNodeName(); 93 String uri = node.getNamespaceURI(); 94 95 if (prefix == null) 96 prefix = ""; 97 98 if (name != null && name.equals("xmlns")) { 99 frame.uris.add(value); 100 frame.prefixes.add(""); 101 } else if (prefix.length() > 0 && prefix.equals("xmlns")) { 102 frame.uris.add(value); 103 frame.prefixes.add(localName); 104 } else if (name.startsWith("xmlns:")) { 105 prefix = name.substring(6); 106 frame.uris.add(value); 107 frame.prefixes.add(prefix); 108 } else { 109 frame.attributes.add(node); 110 } 111 } 112 } 113 114 protected void endElement() { 115 super.endElement(); 116 } 117 118 Element getCurrentElement() { 119 return (Element ) getCurrentFrame().element; 120 } 121 122 protected ElementFrame getChildFrame(int currentChild) { 123 return new ElementFrame(getCurrentElement().getChildNodes().item(currentChild), getCurrentFrame()); 124 } 125 126 protected int getChildCount() { 127 return getCurrentElement().getChildNodes().getLength(); 128 } 129 130 protected int moveToChild(int currentChild) { 131 this.content = getCurrentElement().getChildNodes().item(currentChild); 132 133 if (content instanceof Text ) 134 return CHARACTERS; 135 else if (content instanceof Element ) 136 return START_ELEMENT; 137 else if (content instanceof CDATASection ) 138 return CDATA; 139 else if (content instanceof Comment ) 140 return CHARACTERS; 141 else if (content instanceof EntityReference ) 142 return ENTITY_REFERENCE; 143 144 throw new IllegalStateException (); 145 } 146 147 public String getElementText() throws XMLStreamException { 148 return getText(); 149 } 150 151 public String getNamespaceURI(String prefix) { 152 ElementFrame frame = getCurrentFrame(); 153 154 while (null != frame) { 155 int index = frame.prefixes.indexOf(prefix); 156 if (index != -1) { 157 return (String ) frame.uris.get(index); 158 } 159 160 frame = frame.parent; 161 } 162 163 return null; 164 } 165 166 public String getAttributeValue(String ns, String local) { 167 if (ns == null || ns.equals("")) 168 return getCurrentElement().getAttribute(local); 169 else 170 return getCurrentElement().getAttributeNS(ns, local); 171 } 172 173 public int getAttributeCount() { 174 return getCurrentFrame().attributes.size(); 175 } 176 177 Attr getAttribute(int i) { 178 return (Attr ) getCurrentFrame().attributes.get(i); 179 } 180 181 private String getLocalName(Attr attr) { 182 183 String name = attr.getLocalName(); 184 if (name == null) { 185 name = attr.getNodeName(); 186 } 187 return name; 188 } 189 190 public QName getAttributeName(int i) { 191 Attr at = getAttribute(i); 192 193 String prefix = at.getPrefix(); 194 String ln = getLocalName(at); 195 String ns = at.getNamespaceURI(); 197 198 if (prefix == null) { 199 return new QName (ns, ln); 200 } else { 201 return new QName (ns, ln, prefix); 202 } 203 } 204 205 public String getAttributeNamespace(int i) { 206 return getAttribute(i).getNamespaceURI(); 207 } 208 209 public String getAttributeLocalName(int i) { 210 Attr attr = getAttribute(i); 211 String name = getLocalName(attr); 212 return name; 213 } 214 215 public String getAttributePrefix(int i) { 216 return getAttribute(i).getPrefix(); 217 } 218 219 public String getAttributeType(int i) { 220 return toStaxType(getAttribute(i).getNodeType()); 221 } 222 223 public static String toStaxType(short jdom) { 224 switch (jdom) { 225 default: 226 return null; 227 } 228 } 229 230 public String getAttributeValue(int i) { 231 return getAttribute(i).getValue(); 232 } 233 234 public boolean isAttributeSpecified(int i) { 235 return getAttribute(i).getValue() != null; 236 } 237 238 public int getNamespaceCount() { 239 return getCurrentFrame().prefixes.size(); 240 } 241 242 public String getNamespacePrefix(int i) { 243 return (String ) getCurrentFrame().prefixes.get(i); 244 } 245 246 public String getNamespaceURI(int i) { 247 return (String ) getCurrentFrame().uris.get(i); 248 } 249 250 public NamespaceContext getNamespaceContext() { 251 return context; 252 } 253 254 public String getText() { 255 Node node = getCurrentElement().getChildNodes().item(getCurrentFrame().currentChild); 256 return node.getNodeValue(); 257 } 258 259 public char[] getTextCharacters() { 260 return getText().toCharArray(); 261 } 262 263 public int getTextStart() { 264 return 0; 265 } 266 267 public int getTextLength() { 268 return getText().length(); 269 } 270 271 public String getEncoding() { 272 return null; 273 } 274 275 public QName getName() { 276 Element el = getCurrentElement(); 277 278 String prefix = getPrefix(); 279 String ln = getLocalName(); 280 281 if (prefix == null) { 282 return new QName (el.getNamespaceURI(), ln); 283 } else { 284 return new QName (el.getNamespaceURI(), ln, prefix); 285 } 286 } 287 288 public String getLocalName() { 289 String name = getCurrentElement().getLocalName(); 290 if (name == null) { 292 name = getCurrentElement().getNodeName(); 293 } 294 return name; 295 } 296 297 public String getNamespaceURI() { 298 return getCurrentElement().getNamespaceURI(); 299 } 300 301 public String getPrefix() { 302 String prefix = getCurrentElement().getPrefix(); 303 if (prefix == null) { 304 prefix = ""; 305 } 306 return prefix; 307 } 308 309 public String getPITarget() { 310 throw new UnsupportedOperationException (); 311 } 312 313 public String getPIData() { 314 throw new UnsupportedOperationException (); 315 } 316 } 317 | Popular Tags |