001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.jumpimpl.module.download;
026:
027: import java.io.FileInputStream;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030:
031: import java.net.URL;
032: import java.net.URLConnection;
033:
034: import java.util.Vector;
035:
036: import org.xml.sax.Attributes;
037: import org.xml.sax.InputSource;
038: import org.xml.sax.SAXException;
039: import org.xml.sax.helpers.DefaultHandler;
040: import javax.xml.parsers.SAXParserFactory;
041: import javax.xml.parsers.SAXParser;
042:
043: public class Parser extends DefaultHandler {
044:
045: static SAXParserFactory spf;
046:
047: private SAXParser parser;
048: private Document document;
049: private Vector currentElement;
050: private URL url;
051: boolean fastForward = false;
052:
053: public Document getDocument() {
054: return document;
055: }
056:
057: public void parse(URL url) throws Exception {
058:
059: this .url = url;
060:
061: try {
062: InputStream is = null;
063:
064: if ((url.getProtocol()).startsWith("file")) {
065:
066: String fileName = url.getFile();
067: while (fileName.charAt(1) == '/') {
068: fileName = fileName.substring(1);
069: }
070: is = (InputStream) (new FileInputStream(fileName));
071:
072: } else {
073:
074: URLConnection uc = url.openConnection();
075: is = uc.getInputStream();
076:
077: }
078:
079: parse(is);
080:
081: } catch (Exception e) {
082: // e.printStackTrace();
083: throw e;
084: }
085:
086: return;
087: }
088:
089: public void parse(InputStream is) throws Exception {
090: this .parser = spf.newSAXParser();
091: parser.parse(new InputSource(is), this );
092: return;
093: }
094:
095: // Called once at the beginning of a document
096: public void startDocument() {
097: currentElement = new Vector();
098: return;
099: }
100:
101: public void endDocument() {
102:
103: if (currentElement.size() != 0) {
104: if (DownloadModuleFactoryImpl.verbose) {
105: System.out
106: .println("bogosity! document end with extra elements!");
107: System.out.println("currentElement size is "
108: + currentElement.size());
109: }
110: }
111: return;
112: }
113:
114: /**
115: * Receive notification of the start of an element.
116: * @param uri - The Namespace URI, or the empty string
117: * if the element has no Namespace URI or
118: * if Namespace processing is not being
119: * performed.
120: * @param localName - The local name (without prefix), or the
121: * empty string if Namespace processing is not
122: * being performed.
123: * @param qName - The qualified name (with prefix), or the empty
124: * string if qualified names are not available.
125: * @param atts - The attributes attached to the element. If there
126: * are no attributes, it shall be an empty
127: * Attributes object.
128: * @throws SAXException - Any SAX exception, possibly wrapping
129: * another exception.
130: */
131: public void startElement(String uri, String localName,
132: String qName, Attributes attr) throws SAXException {
133:
134: if (DownloadModuleFactoryImpl.verbose) {
135: System.out.println("startEl : uri=" + uri + ", localname="
136: + localName + ", qName=" + qName);
137: }
138: DocumentElement de = null;
139: DocumentElement parent = null;
140:
141: // The first element we find should be the document
142: if (currentElement.size() == 0) {
143: de = new DocumentElement(qName, true, null);
144: document = new Document(qName, de);
145: for (int i = 0; i < attr.getLength(); i++) {
146: if (DownloadModuleFactoryImpl.verbose) {
147: System.out.println("\tattribute: "
148: + attr.getQName(i) + ", value: "
149: + attr.getValue(i));
150: }
151: de.addAttribute(attr.getQName(i), attr.getValue(i));
152: }
153:
154: } else {
155:
156: // We're inside the document. Create a new element
157: parent = (DocumentElement) currentElement
158: .elementAt(currentElement.size() - 1);
159: de = new DocumentElement(qName, false, parent);
160: for (int i = 0; i < attr.getLength(); i++) {
161: if (DownloadModuleFactoryImpl.verbose) {
162: System.out.println("\tattribute: "
163: + attr.getQName(i) + ", value: "
164: + attr.getValue(i));
165: }
166: de.addAttribute(attr.getQName(i), attr.getValue(i));
167: }
168: }
169: // Add the new element to our vectors
170: if (parent != null) {
171: parent.addElement(de);
172: }
173: currentElement.add(de);
174: return;
175: }
176:
177: public void characters(char[] ch, int start, int length)
178: throws SAXException {
179: if (fastForward) {
180: return;
181: }
182:
183: ((DocumentElement) currentElement.elementAt(currentElement
184: .size() - 1)).addCharacters(ch, start, length);
185:
186: if (DownloadModuleFactoryImpl.verbose) {
187: System.out.println("\tcharacters "
188: + (new String(ch, start, length)).trim());
189: }
190: return;
191: }
192:
193: public void endElement(String namespaceURI, String localName,
194: String qName) throws SAXException {
195: if (DownloadModuleFactoryImpl.verbose) {
196: System.out.println("endEl : uri=" + namespaceURI
197: + ", localname=" + localName + ", qName=" + qName);
198: }
199: currentElement.removeElementAt(currentElement.size() - 1);
200: return;
201: }
202:
203: static {
204: try {
205: spf = SAXParserFactory.newInstance();
206: //spf.setNamespaceAware( true );
207: spf.setValidating(false);
208: } catch (Exception e) {
209: e.printStackTrace();
210: }
211: }
212:
213: }
|