KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > ExtensionsHandler


1 /*******************************************************************************
2  * Copyright (c) 2003, 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;
12
13 import java.io.StringReader JavaDoc;
14 import java.util.Stack JavaDoc;
15
16 import org.eclipse.pde.internal.core.util.IdUtil;
17 import org.w3c.dom.Element JavaDoc;
18 import org.xml.sax.Attributes JavaDoc;
19 import org.xml.sax.InputSource JavaDoc;
20 import org.xml.sax.Locator JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.helpers.DefaultHandler JavaDoc;
23
24 public class ExtensionsHandler extends DefaultHandler JavaDoc {
25     
26     private Stack JavaDoc fOpenElements;
27
28     private Locator JavaDoc fLocator;
29
30     private Element JavaDoc fParent;
31
32     public ExtensionsHandler(Element JavaDoc parent) {
33         fParent = parent;
34     }
35
36     /*
37      * (non-Javadoc)
38      *
39      * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String,
40      * java.lang.String)
41      */

42     public void processingInstruction(String JavaDoc target, String JavaDoc data)
43             throws SAXException JavaDoc {
44         if ("eclipse".equals(target)) { //$NON-NLS-1$
45
fParent.setAttribute("schema", "version=\"3.0\"".equals(data) ? "3.0" : "3.2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
46
}
47     }
48     
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
53      * java.lang.String, java.lang.String, org.xml.sax.Attributes)
54      */

55     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
56             Attributes JavaDoc attributes) throws SAXException JavaDoc {
57         if (fOpenElements == null) {
58             if ((qName.equals("plugin") || qName.equals("fragment"))) { //$NON-NLS-1$ //$NON-NLS-2$
59
fOpenElements = new Stack JavaDoc();
60             }
61         } else if (fOpenElements.size() == 0) {
62             if (qName.equals("extension")) { //$NON-NLS-1$
63
createExtension(attributes);
64             } else if (qName.equals("extension-point")) { //$NON-NLS-1$
65
createExtensionPoint(attributes);
66             }
67         } else {
68             createElement(qName, attributes);
69         }
70     }
71
72     /**
73      * @param attributes
74      */

75     private void createExtension(Attributes JavaDoc attributes) {
76         Element JavaDoc extension = fParent.getOwnerDocument().createElement("extension"); //$NON-NLS-1$
77
String JavaDoc point = attributes.getValue("point"); //$NON-NLS-1$
78
if (point == null)
79             return;
80         extension.setAttribute("point", point); //$NON-NLS-1$
81

82         String JavaDoc id = attributes.getValue("id"); //$NON-NLS-1$
83
if (id != null)
84             extension.setAttribute("id", id); //$NON-NLS-1$
85

86         String JavaDoc name = attributes.getValue("name"); //$NON-NLS-1$
87
if (name != null)
88             extension.setAttribute("name", name); //$NON-NLS-1$
89

90         extension.setAttribute("line", Integer.toString(fLocator.getLineNumber())); //$NON-NLS-1$
91

92         fParent.appendChild(extension);
93         
94         if (IdUtil.isInterestingExtensionPoint(point))
95             fOpenElements.push(extension);
96     }
97
98     /**
99      * @param attributes
100      */

101     private void createExtensionPoint(Attributes JavaDoc attributes) {
102         Element JavaDoc extPoint = fParent.getOwnerDocument().createElement("extension-point"); //$NON-NLS-1$
103

104         String JavaDoc id = attributes.getValue("id"); //$NON-NLS-1$
105
if (id == null)
106             return;
107         extPoint.setAttribute("id", id); //$NON-NLS-1$
108

109         String JavaDoc name = attributes.getValue("name"); //$NON-NLS-1$
110
if (name == null)
111             return;
112         extPoint.setAttribute("name", name); //$NON-NLS-1$
113

114         String JavaDoc schema = attributes.getValue("schema"); //$NON-NLS-1$
115
if (schema != null)
116             extPoint.setAttribute("schema", schema); //$NON-NLS-1$
117

118         extPoint.setAttribute("line", Integer.toString(fLocator.getLineNumber())); //$NON-NLS-1$
119

120         fParent.appendChild(extPoint);
121     }
122
123     private void createElement(String JavaDoc tagName, Attributes JavaDoc attributes) {
124         Element JavaDoc 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 JavaDoc)fOpenElements.peek()).appendChild(element);
129         fOpenElements.push(element);
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
136      * java.lang.String, java.lang.String)
137      */

138     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
139             throws SAXException JavaDoc {
140         if (fOpenElements != null && !fOpenElements.isEmpty())
141             fOpenElements.pop();
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
148      */

149     public void setDocumentLocator(Locator JavaDoc locator) {
150         fLocator = locator;
151     }
152
153     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
154         // Prevent the resolution of external entities in order to
155
// prevent the parser from accessing the Internet
156
// This will prevent huge workbench performance degradations and hangs
157
return new InputSource JavaDoc(new StringReader JavaDoc("")); //$NON-NLS-1$
158
}
159     
160 }
161
Popular Tags