001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.tools.appletviewer;
019:
020: import java.io.DataInputStream;
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.ArrayList;
027:
028: import javax.swing.text.ChangedCharSetException;
029: import javax.swing.text.SimpleAttributeSet;
030: import javax.swing.text.html.HTML;
031: import javax.swing.text.html.parser.DTD;
032: import javax.swing.text.html.parser.TagElement;
033:
034: class HTMLParser {
035:
036: private final ParserImpl parser;
037:
038: HTMLParser() throws IOException {
039: DTD dtd = DTD.getDTD("reader");
040: dtd.read(new DataInputStream(dtd.getClass()
041: .getResourceAsStream("html32.bdtd")));//transitional401.bdtd")));
042: parser = new ParserImpl(dtd);
043: }
044:
045: synchronized Object[] parse(String urls[], int start)
046: throws IOException {
047: ArrayList<AppletInfo> list = new ArrayList<AppletInfo>(
048: urls.length - start);
049: for (int i = start; i < urls.length; i++) {
050: parser.parse(urls[i], list);
051: }
052: return list.toArray();
053: }
054:
055: private class ParserImpl extends
056: javax.swing.text.html.parser.Parser {
057: private URL documentBase;
058: private ArrayList<AppletInfo> list;
059: private AppletInfo appletInfo = null;
060: private HTML.Tag startElement = null;
061:
062: public ParserImpl(DTD dtd) {
063: super (dtd);
064: }
065:
066: public void parse(String url, ArrayList<AppletInfo> list)
067: throws IOException {
068: try {
069: this .documentBase = new URL(url);
070: } catch (MalformedURLException _) {
071: File f = new File(url);
072: this .documentBase = f.toURL();
073: }
074: this .list = list;
075:
076: // Open the stream
077: InputStreamReader isr = null;
078: try {
079: isr = new InputStreamReader(documentBase.openStream());
080: } catch (IOException e) {
081: System.err.println("I/O exception while reading: "
082: + e.getMessage());
083: System.exit(-1);
084: }
085: parse(isr);
086: }
087:
088: @Override
089: protected void handleStartTag(TagElement tag) {
090: if (tag == null)
091: return;
092:
093: HTML.Tag htmlTag = tag.getHTMLTag();
094:
095: if (htmlTag == HTML.Tag.APPLET
096: || htmlTag == HTML.Tag.OBJECT) {
097:
098: if (startElement != null) {
099: throw new RuntimeException(htmlTag + " inside "
100: + startElement);
101: }
102:
103: startElement = htmlTag;
104: appletInfo = new AppletInfo();
105: appletInfo.setTag(htmlTag.toString());
106: list.add(appletInfo);
107:
108: appletInfo.setDocumentBase(documentBase);
109:
110: SimpleAttributeSet attributes = getAttributes();
111:
112: appletInfo.setParameter("WIDTH", (String) attributes
113: .getAttribute(HTML.Attribute.WIDTH));
114: appletInfo.setParameter("HEIGHT", (String) attributes
115: .getAttribute(HTML.Attribute.HEIGHT));
116: appletInfo.setParameter("CODE", (String) attributes
117: .getAttribute(HTML.Attribute.CODE));
118: appletInfo.setParameter("ARCHIVE", (String) attributes
119: .getAttribute(HTML.Attribute.ARCHIVE));
120:
121: if (htmlTag != HTML.Tag.OBJECT) {
122: appletInfo
123: .setParameter(
124: "CODEBASE",
125: (String) attributes
126: .getAttribute(HTML.Attribute.CODEBASE));
127: }
128:
129: }
130: }
131:
132: @Override
133: protected void handleEndTag(TagElement tag) {
134: if (tag != null && tag.getHTMLTag() == startElement)
135: startElement = null;
136: }
137:
138: @Override
139: protected void handleEmptyTag(TagElement tag)
140: throws ChangedCharSetException {
141:
142: HTML.Tag htmlTag = tag.getHTMLTag();
143: if (appletInfo != null && htmlTag == HTML.Tag.PARAM) {
144: SimpleAttributeSet attributes = getAttributes();
145: appletInfo.setParameter((String) attributes
146: .getAttribute(HTML.Attribute.NAME),
147: (String) attributes
148: .getAttribute(HTML.Attribute.VALUE));
149: }
150:
151: }
152: }
153: }
|