1 16 package org.apache.axis2.om.impl.llom.builder; 17 18 import org.apache.axis2.om.*; 19 import org.apache.axis2.om.impl.llom.OMElementImpl; 20 import org.apache.axis2.om.impl.llom.OMNodeImpl; 21 22 import javax.xml.stream.XMLStreamConstants; 23 import javax.xml.stream.XMLStreamReader; 24 25 29 public abstract class StAXBuilder implements OMXMLParserWrapper { 30 31 34 protected XMLStreamReader parser; 35 36 39 protected OMFactory omfactory; 40 41 44 protected OMNode lastNode; 45 46 48 51 protected boolean done = false; 52 53 55 58 protected boolean cache = true; 59 60 63 66 protected boolean parserAccessed = false; 67 68 74 protected StAXBuilder(OMFactory ombuilderFactory, XMLStreamReader parser) { 75 this.parser = parser; 76 omfactory = ombuilderFactory; 77 } 78 79 84 protected StAXBuilder(XMLStreamReader parser) { 85 this(OMAbstractFactory.getOMFactory(), parser); 86 } 87 88 93 public void setOmbuilderFactory(OMFactory ombuilderFactory) { 94 this.omfactory = ombuilderFactory; 95 } 96 97 103 protected abstract void processNamespaceData(OMElement node, 104 boolean isSOAPElement); 105 106 109 114 protected void processAttributes(OMElement node) { 115 int attribCount = parser.getAttributeCount(); 116 for (int i = 0; i < attribCount; i++) { 117 OMNamespace ns = null; 118 String uri = parser.getAttributeNamespace(i); 119 if (uri.hashCode() != 0) { 120 ns = node.findNamespace(uri, 121 parser.getAttributePrefix(i)); 122 } 123 124 node.addAttribute(parser.getAttributeLocalName(i), 127 parser.getAttributeValue(i), ns); 128 } 129 } 130 131 137 protected OMNode createOMText() throws OMException { 138 if (lastNode == null) { 139 throw new OMException(); 140 } 141 OMNode node; 142 if (lastNode.isComplete()) { 143 node = omfactory.createText((OMElement)lastNode.getParent(), parser.getText()); 144 lastNode.setNextSibling(node); 145 node.setPreviousSibling(lastNode); 146 } else { 147 OMElement e = (OMElement) lastNode; 148 node = omfactory.createText(e, parser.getText()); 149 e.setFirstChild(node); 150 } 151 return node; 152 } 153 154 160 public void reset(OMNode node) throws OMException { 161 lastNode = null; 162 } 163 164 170 public void discard(OMElement el) throws OMException { 171 OMElementImpl elementImpl = null; 172 if (el instanceof OMElementImpl) { 173 elementImpl = (OMElementImpl) el; 174 } else { 175 throw new OMException(); 176 } 177 if (elementImpl.isComplete() || !cache) { 178 throw new OMException(); 179 } 180 try { 181 cache = false; 182 do { 183 while (parser.next() != XMLStreamConstants.END_ELEMENT) ; 184 185 } while (!parser.getName().equals(elementImpl.getQName())); 187 lastNode = (OMNodeImpl) elementImpl.getPreviousSibling(); 188 if (lastNode != null) { 189 lastNode.setNextSibling(null); 190 } else { 191 OMElement parent = (OMElement)elementImpl.getParent(); 192 if (parent == null) { 193 throw new OMException(); 194 } 195 parent.setFirstChild(null); 196 lastNode = parent; 197 } 198 cache = true; 199 } catch (OMException e) { 200 throw e; 201 } catch (Exception e) { 202 throw new OMException(e); 203 } 204 } 205 206 212 public String getText() throws OMException { 213 return parser.getText(); 214 } 215 216 222 public String getNamespace() throws OMException { 223 return parser.getNamespaceURI(); 224 } 225 226 232 public int getNamespaceCount() throws OMException { 233 try { 234 return parser.getNamespaceCount(); 235 } catch (Exception e) { 236 throw new OMException(e); 237 } 238 } 239 240 247 public String getNamespacePrefix(int index) throws OMException { 248 try { 249 return parser.getNamespacePrefix(index); 250 } catch (Exception e) { 251 throw new OMException(e); 252 } 253 } 254 255 262 public String getNamespaceUri(int index) throws OMException { 263 try { 264 return parser.getNamespaceURI(index); 265 } catch (Exception e) { 266 throw new OMException(e); 267 } 268 } 269 270 275 public void setCache(boolean b) { 276 if (parserAccessed && b) { 277 throw new UnsupportedOperationException ( 278 "parser accessed. cannot set cache"); 279 } 280 cache = b; 281 } 282 283 289 public String getName() throws OMException { 290 return parser.getLocalName(); 291 } 292 293 299 public String getPrefix() throws OMException { 300 return parser.getPrefix(); 301 } 302 303 309 public int getAttributeCount() throws OMException { 310 return parser.getAttributeCount(); 311 } 312 313 320 public String getAttributeNamespace(int arg) throws OMException { 321 return parser.getAttributeNamespace(arg); 322 } 323 324 331 public String getAttributeName(int arg) throws OMException { 332 return parser.getAttributeNamespace(arg); 333 } 334 335 342 public String getAttributePrefix(int arg) throws OMException { 343 return parser.getAttributeNamespace(arg); 344 } 345 346 351 public Object getParser() { 352 if (!cache) { 353 parserAccessed = true; 354 return parser; 355 } else { 356 throw new UnsupportedOperationException ( 357 "cache must be switched off to access the parser"); 358 } 359 } 360 361 366 public boolean isCompleted() { 367 return done; 368 } 369 370 376 protected abstract OMNode createOMElement() throws OMException; 377 378 388 public abstract int next() throws OMException; 389 390 393 public short getBuilderType() { 394 return OMConstants.PULL_TYPE_BUILDER; 395 } 396 397 402 public void registerExternalContentHandler(Object obj) { 403 throw new UnsupportedOperationException (); 404 } 405 406 411 public Object getRegisteredContentHandler() { 412 throw new UnsupportedOperationException (); 413 } 414 } 415 | Popular Tags |