1 17 package org.apache.ws.jaxme.util; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.xml.XMLConstants ; 23 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.EntityReference ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.ProcessingInstruction ; 29 import org.w3c.dom.Text ; 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.ContentHandler ; 32 import org.xml.sax.Locator ; 33 import org.xml.sax.SAXException ; 34 35 40 public class DOMBuilder implements ContentHandler { 41 private Document document; 42 private Node target; 43 private Node currentNode; 44 private Locator locator; 45 private boolean prefixMappingIsAttribute; 46 private List prefixes; 47 48 52 public boolean isPrefixMappingIsAttribute() { 53 return prefixMappingIsAttribute; 54 } 55 56 60 public void setPrefixMappingIsAttribute(boolean pPrefixMappingIsAttribute) { 61 prefixMappingIsAttribute = pPrefixMappingIsAttribute; 62 } 63 64 66 public void setDocument(Document pDocument) { 67 document = pDocument; 68 } 69 70 72 public Document getDocument() { 73 return document; 74 } 75 76 78 public void setDocumentLocator(Locator pLocator) { 79 locator = pLocator; 80 } 81 82 84 public Locator getDocumentLocator() { 85 return locator; 86 } 87 88 91 public void setTarget(Node pNode) { 92 target = pNode; 93 currentNode = pNode; 94 if (getDocument() == null) { 95 setDocument(pNode.getNodeType() == Node.DOCUMENT_NODE ? 96 (Document ) pNode : pNode.getOwnerDocument()); 97 } 98 } 99 100 103 public Node getTarget() { 104 return target; 105 } 106 107 110 public void startDocument() throws SAXException { 111 } 112 113 116 public void endDocument() throws SAXException { 117 } 118 119 public void startPrefixMapping(String prefix, String uri) 120 throws SAXException { 121 if (isPrefixMappingIsAttribute()) { 122 if (prefixes == null) { 123 prefixes = new ArrayList (); 124 } 125 prefixes.add(prefix); 126 prefixes.add(uri); 127 } 128 } 129 130 public void endPrefixMapping(String prefix) throws SAXException { 131 } 132 133 public void startElement(String pNamespaceURI, String pLocalName, 134 String pQName, Attributes pAttr) throws SAXException { 135 Document doc = getDocument(); 136 Element element; 137 if (pNamespaceURI == null || pNamespaceURI.length() == 0) { 138 element = doc.createElement(pQName); 139 } else { 140 element = doc.createElementNS(pNamespaceURI, pQName); 141 } 142 if (pAttr != null) { 143 for (int i = 0; i < pAttr.getLength(); i++) { 144 String uri = pAttr.getURI(i); 145 String qName = pAttr.getQName(i); 146 String value = pAttr.getValue(i); 147 if (uri == null || uri.length() == 0) { 148 element.setAttribute(qName, value); 149 } else { 150 element.setAttributeNS(uri, qName, value); 151 } 152 } 153 } 154 if (prefixes != null) { 155 for (int i = 0; i < prefixes.size(); i += 2) { 156 String prefix = (String ) prefixes.get(i); 157 String uri = (String ) prefixes.get(i+1); 158 if (prefix == null || "".equals(prefix)) { 159 element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, 160 XMLConstants.XMLNS_ATTRIBUTE, uri); 161 } else { 162 element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, 163 XMLConstants.XMLNS_ATTRIBUTE + ':' + prefix, uri); 164 } 165 } 166 prefixes.clear(); 167 } 168 currentNode.appendChild(element); 169 currentNode = element; 170 } 171 172 public void endElement(String namespaceURI, String localName, String qName) 173 throws SAXException { 174 currentNode = currentNode.getParentNode(); 175 } 176 177 public void characters(char[] ch, int start, int length) 178 throws SAXException { 179 Node node = currentNode.getLastChild(); 180 String s = new String (ch, start, length); 181 if (node != null && node.getNodeType() == Node.TEXT_NODE) { 182 ((Text ) node).appendData(s); 183 } else { 184 Text text = getDocument().createTextNode(s); 185 currentNode.appendChild(text); 186 } 187 } 188 189 public void ignorableWhitespace(char[] ch, int start, int length) 190 throws SAXException { 191 characters(ch, start, length); 192 } 193 194 public void processingInstruction(String pTarget, String pData) 195 throws SAXException { 196 ProcessingInstruction pi = getDocument().createProcessingInstruction(pTarget, pData); 197 currentNode.appendChild(pi); 198 } 199 200 public void skippedEntity(String pName) throws SAXException { 201 EntityReference entity = getDocument().createEntityReference(pName); 202 currentNode.appendChild(entity); 203 } 204 } 205 | Popular Tags |