1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.StringReader ; 14 import java.util.Stack ; 15 16 import javax.xml.parsers.DocumentBuilderFactory ; 17 import javax.xml.parsers.ParserConfigurationException ; 18 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.Text ; 22 import org.xml.sax.Attributes ; 23 import org.xml.sax.InputSource ; 24 import org.xml.sax.Locator ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.DefaultHandler ; 27 28 public class XMLDefaultHandler extends DefaultHandler { 29 30 private org.w3c.dom.Document fDocument; 31 private Element fRootElement; 32 33 protected Stack fElementStack = new Stack (); 34 protected boolean fAbbreviated; 35 36 public XMLDefaultHandler() { 37 } 38 39 public XMLDefaultHandler(boolean abbreviated) { 40 fAbbreviated = abbreviated; 41 } 42 43 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 44 if (!isPrepared()) 45 return; 46 Element element = fDocument.createElement(qName); 47 for (int i = 0; i < attributes.getLength(); i++) { 48 element.setAttribute(attributes.getQName(i), attributes.getValue(i)); 49 } 50 51 if (fRootElement == null) 52 fRootElement = element; 53 else 54 ((Element )fElementStack.peek()).appendChild(element); 55 fElementStack.push(element); 56 } 57 58 public void endElement(String uri, String localName, String qName) throws SAXException { 59 if (isPrepared() && !fElementStack.isEmpty()) 60 fElementStack.pop(); 61 } 62 63 66 public void setDocumentLocator(Locator locator) { 67 } 68 69 72 public void startDocument() throws SAXException { 73 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 74 try { 75 fDocument = factory.newDocumentBuilder().newDocument(); 76 } catch (ParserConfigurationException e) { 77 } 78 } 79 80 83 public void endDocument() throws SAXException { 84 if (isPrepared()) 85 fDocument.appendChild(fRootElement); 86 } 87 88 91 public void processingInstruction(String target, String data) throws SAXException { 92 if (isPrepared()) 93 fDocument.appendChild(fDocument.createProcessingInstruction(target, data)); 94 } 95 96 99 public void characters(char[] characters, int start, int length) throws SAXException { 100 if (fAbbreviated || !isPrepared()) 101 return; 102 StringBuffer buff = new StringBuffer (); 103 for (int i = 0; i < length; i++) { 104 buff.append(characters[start + i]); 105 } 106 Text text = fDocument.createTextNode(buff.toString()); 107 if (fRootElement == null) 108 fDocument.appendChild(text); 109 else 110 ((Element )fElementStack.peek()).appendChild(text); 111 } 112 113 public Node getDocumentElement() { 114 if (!isPrepared()) 115 return null; 116 normalizeDocumentElement(); 117 return fDocument.getDocumentElement(); 118 } 119 120 public org.w3c.dom.Document getDocument() { 121 if (!isPrepared()) 122 return null; 123 normalizeDocumentElement(); 124 return fDocument; 125 } 126 127 public boolean isPrepared() { 128 return fDocument != null; 129 } 130 131 private void normalizeDocumentElement() { 132 if (fDocument.getDocumentElement() != null) 133 fDocument.getDocumentElement().normalize(); 134 } 135 136 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 137 return new InputSource (new StringReader ("")); } 142 143 } 144 | Popular Tags |