📄 applettag.java
字号:
/* AppletTag.java - representation of an HTML APPLET tag Copyright (C) 2003, 2004, 2005 Thomas Fitzsimmons <fitzsim@redhat.com> 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 java.awt.Dimension;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StreamTokenizer;import java.net.MalformedURLException;import java.net.URL;import java.text.NumberFormat;import java.text.ParseException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.StringTokenizer;class AppletTag{ String name = ""; String code = ""; String codebase = ""; String archivesList = ""; ArrayList archives = new ArrayList(); HashMap parameters = new HashMap(); // documentbase is not specified in an applet tag but we keep it // here anyway, for convenience. URL documentbase; // Same goes for the HTML file's character encoding. String encoding; // Default constructor needed by derived classes. AppletTag() { } // Construct an applet tag object from a .class file and the user's // current directory. AppletTag(String code, String archives, List parameters, Dimension dimensions, String encoding) throws MalformedURLException, IOException { this.encoding = encoding; // Calculate code. this.code = code.substring(code.lastIndexOf(File.separatorChar) + 1, code.length()); // When a class file is given on the command line, codebase is // always set to the empty string and documentbase is the // directory in which the class file resides. String tmpCodeBase = code.substring(0, code.lastIndexOf(File.separatorChar) + 1); if (tmpCodeBase.startsWith(File.separator)) this.documentbase = new URL("file", "", tmpCodeBase); else // documentbase is the current working directory. this.documentbase = new URL("file", "", System.getProperty("user.dir") + File.separator + tmpCodeBase); archivesList = archives; // Parse archives. parseArchives(); // Parse parameters, a list of comma-delimited key-value pairs. Iterator pairs = parameters.iterator(); while (pairs.hasNext()) { StringTokenizer paramTokenizer = new StringTokenizer((String) pairs.next(), ","); this.parameters.put(paramTokenizer.nextToken().trim().toLowerCase(), paramTokenizer.nextToken().trim()); } this.parameters.put("width", Integer.toString(dimensions.width)); this.parameters.put("height", Integer.toString(dimensions.height)); } // Construct an applet tag object from an HTML stream. AppletTag(StreamTokenizer tagTokenizer, URL documentbase, String encoding) throws IOException { this.documentbase = documentbase; this.encoding = encoding; while (tagTokenizer.nextToken() != '>') { if (tagTokenizer.ttype == StreamTokenizer.TT_EOF) break; if (tagTokenizer.ttype == StreamTokenizer.TT_WORD) { if (tagTokenizer.sval.equals("name")) { name = parseAttributeString(tagTokenizer); this.parameters.put("name", name); } else if (tagTokenizer.sval.equals("code")) code = parseAttributeString(tagTokenizer); else if (tagTokenizer.sval.equals("codebase")) codebase = parseAttributeString(tagTokenizer); else if (tagTokenizer.sval.equals("archive")) archivesList = parseAttributeString(tagTokenizer); else if (tagTokenizer.sval.equals("width")) { tagTokenizer.nextToken(); tagTokenizer.nextToken(); if (tagTokenizer.ttype == StreamTokenizer.TT_NUMBER) this.parameters.put("width", Integer.toString((int) tagTokenizer.nval)); else this.parameters.put("width", tagTokenizer.sval); } else if (tagTokenizer.sval.equals("height")) { tagTokenizer.nextToken(); tagTokenizer.nextToken(); if (tagTokenizer.ttype == StreamTokenizer.TT_NUMBER) this.parameters.put("height", Integer.toString((int) tagTokenizer.nval)); else this.parameters.put("height", tagTokenizer.sval); } } } } // Parse a case-sensitive element. static String parseAttributeString(StreamTokenizer tagTokenizer) throws IOException { String value = ""; // Read "=". tagTokenizer.nextToken(); // Read non-alphabetic characters that may appear in file names as // word characters so that an unquoted file name is parsed as a // single token. tagTokenizer.wordChars('!', '!'); tagTokenizer.wordChars('#', '&'); tagTokenizer.wordChars('(', '/'); tagTokenizer.wordChars('{', '~'); tagTokenizer.wordChars('[', '^'); tagTokenizer.lowerCaseMode(false); tagTokenizer.nextToken(); value = new String(tagTokenizer.sval); // Reset character meanings. tagTokenizer.ordinaryChar('!'); tagTokenizer.ordinaryChars('#', '&'); tagTokenizer.ordinaryChars('(', '/'); tagTokenizer.ordinaryChars('{', '~'); tagTokenizer.ordinaryChars('[', '^'); tagTokenizer.lowerCaseMode(true); return value; } // Parse a comma-delimited list of archives. void parseArchives() throws IOException { StringTokenizer tagTokenizer = new StringTokenizer(archivesList, ","); while (tagTokenizer.hasMoreTokens()) archives.add(prependCodebase(tagTokenizer.nextToken().trim())); } // Prepend the full codebase to basename and return a URL object // representing the result. The full codebase is the codebase // appended to the documentbase. URL prependCodebase(String basename) throws MalformedURLException { 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 stripping 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 if (codebase.endsWith(File.separator)) fullcodebase = new URL(documentbase, codebase); else fullcodebase = new URL(documentbase, codebase + File.separator); } return new URL(fullcodebase, basename); } // Parse an applet parameter. void parseParam(StreamTokenizer tagTokenizer) throws IOException { String key = null; String value = null; while (tagTokenizer.nextToken() != '>') { if (tagTokenizer.ttype == StreamTokenizer.TT_WORD) { if (tagTokenizer.sval.equals("name")) { tagTokenizer.nextToken(); tagTokenizer.nextToken(); key = new String(tagTokenizer.sval).toLowerCase(); } else if (tagTokenizer.sval.equals("value")) value = parseParamValue(tagTokenizer); } if (key != null && value != null) parameters.put(key, value); } } // Parse the value of a parameter. static String parseParamValue(StreamTokenizer tagTokenizer) throws IOException { String value = ""; // Read "=". tagTokenizer.nextToken(); // Read non-alphabetic characters that may appear in // file names as word characters so that an unquoted // file name is parsed as a single token. tagTokenizer.wordChars('!', '!'); tagTokenizer.wordChars('#', '&'); tagTokenizer.wordChars('(', '/'); tagTokenizer.wordChars('{', '~'); tagTokenizer.wordChars('[', '^'); tagTokenizer.lowerCaseMode(false); tagTokenizer.nextToken(); if (tagTokenizer.ttype == StreamTokenizer.TT_NUMBER) { // Remove nval's scale. int unscaled_nval = (int) tagTokenizer.nval; // See if nval represents an integer value. if (Double.compare(tagTokenizer.nval, (double) unscaled_nval) == 0) value = Integer.toString(unscaled_nval); else value = Double.toString(tagTokenizer.nval); } else value = new String(tagTokenizer.sval); // Reset character meanings. tagTokenizer.ordinaryChar('!'); tagTokenizer.ordinaryChars('#', '&'); tagTokenizer.ordinaryChars('(', '/'); tagTokenizer.ordinaryChars('{', '~'); tagTokenizer.ordinaryChars('[', '^'); tagTokenizer.lowerCaseMode(true); return value; } static URL locationToURL(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. if (tmpDocumentBase.getFile().equals("")) 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); } return tmpDocumentBase; } // Return a list of applet tag objects representing the applet tags // that appear in the file or URL "location." static List parseAppletTags(String location, String encoding) throws IOException { ArrayList tags = new ArrayList(); URL tmpDocumentBase = locationToURL(location); InputStream input = tmpDocumentBase.openStream(); StreamTokenizer tagTokenizer = new StreamTokenizer(new InputStreamReader(input)); AppletTag currentTag = parseNextTag(tagTokenizer, tmpDocumentBase, encoding); while (currentTag != null) { tags.add(currentTag); currentTag = parseNextTag(tagTokenizer, tmpDocumentBase, encoding); } return tags; } // Parse the next applet tag in the tagTokenizer stream. static AppletTag parseNextTag(StreamTokenizer tagTokenizer, URL documentbase, String encoding) throws IOException { AppletTag currentTag = null; int token; tagTokenizer.lowerCaseMode(true); tagTokenizer.ordinaryChar('/'); tagTokenizer.wordChars('_', '_'); token = tagTokenizer.nextToken(); while (token != StreamTokenizer.TT_EOF) { // Start of an HTML tag. if (token == '<') { token = tagTokenizer.nextToken(); if (token == StreamTokenizer.TT_WORD) { // An APPLET tag. if (tagTokenizer.sval.equals("applet")) currentTag = new AppletTag(tagTokenizer, documentbase, encoding); else if (tagTokenizer.sval.equals("embed")) currentTag = new EmbedTag(tagTokenizer, documentbase, encoding); else if (tagTokenizer.sval.equals("object")) currentTag = new ObjectTag(tagTokenizer, documentbase, encoding); else if (tagTokenizer.sval.equals("app")) currentTag = new AppTag(tagTokenizer, documentbase, encoding); else if (tagTokenizer.sval.equals("param")) { // Parse APPLET parameters only. if (currentTag != null && ! (currentTag instanceof EmbedTag)) currentTag.parseParam(tagTokenizer); } } else { if (token == '/') { token = tagTokenizer.nextToken(); if (token == StreamTokenizer.TT_WORD) { if (currentTag instanceof AppletTag) { if (tagTokenizer.sval.equals("applet") || tagTokenizer.sval.equals("embed") || tagTokenizer.sval.equals("object") || tagTokenizer.sval.equals("app")) { // Parse archives at this point so // that we can generate full archive // URLs using the documentbase and // codebase fields. currentTag.parseArchives(); return currentTag; } } } } } } token = tagTokenizer.nextToken(); } // If we hit EOF, just go ahead with whatever we've got. Some // pages don't properly terminate; besides which our parser is a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -