Moved some stuff into BundleUtil. Added code to support post-parsing

tileset validation.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@700 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-11-29 21:58:15 +00:00
parent be5428a2f7
commit 46b71392aa
6 changed files with 183 additions and 11 deletions
@@ -1,5 +1,5 @@
//
// $Id: TileSetBundler.java,v 1.2 2001/11/29 00:13:20 mdb Exp $
// $Id: TileSetBundler.java,v 1.3 2001/11/29 21:58:15 mdb Exp $
package com.threerings.media.tools.tile.bundle;
@@ -30,7 +30,7 @@ import com.samskivert.io.PersistenceException;
import com.threerings.media.Log;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.TileSetIDBroker;
import com.threerings.media.tile.bundle.BundledTileSetRepository;
import com.threerings.media.tile.bundle.BundleUtil;
import com.threerings.media.tile.bundle.TileSetBundle;
import com.threerings.media.tools.tile.xml.TileSetRuleSet;
@@ -270,8 +270,7 @@ public class TileSetBundler
try {
// first write a serialized representation of the tileset
// bundle object to the bundle jar file
JarEntry entry = new JarEntry(
BundledTileSetRepository.METADATA_PATH);
JarEntry entry = new JarEntry(BundleUtil.METADATA_PATH);
jar.putNextEntry(entry);
ObjectOutputStream oout = new ObjectOutputStream(jar);
oout.writeObject(bundle);
@@ -0,0 +1,75 @@
//
// $Id: ObjectTileSetRuleSet.java,v 1.1 2001/11/29 21:58:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
import java.awt.Dimension;
import java.awt.Point;
import org.xml.sax.Attributes;
import org.apache.commons.digester.Digester;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.CallMethodSpecialRule;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.ObjectTileSet;
/**
* Parses {@link ObjectTileSet} instances from a tileset description. An
* object tileset description looks like so:
*
* <pre>
* &lt;tileset name="Sample Object Tileset"&gt;
* &lt;imagePath&gt;path/to/image.png&lt;/imagePath&gt;
* &lt;!-- the widths (per row) of each tile in pixels --&gt;
* &lt;widths&gt;265&lt;/widths&gt;
* &lt;!-- the heights (per row) of each tile in pixels --&gt;
* &lt;heights&gt;224&lt;/heights&gt;
* &lt;!-- the number of tiles in each row --&gt;
* &lt;tileCounts&gt;4&lt;/tileCounts&gt;
* &lt;!-- the offset in pixels to the upper left tile --&gt;
* &lt;offsetPos&gt;0, 0&lt;/offsetPos&gt;
* &lt;!-- the gap between tiles in pixels --&gt;
* &lt;gapSize&gt;0, 0&lt;/gapSize&gt;
* &lt;!-- the widths (in unit tile count) of the objects --&gt;
* &lt;objectWidths&gt;4, 3, 4, 3&lt;/objectWidths&gt;
* &lt;!-- the heights (in unit tile count) of the objects --&gt;
* &lt;objectHeights&gt;3, 4, 3, 4&lt;/objectHeights&gt;
* &lt;/tileset&gt;
* </pre>
*/
public class ObjectTileSetRuleSet extends TileSetRuleSet
{
// documentation inherited
public void addRuleInstances (Digester digester)
{
super.addRuleInstances(digester);
digester.addRule(
_prefix + TILESET_PATH + "/objectWidths",
new CallMethodSpecialRule(digester) {
public void parseAndSet (String bodyText, Object target)
{
int[] widths = StringUtil.parseIntArray(bodyText);
((ObjectTileSet)target).setObjectWidths(widths);
}
});
digester.addRule(
_prefix + TILESET_PATH + "/objectHeights",
new CallMethodSpecialRule(digester) {
public void parseAndSet (String bodyText, Object target)
{
int[] heights = StringUtil.parseIntArray(bodyText);
((ObjectTileSet)target).setObjectHeights(heights);
}
});
}
// documentation inherited
protected Class getTileSetClass ()
{
return ObjectTileSet.class;
}
}
@@ -1,5 +1,5 @@
//
// $Id: SwissArmyTileSetRuleSet.java,v 1.6 2001/11/29 20:39:37 mdb Exp $
// $Id: SwissArmyTileSetRuleSet.java,v 1.7 2001/11/29 21:58:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
@@ -12,6 +12,7 @@ import org.apache.commons.digester.Digester;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.CallMethodSpecialRule;
import com.threerings.media.Log;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.SwissArmyTileSet;
@@ -95,6 +96,36 @@ public class SwissArmyTileSetRuleSet extends TileSetRuleSet
});
}
// documentation inherited
public boolean isValid (Object target)
{
SwissArmyTileSet set = (SwissArmyTileSet)target;
boolean valid = super.isValid(target);
// check for a <widths> element
if (set.getWidths() == null) {
Log.warning("Tile set definition missing valid <widths> " +
"element [set=" + set + "].");
valid = false;
}
// check for a <heights> element
if (set.getHeights() == null) {
Log.warning("Tile set definition missing valid <heights> " +
"element [set=" + set + "].");
valid = false;
}
// check for a <tileCounts> element
if (set.getTileCounts() == null) {
Log.warning("Tile set definition missing valid <tileCounts> " +
"element [set=" + set + "].");
valid = false;
}
return valid;
}
// documentation inherited
protected Class getTileSetClass ()
{
@@ -1,5 +1,5 @@
//
// $Id: TileSetRuleSet.java,v 1.4 2001/11/21 02:42:15 mdb Exp $
// $Id: TileSetRuleSet.java,v 1.5 2001/11/29 21:58:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
@@ -9,6 +9,11 @@ import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.digester.RuleSetBase;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.ValidatedSetNextRule;
import com.samskivert.xml.ValidatedSetNextRule.Validator;
import com.threerings.media.Log;
import com.threerings.media.tile.TileSet;
/**
@@ -16,7 +21,8 @@ import com.threerings.media.tile.TileSet;
* instance. Derived classes would extend this and add rules for their own
* special tilesets.
*/
public abstract class TileSetRuleSet extends RuleSetBase
public abstract class TileSetRuleSet
extends RuleSetBase implements Validator
{
/** 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
@@ -69,6 +75,33 @@ public abstract class TileSetRuleSet extends RuleSetBase
_prefix + TILESET_PATH + "/imagePath", "setImagePath", 0);
}
/**
* The ruleset can be provided to a {@link ValidatedSetNextRule} to
* ensure that the tileset was fully parsed before doing something
* with it.
*/
public boolean isValid (Object target)
{
TileSet set = (TileSet)target;
boolean valid = true;
// check for the 'name' attribute
if (StringUtil.blank(set.getName())) {
Log.warning("Tile set definition missing 'name' attribute " +
"[set=" + set + "].");
valid = false;
}
// check for an <imagePath> element
if (StringUtil.blank(set.getImagePath())) {
Log.warning("Tile set definition missing <imagePath> element " +
"[set=" + set + "].");
valid = false;
}
return valid;
}
/**
* A tileset rule set will create tilesets of a particular class,
* which must be provided by the derived class via this method.
@@ -1,11 +1,12 @@
//
// $Id: UniformTileSetRuleSet.java,v 1.5 2001/11/29 20:39:37 mdb Exp $
// $Id: UniformTileSetRuleSet.java,v 1.6 2001/11/29 21:58:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
import org.xml.sax.Attributes;
import org.apache.commons.digester.Digester;
import com.threerings.media.Log;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.UniformTileSet;
@@ -43,6 +44,36 @@ public class UniformTileSetRuleSet extends TileSetRuleSet
new Class[] { java.lang.Integer.TYPE });
}
// documentation inherited
public boolean isValid (Object target)
{
UniformTileSet set = (UniformTileSet)target;
boolean valid = super.isValid(target);
// check for a <width> element
if (set.getWidth() == 0) {
Log.warning("Tile set definition missing valid <width> " +
"element [set=" + set + "].");
valid = false;
}
// check for a <height> element
if (set.getHeight() == 0) {
Log.warning("Tile set definition missing valid <height> " +
"element [set=" + set + "].");
valid = false;
}
// check for a <tileCount> element
if (set.getTileCount() == 0) {
Log.warning("Tile set definition missing valid <tileCount> " +
"element [set=" + set + "].");
valid = false;
}
return valid;
}
// documentation inherited
protected Class getTileSetClass ()
{
@@ -1,5 +1,5 @@
//
// $Id: XMLTileSetParser.java,v 1.4 2001/11/27 22:06:39 mdb Exp $
// $Id: XMLTileSetParser.java,v 1.5 2001/11/29 21:58:15 mdb Exp $
package com.threerings.media.tools.tile.xml;
@@ -16,6 +16,7 @@ import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import com.samskivert.util.ConfigUtil;
import com.samskivert.xml.ValidatedSetNextRule;
import com.threerings.media.Log;
import com.threerings.media.tile.TileSet;
@@ -57,8 +58,10 @@ public class XMLTileSetParser
// 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");
_digester.addRule(prefix + TileSetRuleSet.TILESET_PATH,
new ValidatedSetNextRule(_digester,
"add", Object.class,
ruleset));
}
/**