1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.StringReader ; 14 import java.util.Stack ; 15 16 import org.eclipse.pde.internal.core.util.IdUtil; 17 import org.w3c.dom.Element ; 18 import org.xml.sax.Attributes ; 19 import org.xml.sax.InputSource ; 20 import org.xml.sax.Locator ; 21 import org.xml.sax.SAXException ; 22 import org.xml.sax.helpers.DefaultHandler ; 23 24 public class ExtensionsHandler extends DefaultHandler { 25 26 private Stack fOpenElements; 27 28 private Locator fLocator; 29 30 private Element fParent; 31 32 public ExtensionsHandler(Element parent) { 33 fParent = parent; 34 } 35 36 42 public void processingInstruction(String target, String data) 43 throws SAXException { 44 if ("eclipse".equals(target)) { fParent.setAttribute("schema", "version=\"3.0\"".equals(data) ? "3.0" : "3.2"); } 47 } 48 49 55 public void startElement(String uri, String localName, String qName, 56 Attributes attributes) throws SAXException { 57 if (fOpenElements == null) { 58 if ((qName.equals("plugin") || qName.equals("fragment"))) { fOpenElements = new Stack (); 60 } 61 } else if (fOpenElements.size() == 0) { 62 if (qName.equals("extension")) { createExtension(attributes); 64 } else if (qName.equals("extension-point")) { createExtensionPoint(attributes); 66 } 67 } else { 68 createElement(qName, attributes); 69 } 70 } 71 72 75 private void createExtension(Attributes attributes) { 76 Element extension = fParent.getOwnerDocument().createElement("extension"); String point = attributes.getValue("point"); if (point == null) 79 return; 80 extension.setAttribute("point", point); 82 String id = attributes.getValue("id"); if (id != null) 84 extension.setAttribute("id", id); 86 String name = attributes.getValue("name"); if (name != null) 88 extension.setAttribute("name", name); 90 extension.setAttribute("line", Integer.toString(fLocator.getLineNumber())); 92 fParent.appendChild(extension); 93 94 if (IdUtil.isInterestingExtensionPoint(point)) 95 fOpenElements.push(extension); 96 } 97 98 101 private void createExtensionPoint(Attributes attributes) { 102 Element extPoint = fParent.getOwnerDocument().createElement("extension-point"); 104 String id = attributes.getValue("id"); if (id == null) 106 return; 107 extPoint.setAttribute("id", id); 109 String name = attributes.getValue("name"); if (name == null) 111 return; 112 extPoint.setAttribute("name", name); 114 String schema = attributes.getValue("schema"); if (schema != null) 116 extPoint.setAttribute("schema", schema); 118 extPoint.setAttribute("line", Integer.toString(fLocator.getLineNumber())); 120 fParent.appendChild(extPoint); 121 } 122 123 private void createElement(String tagName, Attributes attributes) { 124 Element element = fParent.getOwnerDocument().createElement(tagName); 125 for (int i = 0; i < attributes.getLength(); i++) { 126 element.setAttribute(attributes.getQName(i), attributes.getValue(i)); 127 } 128 ((Element )fOpenElements.peek()).appendChild(element); 129 fOpenElements.push(element); 130 } 131 132 138 public void endElement(String uri, String localName, String qName) 139 throws SAXException { 140 if (fOpenElements != null && !fOpenElements.isEmpty()) 141 fOpenElements.pop(); 142 } 143 144 149 public void setDocumentLocator(Locator locator) { 150 fLocator = locator; 151 } 152 153 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 154 return new InputSource (new StringReader ("")); } 159 160 } 161 | Popular Tags |