1 16 17 package org.apache.axis2.om.impl.llom.builder; 18 19 import org.apache.axis2.om.*; 20 import org.xml.sax.Attributes ; 21 import org.xml.sax.Locator ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.helpers.DefaultHandler ; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 public class SAXOMBuilder extends DefaultHandler { 29 OMElement root = null; 30 31 OMNode lastNode = null; 32 33 OMElement nextElem = null; 34 35 OMFactory factory = OMAbstractFactory.getOMFactory(); 36 37 List prefixMappings = new ArrayList (); 38 39 public void setDocumentLocator(Locator arg0) { 40 } 41 42 public void startDocument() throws SAXException { 43 44 } 45 46 public void endDocument() throws SAXException { 47 } 48 49 protected OMElement createNextElement(String localName) throws OMException { 50 OMElement e; 51 if (lastNode == null) { 52 root = e = factory.createOMElement(localName, null, null, null); 53 } else if (lastNode.isComplete()) { 54 e = factory.createOMElement(localName, null, lastNode.getParent(), 55 null); 56 lastNode.setNextSibling(e); 57 e.setPreviousSibling(lastNode); 58 } else { 59 OMElement parent = (OMElement) lastNode; 60 e = factory.createOMElement(localName, null, (OMElement) lastNode, 61 null); 62 parent.setFirstChild(e); 63 } 64 return e; 65 } 66 67 73 public void startPrefixMapping(String prefix, String uri) 74 throws SAXException { 75 if (nextElem == null) 76 nextElem = createNextElement(null); 77 nextElem.declareNamespace(uri, prefix); 78 } 79 80 public void endPrefixMapping(String arg0) throws SAXException { 81 } 82 83 89 public void startElement(String namespaceURI, String localName, 90 String qName, Attributes atts) throws SAXException { 91 if (localName == null || localName.trim().equals("")) 92 localName = qName.substring(qName.indexOf(':') + 1); 93 if (nextElem == null) 94 nextElem = createNextElement(localName); 95 else 96 nextElem.setLocalName(localName); 97 nextElem 98 .setNamespace(nextElem.findNamespace(namespaceURI, null)); 99 int j = atts.getLength(); 100 for (int i = 0; i < j; i++) 101 nextElem.addAttribute(atts.getLocalName(i), atts.getValue(i), 102 nextElem.findNamespace(atts.getURI(i), null)); 103 lastNode = nextElem; 104 nextElem = null; 105 } 106 107 113 public void endElement(String arg0, String arg1, String arg2) 114 throws SAXException { 115 if (lastNode.isComplete()) { 116 OMContainer parent = lastNode.getParent(); 117 parent.setComplete(true); 118 lastNode = (OMNode)parent; 119 } else { 120 OMElement e = (OMElement) lastNode; 121 e.setComplete(true); 122 } 123 } 124 125 130 public void characters(char[] ch, int start, int length) 131 throws SAXException { 132 if (lastNode == null) { 133 throw new SAXException (""); 134 } 135 OMNode node; 136 if (lastNode.isComplete()) { 137 node = factory.createText((OMElement)lastNode.getParent(), new String (ch, 138 start, length)); 139 lastNode.setNextSibling(node); 140 node.setPreviousSibling(lastNode); 141 } else { 142 OMElement e = (OMElement) lastNode; 143 node = factory.createText(e, new String (ch, start, length)); 144 e.setFirstChild(node); 145 } 146 lastNode = node; 147 } 148 149 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) 150 throws SAXException { 151 } 152 153 public void processingInstruction(String arg0, String arg1) 154 throws SAXException { 155 } 156 157 public void skippedEntity(String arg0) throws SAXException { 158 } 159 160 163 public OMElement getRootElement() { 164 return root; 165 } 166 } 167 | Popular Tags |