Index: src/gnu/gcjwebplugin/AppletTag.java =================================================================== RCS file: /sources/gcjwebplugin/gcjwebplugin/src/gnu/gcjwebplugin/AppletTag.java,v retrieving revision 1.30 diff -u -r1.30 AppletTag.java --- src/gnu/gcjwebplugin/AppletTag.java 19 Apr 2006 18:40:06 -0000 1.30 +++ src/gnu/gcjwebplugin/AppletTag.java 19 Apr 2006 21:45:39 -0000 @@ -21,23 +21,14 @@ package gnu.gcjwebplugin; -import gnu.javax.swing.text.html.parser.HTML_401F; - -import gnu.xml.dom.DomNode; import gnu.xml.dom.html2.DomHTMLAppletElement; -import gnu.xml.dom.html2.DomHTMLDocument; import gnu.xml.dom.html2.DomHTMLEmbedElement; import gnu.xml.dom.html2.DomHTMLObjectElement; -import gnu.xml.dom.html2.DomHTMLParamElement; -import gnu.xml.dom.html2.DomHTMLParser; import java.awt.Dimension; import java.awt.Toolkit; import java.io.File; -import java.io.InputStreamReader; -import java.io.IOException; -import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; @@ -48,31 +39,17 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Locale; -import java.util.StringTokenizer; -import java.util.Vector; - -import org.w3c.dom.NodeList; class AppletTag { /** - * Parsed document. - */ - DomHTMLDocument document; - - /** * The document base of this applet. */ URL documentbase; /** - * The document base of all the applets. - */ - static URL db; - - /** * name of applet tag. */ String name = ""; @@ -96,11 +73,6 @@ * The parameters. */ HashMap parameters = new HashMap(); - - /** - * The Elements that may be in the document. - */ - Vector tags = new Vector(); /** * The screen size. @@ -114,40 +86,13 @@ { // Do nothing. } - - /** - * Constructs and parses document using the given location. - * - * @param location - location of applet - */ - AppletTag(String location) throws IOException - { - documentbase = getLocationToURL(location); - db = documentbase; - InputStreamReader in = new InputStreamReader(documentbase.openStream()); - document = (DomHTMLDocument) (new DomHTMLParser(HTML_401F.getInstance()).parseDocument(in)); - } - - /** - * Constructs and parses document. - * - * @param in - Reader to parse document from. - * @param documentBase - the URL of the applet - * @throws IOException - is thrown if any IO error occurs. - */ - AppletTag(Reader in, URL documentBase) throws IOException - { - documentbase = documentBase; - db = documentbase; - document = (DomHTMLDocument) (new DomHTMLParser(HTML_401F.getInstance()).parseDocument(in)); - } /** * Constructs an AppletTag and parses the given applet element. * * @param appElement - the Applet element to parse. */ - private AppletTag(DomHTMLAppletElement appElement) + AppletTag(DomHTMLAppletElement appElement) { name = appElement.getName(); parameters.put("name", name); @@ -160,7 +105,7 @@ parameters.put("vspace", Integer.toString(appElement.getVspace())); parameters.put("width", appElement.getWidth()); - parseParams(appElement); + TagParser.parseParams(appElement, this); if (code.equals("")) { @@ -178,7 +123,7 @@ } if (archives.size() == 0) - archives = parseArchives(appElement.getArchive()); + archives = TagParser.parseArchives(appElement.getArchive(), this); } /** @@ -186,7 +131,7 @@ * * @param embElement - the Embed element to parse. */ - private AppletTag(DomHTMLEmbedElement embElement) + AppletTag(DomHTMLEmbedElement embElement) { // In an EMBED tag, a parameter is any non-standard attribute. This // is a problem for applets that take parameters named "code", @@ -218,7 +163,7 @@ parameters.put("java_type", embElement.getJavaType()); parameters.put("vspace", Integer.toString(embElement.getVspace())); - parseParams(embElement); + TagParser.parseParams(embElement, this); // Must initialize codebase before archives if (codebase.equals("")) @@ -241,11 +186,11 @@ if (archives.size() == 0) { - ArrayList jarch = parseArchives(embElement.getJavaArchive()); + ArrayList jarch = TagParser.parseArchives(embElement.getJavaArchive(), this); if (jarch.size() != 0) archives = jarch; else - archives = parseArchives(embElement.getArchive()); + archives = TagParser.parseArchives(embElement.getArchive(), this); } } @@ -254,7 +199,7 @@ * * @param objElement - the Object element to parse. */ - private AppletTag(DomHTMLObjectElement objElement) + AppletTag(DomHTMLObjectElement objElement) { // In an OBJECT tag, a parameter is any non-standard attribute. This // is a problem for applets that take parameters named "code", @@ -291,7 +236,7 @@ parameters.put("mayscript", objElement.getMayscript()); parameters.put("scriptable", objElement.getScriptable()); - parseParams(objElement); + TagParser.parseParams(objElement, this); // Must initialize codebase before archives if (codebase.equals("")) @@ -314,150 +259,12 @@ if (archives.size() == 0) { - ArrayList jarch = parseArchives(objElement.getJavaArchive()); + ArrayList jarch = TagParser.parseArchives(objElement.getJavaArchive(), this); if (jarch.size() != 0) archives = jarch; else - archives = parseArchives(objElement.getArchive()); - } - } - - /** - * Parses all applet tags in document. - * - * @return a list of AppletTag objects representing the applet tags - * in document - */ - ArrayList parseAppletTags() - { - ArrayList allTags = new ArrayList(); - if (document == null) - return null;; - - recurseDocument(document.getChildNodes()); - - int sz = tags.size(); - for (int j = 0; j < sz; j++) - { - Object curr = tags.get(j); - // Order of checking is important here. - // Must check embed element before applet element - // because DomHTMLEmbedElement extends DomHTMLAppletElement - AppletTag a = null; - if (curr instanceof DomHTMLEmbedElement) - a = new AppletTag((DomHTMLEmbedElement) curr); - else if (curr instanceof DomHTMLAppletElement) - a = new AppletTag((DomHTMLAppletElement) curr); - else if (curr instanceof DomHTMLObjectElement) - a = new AppletTag((DomHTMLObjectElement) curr); - a.documentbase = documentbase; - allTags.add(a); - } - - return allTags; - } - - /** - * Recurses the document in search for the appropriate tags. - * - * @param list - the Node list. - */ - private void recurseDocument(NodeList list) - { - // Recurse and store all APPLET, OBJECT and EMBED tags. - int length = list.getLength(); - for (int i = 0; i < length; i++) - { - DomNode curr = (DomNode) list.item(i); - if ((curr instanceof DomHTMLEmbedElement) || - (curr instanceof DomHTMLAppletElement) || - (curr instanceof DomHTMLObjectElement)) - tags.add(curr); - recurseDocument(curr.getChildNodes()); - } - } - - /** - * Parses the param elements for a given node. - * - * @param node - the node element to parse. - */ - private void parseParams(DomNode node) - { - boolean ja = false; - boolean jb = false; - boolean jc = false; - NodeList l = node.getChildNodes(); - int size = l.getLength(); - - if (size != 0) - for (int i = 0; i < size; i++) - { - Object c = l.item(i); - if (! (c instanceof DomHTMLParamElement)) - continue; - DomHTMLParamElement curr = (DomHTMLParamElement) c; - String key = curr.getName(); - String val = curr.getValue(); - - if (key.equals("java_code")) - { - jc = true; - code = val; - } - else if (key.equals("java_codebase")) - { - jb = true; - codebase = val; - } - else if (!jc && key.equals("code")) - code = val; - else if (!jc && key.equals("classid")) - { - int x = val.indexOf(":"); - if (x != -1) - val = val.substring(x + 1); - code = val; - } - else if (!jb && key.equals("codebase")) - codebase = val; - else if (key.equals("java_archive")) - { - ja = true; - archives = parseArchives(val); - val = archives.toString(); - } - else if (!ja && key.equals("archive")) - { - archives = parseArchives(val); - val = archives.toString(); - } - - parameters.put(key, val); - } - } - - /** - * Parses the archive string and returns a list. - * - * @param the list of archives (comma-separated) in a String. - */ - private ArrayList parseArchives(String arcs) - { - try - { - ArrayList list = new ArrayList(); - - StringTokenizer tagTokenizer = new StringTokenizer(arcs, ","); - while (tagTokenizer.hasMoreTokens()) - list.add(prependCodeBase(tagTokenizer.nextToken().trim())); - - return list; - } - catch (MalformedURLException e) - { + archives = TagParser.parseArchives(objElement.getArchive(), this); } - return null; } /** @@ -473,99 +280,6 @@ } /** - * Prepends the base to the codebase. - * - * @return the new URL. - */ - URL prependCodeBase(String base) throws MalformedURLException - { - if (documentbase == null) - documentbase = db; - - URL fullcodebase; - - //If no codebase was specified, default to documentbase. - if (codebase.equals("")) - { - if (documentbase.getFile().endsWith(File.separator)) - fullcodebase = documentbase; - else - { - String dirname = documentbase.getFile(); - - // Determine dirname for file by strippint everything - // past the last file separator. - dirname = dirname.substring(0, - dirname.lastIndexOf(File.separatorChar) + 1); - - fullcodebase = new URL(documentbase.getProtocol(), - documentbase.getHost(), - documentbase.getPort(), dirname); - } - } - else - { - // codebase was specified. - URL codebaseURL = new URL(documentbase, codebase); - - if ("file".equals(codebaseURL.getProtocol())) - { - if (new File(codebaseURL.getFile()).isDirectory() && !codebase.endsWith(File.separator)) - fullcodebase = new URL(documentbase, codebase + File.separator); - else - fullcodebase = new URL(documentbase, codebase); - } - else if (codebase.endsWith(File.separator)) - fullcodebase = new URL(documentbase, codebase); - else - fullcodebase = new URL(documentbase, codebase + File.separator); - } - - return new URL(fullcodebase, base); - } - - /** - * Gets the location to the URL, given a location. - * - * @param location - the given location. - * @return the URL. - */ - static URL getLocationToURL(String location) throws IOException - { - URL tmpDocumentBase = null; - - try - { - // Try parsing location as a URL. - tmpDocumentBase = new URL(location); - - // If no file was specified in the URL the assume the user - // meant the root page. - String f = tmpDocumentBase.getFile(); - if (f.indexOf(".") == -1 && !f.endsWith(File.separator)) - tmpDocumentBase = new URL(location.concat(File.separator)); - } - catch (MalformedURLException e) - { - // location is not a URL. See if it is an HTML file. - String path; - - if (location.startsWith(File.separator)) - path = new File(location).getCanonicalPath(); - else - path = new File(System.getProperty("user.dir") + File.separator - + location).getCanonicalPath(); - - tmpDocumentBase = new URL("file", "", path); - - if (new File(tmpDocumentBase.getFile()).isDirectory()) - tmpDocumentBase = new URL("file", "", path + File.separator); - } - - return tmpDocumentBase; - } - - /** * Returns the size of the applet. * * @return the size. @@ -667,4 +381,56 @@ { return (String) parameters.get(name.toLowerCase()); } + + /** + * Prepends the base to the codebase. + * + * @return the new URL. + */ + URL prependCodeBase(String base) throws MalformedURLException + { + if (documentbase == null) + documentbase = TagParser.db; + + URL fullcodebase; + + //If no codebase was specified, default to documentbase. + if (codebase.equals("")) + { + if (documentbase.getFile().endsWith(File.separator)) + fullcodebase = documentbase; + else + { + String dirname = documentbase.getFile(); + + // Determine dirname for file by strippint everything + // past the last file separator. + dirname = dirname.substring(0, + dirname.lastIndexOf(File.separatorChar) + 1); + + fullcodebase = new URL(documentbase.getProtocol(), + documentbase.getHost(), + documentbase.getPort(), dirname); + } + } + else + { + // codebase was specified. + URL codebaseURL = new URL(documentbase, codebase); + + if ("file".equals(codebaseURL.getProtocol())) + { + if (new File(codebaseURL.getFile()).isDirectory() && !codebase.endsWith(File.separator)) + fullcodebase = new URL(documentbase, codebase + File.separator); + else + fullcodebase = new URL(documentbase, codebase); + } + else if (codebase.endsWith(File.separator)) + fullcodebase = new URL(documentbase, codebase); + else + fullcodebase = new URL(documentbase, codebase + File.separator); + } + + return new URL(fullcodebase, base); + } } Index: src/gnu/gcjwebplugin/PluginAppletViewer.java =================================================================== RCS file: /sources/gcjwebplugin/gcjwebplugin/src/gnu/gcjwebplugin/PluginAppletViewer.java,v retrieving revision 1.27 diff -u -r1.27 PluginAppletViewer.java --- src/gnu/gcjwebplugin/PluginAppletViewer.java 19 Apr 2006 18:40:06 -0000 1.27 +++ src/gnu/gcjwebplugin/PluginAppletViewer.java 19 Apr 2006 21:45:39 -0000 @@ -80,7 +80,7 @@ int pos = message.indexOf(' ', 4); String documentbase = message.substring(4, pos); String tag = message.substring(pos + 1); - currentWindow.setTag(tag, documentbase); + currentWindow.setParser(tag, documentbase); } else if (message.startsWith("handle")) { Index: src/gnu/gcjwebplugin/PluginAppletWindow.java =================================================================== RCS file: /sources/gcjwebplugin/gcjwebplugin/src/gnu/gcjwebplugin/PluginAppletWindow.java,v retrieving revision 1.30 diff -u -r1.30 PluginAppletWindow.java --- src/gnu/gcjwebplugin/PluginAppletWindow.java 19 Apr 2006 18:40:06 -0000 1.30 +++ src/gnu/gcjwebplugin/PluginAppletWindow.java 19 Apr 2006 21:45:39 -0000 @@ -61,6 +61,7 @@ private static HashMap contexts = new HashMap(); private Applet applet; + private TagParser parser; private AppletTag tag; PluginAppletWindow() @@ -378,11 +379,11 @@ } } - void setTag(String tag, String documentbase) throws MalformedURLException, IOException + void setParser(String tag, String documentbase) throws MalformedURLException, IOException { - URL documentbaseURL = AppletTag.getLocationToURL(documentbase); + URL documentbaseURL = TagParser.getLocationToURL(documentbase); StringReader in = new StringReader(tag); - this.tag = new AppletTag(in, documentbaseURL); + this.parser = new TagParser(in, documentbaseURL); } // ///////////////////////////////// @@ -399,7 +400,7 @@ super.setHandle(handle); addNotify(); - ArrayList l = tag.parseAppletTags(); + ArrayList l = parser.parseAppletTags(); int s = l.size(); for (int i = 0; i < s; i++) Index: src/gnu/gcjwebplugin/StandaloneAppletViewer.java =================================================================== RCS file: /sources/gcjwebplugin/gcjwebplugin/src/gnu/gcjwebplugin/StandaloneAppletViewer.java,v retrieving revision 1.14 diff -u -r1.14 StandaloneAppletViewer.java --- src/gnu/gcjwebplugin/StandaloneAppletViewer.java 19 Apr 2006 18:40:06 -0000 1.14 +++ src/gnu/gcjwebplugin/StandaloneAppletViewer.java 19 Apr 2006 21:45:39 -0000 @@ -48,8 +48,8 @@ // Handle each file specified on the command line. for (int i = 0; i < urls.length; i++) { - AppletTag tag = new AppletTag(urls[i]); - appletTags.addAll(tag.parseAppletTags()); + TagParser parser = new TagParser(urls[i]); + appletTags.addAll(parser.parseAppletTags()); } printTags(); @@ -89,9 +89,9 @@ StringReader reader = new StringReader(tagString); String path = System.getProperty("user.dir") + File.separator; - AppletTag tag = new AppletTag(reader, + TagParser parser = new TagParser(reader, new URL("file", "", path)); - appletTags.addAll(tag.parseAppletTags()); + appletTags.addAll(parser.parseAppletTags()); printTags(); createWindows(); Index: src/gnu/gcjwebplugin/TagParser.java =================================================================== RCS file: src/gnu/gcjwebplugin/TagParser.java diff -N src/gnu/gcjwebplugin/TagParser.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/gnu/gcjwebplugin/TagParser.java 19 Apr 2006 21:45:39 -0000 @@ -0,0 +1,284 @@ +/* AppletTag.java - representation of an HTML APPLET tag + Copyright (C) 2006 Lillian Angel
+ + This file is part of GCJ Applet Viewer. + + GCJ Applet Viewer is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + GCJ Applet Viewer is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied warranty + of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GCJ Applet Viewer; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +package gnu.gcjwebplugin; + +import gnu.javax.swing.text.html.parser.HTML_401F; + +import gnu.xml.dom.DomNode; +import gnu.xml.dom.html2.DomHTMLAppletElement; +import gnu.xml.dom.html2.DomHTMLDocument; +import gnu.xml.dom.html2.DomHTMLEmbedElement; +import gnu.xml.dom.html2.DomHTMLObjectElement; +import gnu.xml.dom.html2.DomHTMLParamElement; +import gnu.xml.dom.html2.DomHTMLParser; + +import java.io.File; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.Reader; + +import java.net.MalformedURLException; +import java.net.URL; + +import java.util.ArrayList; +import java.util.StringTokenizer; +import java.util.Vector; + +import org.w3c.dom.NodeList; + + +public class TagParser +{ + + /** + * Parsed document. + */ + DomHTMLDocument document; + + /** + * The document base of this applet. + */ + URL documentbase; + + /** + * The document base of all the applets. + */ + static URL db; + + /** + * The tags in the document. + */ + Vector tags = new Vector(); + + /** + * Default constructor. + */ + TagParser() + { + // Do nothing. + } + + /** + * Constructs and parses document using the given location. + * + * @param location - location of applet + */ + TagParser(String location) throws IOException + { + documentbase = getLocationToURL(location); + db = documentbase; + InputStreamReader in = new InputStreamReader(documentbase.openStream()); + document = (DomHTMLDocument) (new DomHTMLParser(HTML_401F.getInstance()).parseDocument(in)); + } + + /** + * Constructs and parses document. + * + * @param in - Reader to parse document from. + * @param documentBase - the URL of the applet + * @throws IOException - is thrown if any IO error occurs. + */ + TagParser(Reader in, URL documentBase) throws IOException + { + documentbase = documentBase; + db = documentbase; + document = (DomHTMLDocument) (new DomHTMLParser(HTML_401F.getInstance()).parseDocument(in)); + } + + /** + * Parses all applet tags in document. + * + * @return a list of AppletTag objects representing the applet tags + * in document + */ + ArrayList parseAppletTags() + { + ArrayList allTags = new ArrayList(); + if (document == null) + return null;; + + recurseDocument(document.getChildNodes()); + + int sz = tags.size(); + for (int j = 0; j < sz; j++) + { + Object curr = tags.get(j); + // Order of checking is important here. + // Must check embed element before applet element + // because DomHTMLEmbedElement extends DomHTMLAppletElement + AppletTag a = null; + if (curr instanceof DomHTMLEmbedElement) + a = new AppletTag((DomHTMLEmbedElement) curr); + else if (curr instanceof DomHTMLAppletElement) + a = new AppletTag((DomHTMLAppletElement) curr); + else if (curr instanceof DomHTMLObjectElement) + a = new AppletTag((DomHTMLObjectElement) curr); + a.documentbase = documentbase; + allTags.add(a); + } + + return allTags; + } + + /** + * Recurses the document in search for the appropriate tags. + * + * @param list - the Node list. + */ + private void recurseDocument(NodeList list) + { + // Recurse and store all APPLET, OBJECT and EMBED tags. + int length = list.getLength(); + for (int i = 0; i < length; i++) + { + DomNode curr = (DomNode) list.item(i); + if ((curr instanceof DomHTMLEmbedElement) || + (curr instanceof DomHTMLAppletElement) || + (curr instanceof DomHTMLObjectElement)) + tags.add(curr); + recurseDocument(curr.getChildNodes()); + } + } + + /** + * Parses the param elements for a given node. + * + * @param node - the node element to parse. + */ + static void parseParams(DomNode node, AppletTag t) + { + boolean ja = false; + boolean jb = false; + boolean jc = false; + NodeList l = node.getChildNodes(); + int size = l.getLength(); + + if (size != 0) + for (int i = 0; i < size; i++) + { + Object c = l.item(i); + if (! (c instanceof DomHTMLParamElement)) + continue; + DomHTMLParamElement curr = (DomHTMLParamElement) c; + String key = curr.getName(); + String val = curr.getValue(); + + if (key.equals("java_code")) + { + jc = true; + t.code = val; + } + else if (key.equals("java_codebase")) + { + jb = true; + t.codebase = val; + } + else if (!jc && key.equals("code")) + t.code = val; + else if (!jc && key.equals("classid")) + { + int x = val.indexOf(":"); + if (x != -1) + val = val.substring(x + 1); + t.code = val; + } + else if (!jb && key.equals("codebase")) + t.codebase = val; + else if (key.equals("java_archive")) + { + ja = true; + t.archives = parseArchives(val, t); + val = t.archives.toString(); + } + else if (!ja && key.equals("archive")) + { + t.archives = parseArchives(val, t); + val = t.archives.toString(); + } + + t.parameters.put(key, val); + } + } + + /** + * Parses the archive string and returns a list. + * + * @param the list of archives (comma-separated) in a String. + */ + static ArrayList parseArchives(String arcs, AppletTag t) + { + try + { + ArrayList list = new ArrayList(); + + StringTokenizer tagTokenizer = new StringTokenizer(arcs, ","); + while (tagTokenizer.hasMoreTokens()) + list.add(t.prependCodeBase(tagTokenizer.nextToken().trim())); + + return list; + } + catch (MalformedURLException e) + { + } + return null; + } + + /** + * Gets the location to the URL, given a location. + * + * @param location - the given location. + * @return the URL. + */ + static URL getLocationToURL(String location) throws IOException + { + URL tmpDocumentBase = null; + + try + { + // Try parsing location as a URL. + tmpDocumentBase = new URL(location); + + // If no file was specified in the URL the assume the user + // meant the root page. + String f = tmpDocumentBase.getFile(); + if (f.indexOf(".") == -1 && !f.endsWith(File.separator)) + tmpDocumentBase = new URL(location.concat(File.separator)); + } + catch (MalformedURLException e) + { + // location is not a URL. See if it is an HTML file. + String path; + + if (location.startsWith(File.separator)) + path = new File(location).getCanonicalPath(); + else + path = new File(System.getProperty("user.dir") + File.separator + + location).getCanonicalPath(); + + tmpDocumentBase = new URL("file", "", path); + + if (new File(tmpDocumentBase.getFile()).isDirectory()) + tmpDocumentBase = new URL("file", "", path + File.separator); + } + + return tmpDocumentBase; + } +}