KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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 javax.xml.parsers.DocumentBuilderFactory JavaDoc;
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.Text JavaDoc;
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.xml.sax.Locator JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.DefaultHandler JavaDoc;
27
28 public class XMLDefaultHandler extends DefaultHandler JavaDoc {
29     
30     private org.w3c.dom.Document JavaDoc fDocument;
31     private Element JavaDoc fRootElement;
32     
33     protected Stack JavaDoc fElementStack = new Stack JavaDoc();
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 JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
44         if (!isPrepared())
45             return;
46         Element JavaDoc 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 JavaDoc)fElementStack.peek()).appendChild(element);
55         fElementStack.push(element);
56     }
57     
58     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
59         if (isPrepared() && !fElementStack.isEmpty())
60             fElementStack.pop();
61     }
62     
63     /* (non-Javadoc)
64      * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
65      */

66     public void setDocumentLocator(Locator JavaDoc locator) {
67     }
68
69     /* (non-Javadoc)
70      * @see org.xml.sax.helpers.DefaultHandler#startDocument()
71      */

72     public void startDocument() throws SAXException JavaDoc {
73         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
74         try {
75             fDocument = factory.newDocumentBuilder().newDocument();
76         } catch (ParserConfigurationException JavaDoc e) {
77         }
78     }
79     
80     /* (non-Javadoc)
81      * @see org.xml.sax.helpers.DefaultHandler#endDocument()
82      */

83     public void endDocument() throws SAXException JavaDoc {
84         if (isPrepared())
85             fDocument.appendChild(fRootElement);
86     }
87     
88     /* (non-Javadoc)
89      * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String)
90      */

91     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
92         if (isPrepared())
93             fDocument.appendChild(fDocument.createProcessingInstruction(target, data));
94     }
95         
96     /* (non-Javadoc)
97      * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
98      */

99     public void characters(char[] characters, int start, int length) throws SAXException JavaDoc {
100         if (fAbbreviated || !isPrepared())
101             return;
102         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
103         for (int i = 0; i < length; i++) {
104             buff.append(characters[start + i]);
105         }
106         Text JavaDoc text = fDocument.createTextNode(buff.toString());
107         if (fRootElement == null)
108             fDocument.appendChild(text);
109         else
110             ((Element JavaDoc)fElementStack.peek()).appendChild(text);
111     }
112     
113     public Node JavaDoc getDocumentElement() {
114         if (!isPrepared())
115             return null;
116         normalizeDocumentElement();
117         return fDocument.getDocumentElement();
118     }
119     
120     public org.w3c.dom.Document JavaDoc 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 JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
137         // Prevent the resolution of external entities in order to
138
// prevent the parser from accessing the Internet
139
// This will prevent huge workbench performance degradations and hangs
140
return new InputSource JavaDoc(new StringReader JavaDoc("")); //$NON-NLS-1$
141
}
142     
143 }
144
Popular Tags