1 /* TagParser.java -- a parser for applet tags
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.classpath.tools.appletviewer;
40 import gnu.javax.swing.text.html.parser.HTML_401F;
42 import gnu.xml.dom.DomNode;
43 import gnu.xml.dom.html2.DomHTMLAppletElement;
44 import gnu.xml.dom.html2.DomHTMLDocument;
45 import gnu.xml.dom.html2.DomHTMLEmbedElement;
46 import gnu.xml.dom.html2.DomHTMLObjectElement;
47 import gnu.xml.dom.html2.DomHTMLParamElement;
48 import gnu.xml.dom.html2.DomHTMLParser;
51 import java.io.InputStreamReader;
52 import java.io.IOException;
53 import java.io.Reader;
55 import java.net.MalformedURLException;
58 import java.util.ArrayList;
59 import java.util.StringTokenizer;
60 import java.util.Vector;
62 import org.w3c.dom.NodeList;
65 public class TagParser
71 DomHTMLDocument document;
74 * The document base of this applet.
79 * The document base of all the applets.
84 * The tags in the document.
86 Vector tags = new Vector();
89 * Default constructor.
97 * Constructs and parses document using the given location.
99 * @param location - location of applet
101 TagParser(String location) throws IOException
103 documentbase = getLocationToURL(location);
105 InputStreamReader in = new InputStreamReader(documentbase.openStream());
106 document = (DomHTMLDocument) (new DomHTMLParser(HTML_401F.getInstance()).parseDocument(in));
110 * Constructs and parses document.
112 * @param in - Reader to parse document from.
113 * @param documentBase - the URL of the applet
114 * @throws IOException - is thrown if any IO error occurs.
116 TagParser(Reader in, URL documentBase) throws IOException
118 documentbase = documentBase;
120 document = (DomHTMLDocument) (new DomHTMLParser(HTML_401F.getInstance()).parseDocument(in));
124 * Parses all applet tags in document.
126 * @return a list of AppletTag objects representing the applet tags
129 ArrayList parseAppletTags()
131 ArrayList allTags = new ArrayList();
132 if (document == null)
135 recurseDocument(document.getChildNodes());
137 int sz = tags.size();
138 for (int j = 0; j < sz; j++)
140 Object curr = tags.get(j);
141 // Order of checking is important here.
142 // Must check embed element before applet element
143 // because DomHTMLEmbedElement extends DomHTMLAppletElement
145 if (curr instanceof DomHTMLEmbedElement)
146 a = new AppletTag((DomHTMLEmbedElement) curr);
147 else if (curr instanceof DomHTMLAppletElement)
148 a = new AppletTag((DomHTMLAppletElement) curr);
149 else if (curr instanceof DomHTMLObjectElement)
150 a = new AppletTag((DomHTMLObjectElement) curr);
151 a.documentbase = documentbase;
159 * Recurses the document in search for the appropriate tags.
161 * @param list - the Node list.
163 private void recurseDocument(NodeList list)
165 // Recurse and store all APPLET, OBJECT and EMBED tags.
166 int length = list.getLength();
167 for (int i = 0; i < length; i++)
169 DomNode curr = (DomNode) list.item(i);
170 if ((curr instanceof DomHTMLEmbedElement) ||
171 (curr instanceof DomHTMLAppletElement) ||
172 (curr instanceof DomHTMLObjectElement))
174 recurseDocument(curr.getChildNodes());
179 * Parses the param elements for a given node.
181 * @param node - the node element to parse.
183 static void parseParams(DomNode node, AppletTag t)
188 NodeList l = node.getChildNodes();
189 int size = l.getLength();
192 for (int i = 0; i < size; i++)
194 Object c = l.item(i);
195 if (! (c instanceof DomHTMLParamElement))
197 DomHTMLParamElement curr = (DomHTMLParamElement) c;
198 String key = curr.getName();
199 String val = curr.getValue();
201 if (key.equals("java_code"))
206 else if (key.equals("java_codebase"))
211 else if (!jc && key.equals("code"))
213 else if (!jc && key.equals("classid"))
215 int x = val.indexOf(":");
217 val = val.substring(x + 1);
220 else if (!jb && key.equals("codebase"))
222 else if (key.equals("java_archive"))
225 t.archives = parseArchives(val, t);
226 val = t.archives.toString();
228 else if (!ja && key.equals("archive"))
230 t.archives = parseArchives(val, t);
231 val = t.archives.toString();
233 val = unescapeString(val);
234 t.parameters.put(key.toLowerCase(), val);
239 * This method does the same thing as the g_strcompress function in glib.
242 * @return value in its original one-byte equivalence.
244 private static String unescapeString(String value)
246 String unescVal = "";
247 for (int i = 0; i < value.length(); i++)
249 if (i == value.length() - 1)
251 unescVal = unescVal.concat(value.substring(i));
254 if (value.charAt(i) == '\\')
256 switch (value.charAt(i + 1))
259 unescVal = unescVal.concat("\b");
262 unescVal = unescVal.concat("\f");
265 unescVal = unescVal.concat("\n");
268 unescVal = unescVal.concat("\r");
271 unescVal = unescVal.concat("\t");
274 unescVal = unescVal.concat("\\");
277 unescVal = unescVal.concat("\"");
280 unescVal = unescVal.concat("\\");
281 unescVal = unescVal.concat(value.substring(i + 1, i + 2));
287 unescVal = unescVal.concat(value.substring(i, i + 1));
293 * Parses the archive string and returns a list.
295 * @param the list of archives (comma-separated) in a String.
297 static ArrayList parseArchives(String arcs, AppletTag t)
301 ArrayList list = new ArrayList();
303 StringTokenizer tagTokenizer = new StringTokenizer(arcs, ",");
304 while (tagTokenizer.hasMoreTokens())
305 list.add(t.prependCodeBase(tagTokenizer.nextToken().trim()));
309 catch (MalformedURLException e)
316 * Gets the location to the URL, given a location.
318 * @param location - the given location.
321 static URL getLocationToURL(String location) throws IOException
323 URL tmpDocumentBase = null;
327 // Try parsing location as a URL.
328 tmpDocumentBase = new URL(location);
330 // If no file was specified in the URL the assume the user
331 // meant the root page.
332 String f = tmpDocumentBase.getFile();
333 if (f.indexOf(".") == -1 && !f.endsWith(File.separator))
334 if (new File(tmpDocumentBase.getFile()).isDirectory())
335 tmpDocumentBase = new URL(location.concat(File.separator));
337 catch (MalformedURLException e)
339 // location is not a URL. See if it is an HTML file.
342 if (location.startsWith(File.separator))
343 path = new File(location).getCanonicalPath();
345 path = new File(System.getProperty("user.dir") + File.separator
346 + location).getCanonicalPath();
348 tmpDocumentBase = new URL("file", "", path);
350 if (new File(tmpDocumentBase.getFile()).isDirectory())
351 tmpDocumentBase = new URL("file", "", path + File.separator);
354 return tmpDocumentBase;