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.ui.tests.macro;
011:
012: import java.util.Hashtable;
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.Locator;
023: import org.xml.sax.SAXException;
024: import org.xml.sax.helpers.DefaultHandler;
025:
026: public class XMLDefaultHandler extends DefaultHandler {
027:
028: private org.w3c.dom.Document fDocument;
029:
030: private Locator fLocator;
031:
032: private Hashtable fLineTable;
033:
034: private Element fRootElement;
035:
036: private Stack fElementStack = new Stack();
037:
038: public XMLDefaultHandler() {
039: fLineTable = new Hashtable();
040: }
041:
042: public void startElement(String uri, String localName,
043: String qName, Attributes attributes) throws SAXException {
044: Element element = fDocument.createElement(qName);
045: for (int i = 0; i < attributes.getLength(); i++) {
046: element.setAttribute(attributes.getQName(i), attributes
047: .getValue(i));
048: }
049:
050: Integer lineNumber = new Integer(fLocator.getLineNumber());
051: Integer[] range = new Integer[] { lineNumber, new Integer(-1) };
052: fLineTable.put(element, range);
053: if (fRootElement == null)
054: fRootElement = element;
055: else
056: ((Element) fElementStack.peek()).appendChild(element);
057: fElementStack.push(element);
058: }
059:
060: public void endElement(String uri, String localName, String qName)
061: throws SAXException {
062: Integer[] range = (Integer[]) fLineTable.get(fElementStack
063: .pop());
064: range[1] = new Integer(fLocator.getLineNumber());
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator)
071: */
072: public void setDocumentLocator(Locator locator) {
073: fLocator = locator;
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.xml.sax.helpers.DefaultHandler#startDocument()
080: */
081: public void startDocument() throws SAXException {
082: DocumentBuilderFactory factory = DocumentBuilderFactory
083: .newInstance();
084: try {
085: fDocument = factory.newDocumentBuilder().newDocument();
086: } catch (ParserConfigurationException e) {
087: }
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.xml.sax.helpers.DefaultHandler#endDocument()
094: */
095: public void endDocument() throws SAXException {
096: fDocument.appendChild(fRootElement);
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String,
103: * java.lang.String)
104: */
105: public void processingInstruction(String target, String data)
106: throws SAXException {
107: fDocument.appendChild(fDocument.createProcessingInstruction(
108: target, data));
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
115: */
116: public void characters(char[] characters, int start, int length)
117: throws SAXException {
118: StringBuffer buff = new StringBuffer();
119: for (int i = 0; i < length; i++) {
120: buff.append(characters[start + i]);
121: }
122: Text text = fDocument.createTextNode(buff.toString());
123: if (fRootElement == null)
124: fDocument.appendChild(text);
125: else
126: ((Element) fElementStack.peek()).appendChild(text);
127: }
128:
129: public Node getDocumentElement() {
130: fDocument.getDocumentElement().normalize();
131: return fDocument.getDocumentElement();
132: }
133:
134: public org.w3c.dom.Document getDocument() {
135: fDocument.getDocumentElement().normalize();
136: return fDocument;
137: }
138:
139: public Hashtable getLineTable() {
140: return fLineTable;
141: }
142: }
|