KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > plugin > PluginHandler


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.plugin;
12
13 import java.io.StringReader JavaDoc;
14 import java.util.Stack JavaDoc;
15
16 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18
19 import org.eclipse.pde.internal.core.util.IdUtil;
20 import org.w3c.dom.DOMException JavaDoc;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24 import org.w3c.dom.Text JavaDoc;
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.Locator JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.helpers.DefaultHandler JavaDoc;
30
31 public class PluginHandler extends DefaultHandler JavaDoc {
32     private Document JavaDoc fDocument;
33     private Element JavaDoc fRootElement;
34     private Stack JavaDoc fOpenElements = new Stack JavaDoc();
35     
36     private String JavaDoc fSchemaVersion;
37     private boolean fAbbreviated;
38     private Locator JavaDoc fLocator;
39     private boolean fPop;
40     
41     public PluginHandler(boolean abbreviated) {
42         fAbbreviated = abbreviated;
43     }
44     
45     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes)
46         throws SAXException JavaDoc {
47         
48         fPop = true;
49         
50         if (fAbbreviated && fOpenElements.size() == 2) {
51             Element JavaDoc parent = (Element JavaDoc)fOpenElements.peek();
52             if (parent.getNodeName().equals("extension") && !isInterestingExtension((Element JavaDoc)fOpenElements.peek())) { //$NON-NLS-1$
53
fPop = false;
54                 return;
55             }
56         }
57         
58         Element JavaDoc 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)) { //$NON-NLS-1$ //$NON-NLS-2$
62
element.setAttribute("line", Integer.toString(fLocator.getLineNumber())); //$NON-NLS-1$
63
}
64         }
65         
66         if (fRootElement == null)
67             fRootElement = element;
68         else
69             ((Element JavaDoc)fOpenElements.peek()).appendChild(element);
70         
71         fOpenElements.push(element);
72     }
73     
74     protected boolean isInterestingExtension(Element JavaDoc element) {
75         String JavaDoc point = element.getAttribute("point"); //$NON-NLS-1$
76
return IdUtil.isInterestingExtensionPoint(point);
77     }
78         
79     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
80         if (fPop || (qName.equals("extension") && fOpenElements.size() == 2)) { //$NON-NLS-1$
81
fOpenElements.pop();
82         }
83     }
84     
85     /* (non-Javadoc)
86      * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
87      */

88     public void setDocumentLocator(Locator JavaDoc locator) {
89         fLocator = locator;
90     }
91
92     /* (non-Javadoc)
93      * @see org.xml.sax.helpers.DefaultHandler#startDocument()
94      */

95     public void startDocument() throws SAXException JavaDoc {
96         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
97         try {
98             fDocument = factory.newDocumentBuilder().newDocument();
99         } catch (ParserConfigurationException JavaDoc e) {
100         }
101     }
102     
103     /* (non-Javadoc)
104      * @see org.xml.sax.helpers.DefaultHandler#endDocument()
105      */

106     public void endDocument() throws SAXException JavaDoc {
107         fDocument.appendChild(fRootElement);
108     }
109     
110     /* (non-Javadoc)
111      * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String)
112      */

113     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
114         if ("eclipse".equals(target)) { //$NON-NLS-1$
115
fSchemaVersion = "version=\"3.0\"".equals(data) ? "3.0" : "3.2"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
116
}
117     }
118         
119     /* (non-Javadoc)
120      * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
121      */

122     public void characters(char[] characters, int start, int length) throws SAXException JavaDoc {
123         if (fAbbreviated)
124             return;
125         
126         processCharacters(characters, start, length);
127     }
128
129     /**
130      * @param characters
131      * @param start
132      * @param length
133      * @throws DOMException
134      */

135     protected void processCharacters(char[] characters, int start, int length)
136             throws DOMException JavaDoc {
137         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
138         for (int i = 0; i < length; i++) {
139             buff.append(characters[start + i]);
140         }
141         Text JavaDoc text = fDocument.createTextNode(buff.toString());
142         if (fRootElement == null)
143             fDocument.appendChild(text);
144         else
145             ((Element JavaDoc)fOpenElements.peek()).appendChild(text);
146     }
147     
148     public Node JavaDoc getDocumentElement() {
149         if (fRootElement != null) {
150             fRootElement.normalize();
151         }
152         return fRootElement;
153     }
154     
155     public String JavaDoc getSchemaVersion() {
156         return fSchemaVersion;
157     }
158     
159     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
160         // Prevent the resolution of external entities in order to
161
// prevent the parser from accessing the Internet
162
// This will prevent huge workbench performance degradations and hangs
163
return new InputSource JavaDoc(new StringReader JavaDoc("")); //$NON-NLS-1$
164
}
165     
166 }
167
Popular Tags