001: /*
002: * Copyright (C) 2007 Jared Alexander Spigner
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * jspigner@openjx.org
019: *
020: * PluginParser.java
021: *
022: * Created on June 9, 2007, 10:30 PM
023: *
024: */
025:
026: package org.openjx.parser;
027:
028: import java.io.BufferedInputStream;
029: import java.io.IOException;
030: import java.io.StringReader;
031:
032: import java.net.URL;
033:
034: import java.util.Enumeration;
035: import java.util.Properties;
036: import java.util.Vector;
037:
038: import javax.swing.JOptionPane;
039:
040: import org.openjx.conf.Plugin;
041:
042: import org.openjx.core.VirtualMachine;
043:
044: import org.openjx.display.JXMessageBox;
045:
046: /**
047: * This class parses the application file and generates a list of loadable
048: * plugins specified by the XML form.
049: *
050: * @author Jared Spigner
051: */
052: public class PluginParser {
053:
054: /** This is a reference to the Virtual Machine. */
055: private VirtualMachine virtualMachine;
056:
057: /** This is a reference to the list of plugins. */
058: private Vector<Plugin> pluginList;
059:
060: /**
061: * This is the constructor for the PluginParser class. It creates a new
062: * instance of PluginParser.
063: *
064: * @param vm point to an instance of the Virtual Machine class.
065: */
066: public PluginParser(VirtualMachine vm) {
067: this .virtualMachine = vm;
068:
069: this .pluginList = null;
070: }
071:
072: /**
073: * This method returns the pluginList.
074: *
075: * @return the plugin list.
076: */
077: public Vector<Plugin> getPluginList() {
078: return this .pluginList;
079: }
080:
081: /**
082: * This method returns the list of plugins with the control flag set to
083: * true or false.
084: *
085: * @return the plugin list with the specified control flag.
086: */
087: public Vector<Plugin> getPluginListCTL(boolean flag) {
088: Vector<Plugin> plugin = new Vector<Plugin>();
089:
090: for (int i = 0; i < this .pluginList.size(); i++) {
091: if (this .pluginList.elementAt(i).getPluginControl() == flag)
092: plugin.addElement(this .pluginList.elementAt(i));
093: }
094:
095: return plugin;
096: }
097:
098: /**
099: * This method parses the application file and builds a list of plugins to
100: * be loaded by OpenJX.
101: *
102: * @return true on success, else false on failure.
103: */
104: public boolean parsePlugins() {
105: String systemId = this .virtualMachine.getApplicationFile();
106:
107: try {
108: int ch = 0;
109: URL url = new URL(systemId);
110: BufferedInputStream bis = new BufferedInputStream(url
111: .openStream());
112:
113: StringBuffer buf = new StringBuffer();
114: StringBuffer tmp = new StringBuffer();
115:
116: while ((ch = bis.read()) > -1) {
117: tmp.append((char) ch);
118:
119: if ((ch == '\n' || ch == '\r')
120: && !tmp.toString().startsWith(
121: "[JXPLUGIN_START]"))
122: tmp = new StringBuffer();
123:
124: if (tmp.toString().endsWith("[JXPLUGIN_END]")) {
125: buf.append(tmp.toString().substring(16,
126: tmp.toString().length() - 14));
127: tmp = new StringBuffer();
128: }
129: }
130:
131: StringReader sis = new StringReader(buf.toString());
132:
133: Properties prop = new Properties();
134: prop.load(sis);
135:
136: Enumeration en = prop.propertyNames();
137:
138: this .pluginList = new Vector<Plugin>();
139:
140: while (en.hasMoreElements()) {
141: String s = (String) en.nextElement();
142: String strValue = prop.getProperty(s);
143:
144: this .pluginList.add(new Plugin(strValue, s));
145: }
146: } catch (IOException e) {
147: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
148: "Plugin Parsing Failure", "" + e,
149: this .virtualMachine.getViewer());
150: return false;
151: }
152:
153: return true;
154: }
155:
156: }
|