More logging fixing, moved the compiled config stuff into Nenya.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@4 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2006-06-23 21:21:05 +00:00
parent 220e2ddc53
commit e25ef0859e
13 changed files with 434 additions and 60 deletions
@@ -0,0 +1,132 @@
//
// $Id: CompiledConfigTask.java 3786 2005-12-19 19:09:00Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.tools;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import com.samskivert.util.FileUtil;
import com.threerings.tools.xml.CompiledConfigParser;
import com.threerings.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 addFileset (FileSet set)
{
_filesets.add(set);
}
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);
}
CompiledConfigParser parser = (CompiledConfigParser)pobj;
// if we have a single file and target specified, do those
if (_configdef != null) {
parse(parser, _configdef, _target);
}
// deal with the filesets
for (Iterator iter = _filesets.iterator(); iter.hasNext(); ) {
FileSet fs = (FileSet)iter.next();
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File fromDir = fs.getDir(getProject());
String[] srcFiles = ds.getIncludedFiles();
for (int ii = 0; ii < srcFiles.length; ii++) {
File confdef = new File(fromDir, srcFiles[ii]);
parse(parser, confdef, null);
}
}
}
protected void parse (CompiledConfigParser parser, File confdef, File target)
throws BuildException
{
// make sure the source file exists
if (!confdef.exists()) {
String errmsg = "Config definition file not found: " + confdef;
throw new BuildException(errmsg);
}
// if no target was specified, resuffix the source file as to .dat
if (target == null) {
target = new File(FileUtil.resuffix(confdef, ".xml", ".dat"));
}
System.out.println("Compiling " + confdef + "...");
Serializable config = null;
try {
// parse it on up
config = parser.parseConfig(confdef);
} 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;
protected ArrayList _filesets = new ArrayList();
}
@@ -0,0 +1,70 @@
//
// $Id: CompiledConfigParser.java 3310 2005-01-24 23:08:21Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.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.tools.CompiledConfigTask;
import com.threerings.util.CompiledConfig;
/**
* 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();
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);
}
@@ -0,0 +1,83 @@
//
// $Id: NestableRuleSet.java 4191 2006-06-13 22:42:20Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.tools.xml;
import org.apache.commons.digester.Digester;
/**
* Used to define rule sets that can be nested within other rule sets. For
* example, say you have a "scene" object definition like so:
*
* <p> (Note that in the examples square brackets are used instead of
* angle brackets to simplify my life when composing the documentation.)
*
* <pre>
* [scene name="Foo" version=5]
* [/scene]
* </pre>
*
* This scene is extended with some auxiliary data defined by libraries
* which can parse and generate XML for their auxiliary objects:
*
* <pre>
* [scene sceneId=1 name="Foo" version=5]
* [spot]
* [portal portalId=1 x=1 y=1 targetSceneId=2/]
* [portal portalId=2 x=15 y=3 targetSceneId=3/]
* [portal portalId=3 x=9 y=6 targetSceneId=4/]
* [/spot]
* [miso]
* [object tileId=878172 x=4 y=13 action="cluck"/]
* [object tileId=123843 x=18 y=23 action="bark"/]
* [/miso]
* [/scene]
* </pre>
*
* The spot and miso services can define nestable rule sets which will be
* handed to the scene services who will instruct them to add their rule
* instances with a prefix of <code>scene.spot</code> and
* <code>scene.miso</code> respectively. They then happily parse their
* auxiliary objects without knowing that they have been nested inside
* some larger structure.
*
* <p> The nestable ruleset should then leave a single object on the
* digester stack that the enclosing entity can grab.
*
* <p> This isn't proper use of XML, but it solves the problem at hand in
* an easily extensible manner.
*/
public interface NestableRuleSet
{
/**
* Returns the name of the nested object's outer element so that the
* parent parser can use it to compose the total path prefix.
*/
public String getOuterElement ();
/**
* Instructs this ruleset to add its rules such that it parses its
* object from the specified path prefix. The outer element returned
* by {@link #getOuterElement} will have been included in the path
* prefix.
*/
public void addRuleInstances (String prefix, Digester digester);
}
@@ -0,0 +1,39 @@
//
// $Id: NestableWriter.java 4191 2006-06-13 22:42:20Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.tools.xml;
import org.xml.sax.SAXException;
import com.megginson.sax.DataWriter;
/**
* Provides the writing component of the nestable parsing system described
* by {@link NestableRuleSet}.
*/
public interface NestableWriter
{
/**
* Called to generate XML for the supplied object to the supplied data
* writer.
*/
public void write (Object object, DataWriter writer)
throws SAXException;
}