From c2baccf5ba951d9e0ec9a490836c8c1c57348d36 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 8 Mar 2002 06:15:21 +0000 Subject: [PATCH] Abstracted code that parses XML configuration definitions into Java objects which are then serialized for rapid loading and simple utilization by the client and server; created serialized config services for our character component constraints (what components are available at character creation time, etc.). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1101 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/tools/CompiledConfigTask.java | 78 +++++++++++++++++++ .../tools/xml/CompiledConfigParser.java | 53 +++++++++++++ .../com/threerings/util/CompiledConfig.java | 43 ++++++++++ 3 files changed, 174 insertions(+) create mode 100644 src/java/com/threerings/tools/CompiledConfigTask.java create mode 100644 src/java/com/threerings/tools/xml/CompiledConfigParser.java create mode 100644 src/java/com/threerings/util/CompiledConfig.java diff --git a/src/java/com/threerings/tools/CompiledConfigTask.java b/src/java/com/threerings/tools/CompiledConfigTask.java new file mode 100644 index 000000000..0e3b3e802 --- /dev/null +++ b/src/java/com/threerings/tools/CompiledConfigTask.java @@ -0,0 +1,78 @@ +// +// $Id: CompiledConfigTask.java,v 1.1 2002/03/08 06:15:21 mdb Exp $ + +package com.threerings.yohoho.tools; + +import java.io.File; +import java.io.Serializable; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +import com.threerings.yohoho.tools.xml.CompiledConfigParser; +import com.threerings.yohoho.util.CompiledConfig; + +/** + * Used to parse configuration information from an XML file and create the + * serialized representation that is used by the client and server. + */ +public class CompiledConfigTask extends Task +{ + public void setParser (String parser) + { + _parser = parser; + } + + public void setConfigdef (File configdef) + { + _configdef = configdef; + } + + public void setTarget (File target) + { + _target = target; + } + + public void execute () throws BuildException + { + // instantiate and sanity check the parser class + Object pobj = null; + try { + Class pclass = Class.forName(_parser); + pobj = pclass.newInstance(); + } catch (Exception e) { + throw new BuildException("Error instantiating config parser", e); + } + if (!(pobj instanceof CompiledConfigParser)) { + throw new BuildException("Invalid parser class: " + _parser); + } + + // make sure the source file exists + if (!_configdef.exists()) { + String errmsg = "Config definition file not found " + + "[path=" + _configdef.getPath() + "]"; + throw new BuildException(errmsg); + } + + CompiledConfigParser parser = (CompiledConfigParser)pobj; + Serializable config = null; + + try { + // parse it on up + config = parser.parseConfig(_configdef); + } catch (Exception e) { + throw new BuildException("Failure parsing config definition", e); + } + + try { + // and write it on out + CompiledConfig.saveConfig(_target, config); + } catch (Exception e) { + throw new BuildException("Failure writing serialized config", e); + } + } + + protected File _configdef; + protected File _target; + protected String _parser; +} diff --git a/src/java/com/threerings/tools/xml/CompiledConfigParser.java b/src/java/com/threerings/tools/xml/CompiledConfigParser.java new file mode 100644 index 000000000..9a6b1c226 --- /dev/null +++ b/src/java/com/threerings/tools/xml/CompiledConfigParser.java @@ -0,0 +1,53 @@ +// +// $Id: CompiledConfigParser.java,v 1.1 2002/03/08 06:15:21 mdb Exp $ + +package com.threerings.yohoho.tools.xml; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.Serializable; + +import org.xml.sax.SAXException; +import org.apache.commons.digester.Digester; + +import com.threerings.yohoho.util.CompiledConfig; +import com.threerings.yohoho.tools.CompiledConfigTask; + +/** + * An abstract base implementation of a parser that is used to compile + * configuration definitions into config objects for use by the client and + * server. + * + * @see CompiledConfig + * @see CompiledConfigTask + */ +public abstract class CompiledConfigParser +{ + /** + * Parses the supplied configuration file into a serializable + * configuration object. + */ + public Serializable parseConfig (File source) + throws IOException, SAXException + { + Digester digester = new Digester(); + digester.setDebug(99); + Serializable config = createConfigObject(); + addRules(digester); + digester.push(config); + digester.parse(new FileInputStream(source)); + return config; + } + + /** + * Creates the config object instance that will be populated during + * the parsing process. + */ + protected abstract Serializable createConfigObject (); + + /** + * Adds the necessary digester rules for parsing the config object. + */ + protected abstract void addRules (Digester digester); +} diff --git a/src/java/com/threerings/util/CompiledConfig.java b/src/java/com/threerings/util/CompiledConfig.java new file mode 100644 index 000000000..20c92ebe9 --- /dev/null +++ b/src/java/com/threerings/util/CompiledConfig.java @@ -0,0 +1,43 @@ +// +// $Id: CompiledConfig.java,v 1.1 2002/03/08 06:15:21 mdb Exp $ + +package com.threerings.yohoho.util; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +/** + * Used to load and store compiled configuration data (generally XML files + * that are parsed into Java object models and then serialized for rapid + * and simple access on the client and server). + */ +public class CompiledConfig +{ + /** + * Unserializes a configuration object from the supplied input stream. + */ + public static Serializable loadConfig (InputStream source) + throws IOException, ClassNotFoundException + { + ObjectInputStream oin = new ObjectInputStream(source); + return (Serializable)oin.readObject(); + } + + /** + * Serializes the supplied configuration object to the specified file + * path. + */ + public static void saveConfig (File target, Serializable config) + throws IOException + { + FileOutputStream fout = new FileOutputStream(target); + ObjectOutputStream oout = new ObjectOutputStream(fout); + oout.writeObject(config); + oout.close(); + } +}