001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.StringReader;
013: import java.util.Stack;
014:
015: import javax.xml.parsers.DocumentBuilderFactory;
016: import javax.xml.parsers.ParserConfigurationException;
017:
018: import org.w3c.dom.Element;
019: import org.w3c.dom.Node;
020: import org.w3c.dom.Text;
021: import org.xml.sax.Attributes;
022: import org.xml.sax.InputSource;
023: import org.xml.sax.Locator;
024: import org.xml.sax.SAXException;
025: import org.xml.sax.helpers.DefaultHandler;
026:
027: public class XMLDefaultHandler extends DefaultHandler {
028:
029: private org.w3c.dom.Document fDocument;
030: private Element fRootElement;
031:
032: protected Stack fElementStack = new Stack();
033: protected boolean fAbbreviated;
034:
035: public XMLDefaultHandler() {
036: }
037:
038: public XMLDefaultHandler(boolean abbreviated) {
039: fAbbreviated = abbreviated;
040: }
041:
042: public void startElement(String uri, String localName,
043: String qName, Attributes attributes) throws SAXException {
044: if (!isPrepared())
045: return;
046: Element element = fDocument.createElement(qName);
047: for (int i = 0; i < attributes.getLength(); i++) {
048: element.setAttribute(attributes.getQName(i), attributes
049: .getValue(i));
050: }
051:
052: if (fRootElement == null)
053: fRootElement = element;
054: else
055: ((Element) fElementStack.peek()).appendChild(element);
056: fElementStack.push(element);
057: }
058:
059: public void endElement(String uri, String localName, String qName)
060: throws SAXException {
061: if (isPrepared() && !fElementStack.isEmpty())
062: fElementStack.pop();
063: }
064:
065: /* (non-Javadoc)
066: * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
067: */
068: public void setDocumentLocator(Locator locator) {
069: }
070:
071: /* (non-Javadoc)
072: * @see org.xml.sax.helpers.DefaultHandler#startDocument()
073: */
074: public void startDocument() throws SAXException {
075: DocumentBuilderFactory factory = DocumentBuilderFactory
076: .newInstance();
077: try {
078: fDocument = factory.newDocumentBuilder().newDocument();
079: } catch (ParserConfigurationException e) {
080: }
081: }
082:
083: /* (non-Javadoc)
084: * @see org.xml.sax.helpers.DefaultHandler#endDocument()
085: */
086: public void endDocument() throws SAXException {
087: if (isPrepared())
088: fDocument.appendChild(fRootElement);
089: }
090:
091: /* (non-Javadoc)
092: * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String)
093: */
094: public void processingInstruction(String target, String data)
095: throws SAXException {
096: if (isPrepared())
097: fDocument.appendChild(fDocument
098: .createProcessingInstruction(target, data));
099: }
100:
101: /* (non-Javadoc)
102: * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
103: */
104: public void characters(char[] characters, int start, int length)
105: throws SAXException {
106: if (fAbbreviated || !isPrepared())
107: return;
108: StringBuffer buff = new StringBuffer();
109: for (int i = 0; i < length; i++) {
110: buff.append(characters[start + i]);
111: }
112: Text text = fDocument.createTextNode(buff.toString());
113: if (fRootElement == null)
114: fDocument.appendChild(text);
115: else
116: ((Element) fElementStack.peek()).appendChild(text);
117: }
118:
119: public Node getDocumentElement() {
120: if (!isPrepared())
121: return null;
122: normalizeDocumentElement();
123: return fDocument.getDocumentElement();
124: }
125:
126: public org.w3c.dom.Document getDocument() {
127: if (!isPrepared())
128: return null;
129: normalizeDocumentElement();
130: return fDocument;
131: }
132:
133: public boolean isPrepared() {
134: return fDocument != null;
135: }
136:
137: private void normalizeDocumentElement() {
138: if (fDocument.getDocumentElement() != null)
139: fDocument.getDocumentElement().normalize();
140: }
141:
142: public InputSource resolveEntity(String publicId, String systemId)
143: throws SAXException {
144: // Prevent the resolution of external entities in order to
145: // prevent the parser from accessing the Internet
146: // This will prevent huge workbench performance degradations and hangs
147: return new InputSource(new StringReader("")); //$NON-NLS-1$
148: }
149:
150: }
|