1 17 package org.apache.ws.jaxme.xs.parser; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.ParserConfigurationException ; 22 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.EntityReference ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.ProcessingInstruction ; 28 import org.w3c.dom.Text ; 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.Locator ; 32 import org.xml.sax.SAXException ; 33 34 35 40 public class DOMBuilder implements ContentHandler { 41 private Node currentNode; 42 private Element result; 43 private Locator locator; 44 private Document factory; 45 46 50 public DOMBuilder() throws ParserConfigurationException { 51 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 52 dbf.setValidating(false); 53 dbf.setNamespaceAware(true); 54 DocumentBuilder db = dbf.newDocumentBuilder(); 55 currentNode = factory = db.newDocument(); 56 } 57 58 62 public DOMBuilder(Node pTarget) { 63 if (pTarget == null) { 64 throw new NullPointerException ("Target node must not be null."); 65 } 66 factory = pTarget.getOwnerDocument(); 67 currentNode = pTarget; 68 } 69 70 74 public Document getDocument() { 75 return factory; 76 } 77 78 82 public Element getResult() { 83 return result; 84 } 85 86 88 public void setDocumentLocator(Locator pLocator) { 89 locator = pLocator; 90 } 91 92 94 public Locator getDocumentLocator() { 95 return locator; 96 } 97 98 101 public void startDocument() throws SAXException { 102 } 103 104 107 public void endDocument() throws SAXException { 108 } 109 110 public void startPrefixMapping(String prefix, String uri) 111 throws SAXException { 112 } 113 114 public void endPrefixMapping(String prefix) throws SAXException { 115 } 116 117 public void startElement(String pNamespaceURI, String pLocalName, 118 String pQName, Attributes pAttr) throws SAXException { 119 Document doc = getDocument(); 120 Element element; 121 if (pNamespaceURI == null || pNamespaceURI.length() == 0) { 122 element = doc.createElement(pQName); 123 } else { 124 element = doc.createElementNS(pNamespaceURI, pQName); 125 } 126 if (pAttr != null) { 127 for (int i = 0; i < pAttr.getLength(); i++) { 128 String uri = pAttr.getURI(i); 129 String qName = pAttr.getQName(i); 130 String value = pAttr.getValue(i); 131 if (uri == null || uri.length() == 0) { 132 element.setAttribute(qName, value); 133 } else { 134 element.setAttributeNS(uri, qName, value); 135 } 136 } 137 } 138 139 currentNode.appendChild(element); 140 currentNode = element; 141 if (result == null) { 142 result = element; 143 } 144 } 145 146 public void endElement(String namespaceURI, String localName, String qName) 147 throws SAXException { 148 currentNode = currentNode.getParentNode(); 149 } 150 151 public void characters(char[] ch, int start, int length) 152 throws SAXException { 153 Node node = currentNode.getLastChild(); 154 String s = new String (ch, start, length); 155 if (node != null && node.getNodeType() == Node.TEXT_NODE) { 156 ((Text ) node).appendData(s); 157 } else { 158 Text text = getDocument().createTextNode(s); 159 currentNode.appendChild(text); 160 } 161 } 162 163 public void ignorableWhitespace(char[] ch, int start, int length) 164 throws SAXException { 165 characters(ch, start, length); 166 } 167 168 public void processingInstruction(String pTarget, String pData) 169 throws SAXException { 170 ProcessingInstruction pi = getDocument().createProcessingInstruction(pTarget, pData); 171 currentNode.appendChild(pi); 172 } 173 174 public void skippedEntity(String pName) throws SAXException { 175 EntityReference entity = getDocument().createEntityReference(pName); 176 currentNode.appendChild(entity); 177 } 178 } 179 | Popular Tags |