1 11 package org.eclipse.pde.internal.core.plugin; 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.eclipse.pde.internal.core.util.IdUtil; 20 import org.w3c.dom.DOMException ; 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.Node ; 24 import org.w3c.dom.Text ; 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.Locator ; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.helpers.DefaultHandler ; 30 31 public class PluginHandler extends DefaultHandler { 32 private Document fDocument; 33 private Element fRootElement; 34 private Stack fOpenElements = new Stack (); 35 36 private String fSchemaVersion; 37 private boolean fAbbreviated; 38 private Locator fLocator; 39 private boolean fPop; 40 41 public PluginHandler(boolean abbreviated) { 42 fAbbreviated = abbreviated; 43 } 44 45 public void startElement(String uri, String localName, String qName, Attributes attributes) 46 throws SAXException { 47 48 fPop = true; 49 50 if (fAbbreviated && fOpenElements.size() == 2) { 51 Element parent = (Element )fOpenElements.peek(); 52 if (parent.getNodeName().equals("extension") && !isInterestingExtension((Element )fOpenElements.peek())) { fPop = false; 54 return; 55 } 56 } 57 58 Element element = fDocument.createElement(qName); 59 for (int i = 0; i < attributes.getLength(); i++) { 60 element.setAttribute(attributes.getQName(i), attributes.getValue(i)); 61 if ("extension".equals(qName) || "extension-point".equals(qName)) { element.setAttribute("line", Integer.toString(fLocator.getLineNumber())); } 64 } 65 66 if (fRootElement == null) 67 fRootElement = element; 68 else 69 ((Element )fOpenElements.peek()).appendChild(element); 70 71 fOpenElements.push(element); 72 } 73 74 protected boolean isInterestingExtension(Element element) { 75 String point = element.getAttribute("point"); return IdUtil.isInterestingExtensionPoint(point); 77 } 78 79 public void endElement(String uri, String localName, String qName) throws SAXException { 80 if (fPop || (qName.equals("extension") && fOpenElements.size() == 2)) { fOpenElements.pop(); 82 } 83 } 84 85 88 public void setDocumentLocator(Locator locator) { 89 fLocator = locator; 90 } 91 92 95 public void startDocument() throws SAXException { 96 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 97 try { 98 fDocument = factory.newDocumentBuilder().newDocument(); 99 } catch (ParserConfigurationException e) { 100 } 101 } 102 103 106 public void endDocument() throws SAXException { 107 fDocument.appendChild(fRootElement); 108 } 109 110 113 public void processingInstruction(String target, String data) throws SAXException { 114 if ("eclipse".equals(target)) { fSchemaVersion = "version=\"3.0\"".equals(data) ? "3.0" : "3.2"; } 117 } 118 119 122 public void characters(char[] characters, int start, int length) throws SAXException { 123 if (fAbbreviated) 124 return; 125 126 processCharacters(characters, start, length); 127 } 128 129 135 protected void processCharacters(char[] characters, int start, int length) 136 throws DOMException { 137 StringBuffer buff = new StringBuffer (); 138 for (int i = 0; i < length; i++) { 139 buff.append(characters[start + i]); 140 } 141 Text text = fDocument.createTextNode(buff.toString()); 142 if (fRootElement == null) 143 fDocument.appendChild(text); 144 else 145 ((Element )fOpenElements.peek()).appendChild(text); 146 } 147 148 public Node getDocumentElement() { 149 if (fRootElement != null) { 150 fRootElement.normalize(); 151 } 152 return fRootElement; 153 } 154 155 public String getSchemaVersion() { 156 return fSchemaVersion; 157 } 158 159 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 160 return new InputSource (new StringReader ("")); } 165 166 } 167 | Popular Tags |