Bundles! Got all the tileset bundle stuff working and wrote some tests to

inrease my confidence in the use of the word "working".


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@635 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-11-21 02:42:16 +00:00
parent 39c6b9d9b1
commit 7ad2acca11
20 changed files with 625 additions and 137 deletions
@@ -1,5 +1,5 @@
//
// $Id: SwissArmyTileSetRuleSet.java,v 1.3 2001/11/20 04:15:44 mdb Exp $
// $Id: SwissArmyTileSetRuleSet.java,v 1.4 2001/11/21 02:42:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
@@ -36,23 +36,13 @@ import com.threerings.media.tile.SwissArmyTileSet;
*/
public class SwissArmyTileSetRuleSet extends TileSetRuleSet
{
/**
* Constructs a uniform tileset rule set that will match tilesets with
* the specified prefix. See the documentation for {@link
* TileSetRuleSet#TileSetRuleSet} for more info on matching.
*/
public SwissArmyTileSetRuleSet (String prefix)
{
super(prefix);
}
// documentation inherited
public void addRuleInstances (Digester digester)
{
super.addRuleInstances(digester);
digester.addRule(
_prefix + "/tileset/widths",
_prefix + TILESET_PATH + "/widths",
new CallMethodSpecialRule(digester) {
public void parseAndSet (String bodyText, Object target)
{
@@ -62,7 +52,7 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
});
digester.addRule(
_prefix + "/tileset/heights",
_prefix + TILESET_PATH + "/heights",
new CallMethodSpecialRule(digester) {
public void parseAndSet (String bodyText, Object target)
{
@@ -72,7 +62,7 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
});
digester.addRule(
_prefix + "/tileset/tileCounts",
_prefix + TILESET_PATH + "/tileCounts",
new CallMethodSpecialRule(digester) {
public void parseAndSet (String bodyText, Object target)
{
@@ -83,10 +73,9 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
}
// documentation inherited
protected TileSet createTileSet (Attributes attributes)
protected Class getTileSetClass ()
{
// we use uniform tilesets
return new SwissArmyTileSet();
return SwissArmyTileSet.class;
}
/**
@@ -1,5 +1,5 @@
//
// $Id: TileSetRuleSet.java,v 1.3 2001/11/20 04:15:44 mdb Exp $
// $Id: TileSetRuleSet.java,v 1.4 2001/11/21 02:42:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
@@ -18,9 +18,15 @@ import com.threerings.media.tile.TileSet;
*/
public abstract class TileSetRuleSet extends RuleSetBase
{
/** The component of the digester path that is appended by the tileset
* rule set to match a tileset. This is appended to whatever prefix is
* provided to the tileset rule set to obtain the complete XML path to
* a matched tileset. */
public static final String TILESET_PATH = "/tileset";
/**
* Constructs a tileset rule set which will match tilesets with the
* supplied prefix. For example, passing a prefix of
* Instructs the tileset rule set to match tilesets with the supplied
* prefix. For example, passing a prefix of
* <code>tilesets.objectsets</code> will match tilesets in the
* following XML file:
*
@@ -33,21 +39,14 @@ public abstract class TileSetRuleSet extends RuleSetBase
* &lt;/objectsets&gt;
* &lt;/tilesets&gt;
* </pre>
*
* This must be called before adding the ruleset to a digester.
*/
public TileSetRuleSet (String prefix)
public void setPrefix (String prefix)
{
_prefix = prefix;
}
/**
* Called by the {@link XMLTileSetParser} to initialize this tileset
* rule set with the necessary back references to operate properly.
*/
protected void init (XMLTileSetParser parser)
{
_parser = parser;
}
/**
* Adds the necessary rules to the digester to parse our tilesets.
* Derived classes should override this method, being sure to call the
@@ -59,62 +58,23 @@ public abstract class TileSetRuleSet extends RuleSetBase
{
// this creates the appropriate instance when we encounter a
// <tileset> tag
digester.addRule(_prefix + "/tileset",
new TileSetCreateRule(digester));
digester.addObjectCreate(_prefix + TILESET_PATH,
getTileSetClass().getName());
// grab the name attribute from the <tileset> tag
digester.addSetProperties(_prefix + "/tileset");
digester.addSetProperties(_prefix + TILESET_PATH);
// grab the image path from an element
digester.addCallMethod(
_prefix + "/tileset/imagePath", "setImagePath", 0);
_prefix + TILESET_PATH + "/imagePath", "setImagePath", 0);
}
/**
* When a &lt;tileset&gt; element is encountered, this method is
* called to create a new instance of {@link TileSet}. Though the
* attributes are supplied (in case an attribute is needed to
* determine which derived instance of {@link TileSet} to create),
* this method should not configure the created tileset object. It
* should instead rely on the set properties rule that will be
* executed after this object is created or to custom set property
* rules registered in {@link #addRuleInstances}.
* A tileset rule set will create tilesets of a particular class,
* which must be provided by the derived class via this method.
*/
protected abstract TileSet createTileSet (Attributes attributes);
/**
* Used to process a &lt;tileset&gt; element.
*/
protected class TileSetCreateRule extends Rule
{
public TileSetCreateRule (Digester digester)
{
super(digester);
}
public void begin (Attributes attributes)
throws Exception
{
// pass the torch to the XML parser to create the tileset
TileSet set = createTileSet(attributes);
// then push it onto the stack
digester.push(set);
}
public void end ()
throws Exception
{
// pop the tileset off of the stack
TileSet set = (TileSet)digester.pop();
// and stick it into our tileset map
_parser._tilesets.put(set.getName(), set);
}
}
protected abstract Class getTileSetClass ();
/** The prefix at which me match our tilesets. */
protected String _prefix;
/** A reference to the XMLTileSetParser on whose behalf we are
* parsing. */
protected XMLTileSetParser _parser;
}
@@ -1,5 +1,5 @@
//
// $Id: UniformTileSetRuleSet.java,v 1.3 2001/11/20 04:15:44 mdb Exp $
// $Id: UniformTileSetRuleSet.java,v 1.4 2001/11/21 02:42:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
@@ -27,36 +27,25 @@ import com.threerings.media.tile.UniformTileSet;
*/
public class UniformTileSetRuleSet extends TileSetRuleSet
{
/**
* Constructs a uniform tileset rule set that will match tilesets with
* the specified prefix. See the documentation for {@link
* TileSetRuleSet#TileSetRuleSet} for more info on matching.
*/
public UniformTileSetRuleSet (String prefix)
{
super(prefix);
}
// documentation inherited
public void addRuleInstances (Digester digester)
{
super.addRuleInstances(digester);
digester.addCallMethod(
_prefix + "/tileset/width", "setWidth", 0,
_prefix + TILESET_PATH + "/width", "setWidth", 0,
new Class[] { java.lang.Integer.TYPE });
digester.addCallMethod(
_prefix + "/tileset/height", "setHeight", 0,
_prefix + TILESET_PATH + "/height", "setHeight", 0,
new Class[] { java.lang.Integer.TYPE });
digester.addCallMethod(
_prefix + "/tileset/tileCount", "setTileCount", 0,
_prefix + TILESET_PATH + "/tileCount", "setTileCount", 0,
new Class[] { java.lang.Integer.TYPE });
}
// documentation inherited
protected TileSet createTileSet (Attributes attributes)
protected Class getTileSetClass ()
{
// we use uniform tilesets
return new UniformTileSet();
return UniformTileSet.class;
}
}
@@ -1,5 +1,5 @@
//
// $Id: XMLTileSetParser.java,v 1.2 2001/11/20 04:15:44 mdb Exp $
// $Id: XMLTileSetParser.java,v 1.3 2001/11/21 02:42:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
@@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.SAXException;
@@ -17,12 +18,13 @@ import org.apache.commons.digester.Rule;
import com.samskivert.util.ConfigUtil;
import com.threerings.media.Log;
import com.threerings.media.tile.TileSet;
/**
* Parse an XML tileset description file and construct tileset objects
* for each valid description. Does not currently perform validation
* on the input XML stream, though the parsing code assumes the XML
* document is well-formed.
* Parse an XML tileset description file and construct tileset objects for
* each valid description. Does not currently perform validation on the
* input XML stream, though the parsing code assumes the XML document is
* well-formed.
*/
public class XMLTileSetParser
{
@@ -33,38 +35,45 @@ public class XMLTileSetParser
{
// create our digester
_digester = new Digester();
// _digester.setDebug(10);
}
/**
* Adds a ruleset to be used when parsing tiles. This should be an
* instance of a class derived from {@link TileSetRuleSet} which was
* constructed to match a particular kind of tile at a particular
* point in the XML hierarchy. For example:
* instance of a class derived from {@link TileSetRuleSet}. The prefix
* will be used to configure the ruleset so that it matches elements
* at a particular point in the XML hierarchy. For example:
*
* <pre>
* _parser.addRuleSet(new UniformTileSetRuleSet("tilesets"));
* _parser.addRuleSet("tilesets", new UniformTileSetRuleSet());
* </pre>
*/
public void addRuleSet (TileSetRuleSet ruleset)
public void addRuleSet (String prefix, TileSetRuleSet ruleset)
{
// provide a reference to ourselves to the ruleset
ruleset.init(this);
// configure the ruleset with the appropriate prefix
ruleset.setPrefix(prefix);
// and have it set itself up with the digester
_digester.addRuleSet(ruleset);
// add a set next rule which will put tilesets with this prefix
// into the array list that'll be on the top of the stack
_digester.addSetNext(prefix + TileSetRuleSet.TILESET_PATH,
"add", "java.lang.Object");
}
/**
* Loads all of the tilesets specified in the supplied XML tileset
* description file and places them into the supplied hashmap indexed
* by tileset name.
* by tileset name. This method is not reentrant, so don't go calling
* it from multiple threads.
*/
public void loadTileSets (String path, HashMap tilesets)
throws IOException
{
// save off the tileset hashtable
_tilesets = tilesets;
// stick an array list on the top of the stack for collecting
// parsed tilesets
ArrayList setlist = new ArrayList();
_digester.push(setlist);
// get an input stream for this XML file
InputStream is = ConfigUtil.getStream(path);
@@ -84,14 +93,24 @@ public class XMLTileSetParser
"[error=" + saxe + "].");
Log.logStackTrace(saxe);
}
// stick the tilesets from the list into the hashtable
for (int i = 0; i < setlist.size(); i++) {
TileSet set = (TileSet)setlist.get(i);
if (set.getName() == null) {
Log.warning("Tileset did not receive name during " +
"parsing process [set=" + set + "].");
} else {
tilesets.put(set.getName(), set);
}
}
// and clear out the list for next time
setlist.clear();
}
/** Our XML digester. */
protected Digester _digester;
/** The tilesets constructed thus far. */
protected HashMap _tilesets;
/** Default tileset name. */
protected static final String DEF_NAME = "Untitled";