1 21 package oracle.toplink.essentials.platform.xml; 23 24 import java.util.Stack ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.Locator ; 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.ContentHandler ; 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.ProcessingInstruction ; 34 import org.w3c.dom.Text ; 35 36 public class SAXDocumentBuilder implements ContentHandler { 37 private Document document; 38 private Stack nodes = new Stack (); 39 private XMLPlatform xmlPlatform; 40 41 public SAXDocumentBuilder() { 42 super(); 43 nodes = new Stack (); 44 xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform(); 45 } 46 47 public Document getDocument() { 48 return document; 49 } 50 51 public Document getInitializedDocument() throws SAXException { 52 if (document == null) { 53 try { 54 document = xmlPlatform.createDocument(); 55 nodes.push(document); 56 } catch (Exception e) { 57 throw new SAXException (e); 58 } 59 } 60 return document; 61 } 62 63 public void setDocumentLocator(Locator locator) { 64 } 65 66 public void startDocument() throws SAXException { 67 try { 68 document = xmlPlatform.createDocument(); 69 nodes.push(document); 70 } catch (Exception e) { 71 throw new SAXException (e); 72 } 73 } 74 75 public void endDocument() throws SAXException { 76 nodes.pop(); 77 } 78 79 public void startPrefixMapping(String prefix, String uri) throws SAXException { 80 } 81 82 public void endPrefixMapping(String prefix) throws SAXException { 83 } 84 85 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 86 if ((null != namespaceURI) && ("".equals(namespaceURI))) { 87 namespaceURI = null; 88 } 89 Element element = getInitializedDocument().createElementNS(namespaceURI, qName); 90 Node parentNode = (Node )nodes.peek(); 91 parentNode.appendChild(element); 92 nodes.push(element); 93 94 int numberOfAttributes = atts.getLength(); 95 Attr attribute; 96 for (int x = 0; x < numberOfAttributes; x++) { 97 element.setAttributeNS(atts.getURI(x), atts.getQName(x), atts.getValue(x)); 98 } 99 } 100 101 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 102 nodes.pop(); 103 } 104 105 public void characters(char[] ch, int start, int length) throws SAXException { 106 String characters = new String (ch, start, length); 107 if (characters.trim().length() == 0) { 108 return; 109 } 110 Text text = getInitializedDocument().createTextNode(characters); 111 Node parentNode = (Node )nodes.peek(); 112 parentNode.appendChild(text); 113 } 114 115 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 116 } 117 118 public void processingInstruction(String target, String data) throws SAXException { 119 ProcessingInstruction pi = getInitializedDocument().createProcessingInstruction(target, data); 120 Node parentNode = (Node )nodes.peek(); 121 parentNode.appendChild(pi); 122 } 123 124 public void skippedEntity(String name) throws SAXException { 125 } 126 } 127 | Popular Tags |