1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*******************************************************************************
* Copyright (c) 2003, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.internal.core;
import java.util.Stack;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLDefaultHandler extends DefaultHandler {
private org.w3c.dom.Document fDocument;
private Element fRootElement;
private Stack fElementStack = new Stack();
private boolean fAbbreviated;
public XMLDefaultHandler() {
}
public XMLDefaultHandler(boolean abbreviated) {
fAbbreviated = abbreviated;
}
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
Element element = fDocument.createElement(qName);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttribute(attributes.getQName(i), attributes.getValue(i));
}
if (fRootElement == null)
fRootElement = element;
else
((Element)fElementStack.peek()).appendChild(element);
fElementStack.push(element);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
fElementStack.pop();
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
*/
public void setDocumentLocator(Locator locator) {
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startDocument()
*/
public void startDocument() throws SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
fDocument = factory.newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
}
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#endDocument()
*/
public void endDocument() throws SAXException {
fDocument.appendChild(fRootElement);
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String)
*/
public void processingInstruction(String target, String data) throws SAXException {
fDocument.appendChild(fDocument.createProcessingInstruction(target, data));
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
*/
public void characters(char[] characters, int start, int length) throws SAXException {
if (fAbbreviated)
return;
StringBuffer buff = new StringBuffer();
for (int i = 0; i < length; i++) {
buff.append(characters[start + i]);
}
Text text = fDocument.createTextNode(buff.toString());
if (fRootElement == null)
fDocument.appendChild(text);
else
((Element)fElementStack.peek()).appendChild(text);
}
public Node getDocumentElement() {
fDocument.getDocumentElement().normalize();
return fDocument.getDocumentElement();
}
public org.w3c.dom.Document getDocument() {
fDocument.getDocumentElement().normalize();
return fDocument;
}
}
|