+ String epclass = EditablePortal.class.getName();
+ digester.addObjectCreate(_prefix + "/portal", epclass);
+ digester.addRule(_prefix + "/portal",
+ new SetPropertyFieldsRule(digester));
+ digester.addSetNext(_prefix + "/portal", "addPortal", epclass);
}
// documentation inherited
- protected Class getSceneModelClass ()
+ protected Class getSceneClass ()
{
- return SpotSceneModel.class;
+ return EditableSpotSceneImpl.class;
}
}
diff --git a/src/java/com/threerings/whirled/tools/EditableScene.java b/src/java/com/threerings/whirled/tools/EditableScene.java
index cfbbc0f41..bac548380 100644
--- a/src/java/com/threerings/whirled/tools/EditableScene.java
+++ b/src/java/com/threerings/whirled/tools/EditableScene.java
@@ -1,8 +1,10 @@
//
-// $Id: EditableScene.java,v 1.2 2001/12/04 22:34:04 mdb Exp $
+// $Id: EditableScene.java,v 1.3 2001/12/05 03:38:09 mdb Exp $
package com.threerings.whirled.tools;
+import java.util.ArrayList;
+
import com.threerings.whirled.client.DisplayScene;
import com.threerings.whirled.data.SceneModel;
@@ -53,12 +55,21 @@ public interface EditableScene extends DisplayScene
/**
* Returns the names of the neighbors of this scene.
*/
- public String[] getNeighborNames ();
+ public ArrayList getNeighborNames ();
/**
- * Sets the names of the neighbors of this scene.
+ * Adds a neighbor to this scene. If the neighbor is already listed
+ * among the neighbors, it will not be added again.
*/
- public void setNeighborNames (String[] neighborNames);
+ public void addNeighbor (String neighborName);
+
+ /**
+ * Adds a neighbor to this scene.
+ *
+ * @return true if the neighbor was removed, false if they were not in
+ * the neighbor list.
+ */
+ public boolean removeNeighbor (String neighborName);
/**
* Implementations must provide an editable scene model that
diff --git a/src/java/com/threerings/whirled/tools/EditableSceneImpl.java b/src/java/com/threerings/whirled/tools/EditableSceneImpl.java
index 4085edef6..660ecdb3c 100644
--- a/src/java/com/threerings/whirled/tools/EditableSceneImpl.java
+++ b/src/java/com/threerings/whirled/tools/EditableSceneImpl.java
@@ -1,8 +1,10 @@
//
-// $Id: EditableSceneImpl.java,v 1.3 2001/12/04 22:34:04 mdb Exp $
+// $Id: EditableSceneImpl.java,v 1.4 2001/12/05 03:38:09 mdb Exp $
package com.threerings.whirled.tools;
+import java.util.ArrayList;
+
import com.threerings.whirled.client.DisplaySceneImpl;
import com.threerings.whirled.data.SceneModel;
@@ -20,6 +22,15 @@ public class EditableSceneImpl
public EditableSceneImpl (EditableSceneModel model)
{
super(model.sceneModel, null);
+ _emodel = model;
+ }
+
+ /**
+ * Creates an instance that will create and use a blank scene model.
+ */
+ public EditableSceneImpl ()
+ {
+ this(EditableSceneModel.blankSceneModel());
}
// documentation inherited
@@ -53,15 +64,23 @@ public class EditableSceneImpl
}
// documentation inherited
- public String[] getNeighborNames ()
+ public ArrayList getNeighborNames ()
{
return _emodel.neighborNames;
}
// documentation inherited
- public void setNeighborNames (String[] neighborNames)
+ public void addNeighbor (String neighborName)
{
- _emodel.neighborNames = neighborNames;
+ if (!_emodel.neighborNames.contains(neighborName)) {
+ _emodel.neighborNames.add(neighborName);
+ }
+ }
+
+ // documentation inherited
+ public boolean removeNeighbor (String neighborName)
+ {
+ return _emodel.neighborNames.remove(neighborName);
}
// documentation inherited
diff --git a/src/java/com/threerings/whirled/tools/EditableSceneModel.java b/src/java/com/threerings/whirled/tools/EditableSceneModel.java
index ef6f99e78..9ab5729cb 100644
--- a/src/java/com/threerings/whirled/tools/EditableSceneModel.java
+++ b/src/java/com/threerings/whirled/tools/EditableSceneModel.java
@@ -1,8 +1,11 @@
//
-// $Id: EditableSceneModel.java,v 1.1 2001/12/04 22:34:04 mdb Exp $
+// $Id: EditableSceneModel.java,v 1.2 2001/12/05 03:38:09 mdb Exp $
package com.threerings.whirled.tools;
+import java.util.ArrayList;
+import com.samskivert.util.StringUtil;
+
import com.threerings.whirled.data.SceneModel;
/**
@@ -18,5 +21,56 @@ public class EditableSceneModel
public String sceneName;
/** The human readable name of this scene's neighbors. */
- public String[] neighborNames;
+ public ArrayList neighborNames;
+
+ /**
+ * Generates a string representation of this scene model.
+ */
+ public String toString ()
+ {
+ StringBuffer buf = new StringBuffer("[");
+ delegatesToString(buf);
+ toString(buf);
+ return buf.append("]").toString();
+ }
+
+ /**
+ * Derived classes override this to tack their toString
+ * data on to the string buffer.
+ */
+ protected void delegatesToString (StringBuffer buf)
+ {
+ buf.append(sceneModel);
+ }
+
+ /**
+ * Derived classes override this to tack their toString
+ * data on to the string buffer.
+ */
+ protected void toString (StringBuffer buf)
+ {
+ buf.append(", sceneName=").append(sceneName);
+ buf.append(", neighborNames=").
+ append(StringUtil.toString(neighborNames));
+ }
+
+ /**
+ * Creates and returns a blank editable scene model.
+ */
+ public static EditableSceneModel blankSceneModel ()
+ {
+ EditableSceneModel model = new EditableSceneModel();
+ model.sceneModel = SceneModel.blankSceneModel();
+ populateBlankSceneModel(model);
+ return model;
+ }
+
+ /**
+ * Populates a blank editable scene model with blank values.
+ */
+ protected static void populateBlankSceneModel (EditableSceneModel model)
+ {
+ model.sceneName = "";
+ model.neighborNames = new ArrayList();
+ }
}
diff --git a/src/java/com/threerings/whirled/tools/xml/SceneParser.java b/src/java/com/threerings/whirled/tools/xml/SceneParser.java
new file mode 100644
index 000000000..35445ba07
--- /dev/null
+++ b/src/java/com/threerings/whirled/tools/xml/SceneParser.java
@@ -0,0 +1,58 @@
+//
+// $Id: SceneParser.java,v 1.1 2001/12/05 03:38:09 mdb Exp $
+
+package com.threerings.whirled.tools.xml;
+
+import java.io.IOException;
+import java.io.FileInputStream;
+
+import org.xml.sax.SAXException;
+import org.apache.commons.digester.Digester;
+
+import com.threerings.whirled.tools.EditableScene;
+import com.threerings.whirled.tools.EditableSceneImpl;
+
+/**
+ * A simple class for parsing an editable scene instance.
+ */
+public class SceneParser
+{
+ /**
+ * Constructs a scene parser that parses scenes with the specified XML
+ * path prefix. See the {@link SceneRuleSet#SceneRuleSet}
+ * documentation for more information.
+ */
+ public SceneParser (String prefix)
+ {
+ // create and configure our digester
+ _digester = new Digester();
+ SceneRuleSet set = new SceneRuleSet();
+ set.setPrefix(prefix);
+ _digester.addRuleSet(set);
+ _digester.addSetNext(prefix, "setScene", EditableScene.class.getName());
+ }
+
+ /**
+ * Parses the XML file at the specified path into an editable scene
+ * instance.
+ */
+ public EditableScene parseScene (String path)
+ throws IOException, SAXException
+ {
+ _scene = null;
+ _digester.push(this);
+ _digester.parse(new FileInputStream(path));
+ return _scene;
+ }
+
+ /**
+ * Called by the parser once the scene is parsed.
+ */
+ public void setScene (EditableScene scene)
+ {
+ _scene = scene;
+ }
+
+ protected Digester _digester;
+ protected EditableScene _scene;
+}
diff --git a/src/java/com/threerings/whirled/tools/xml/SceneRuleSet.java b/src/java/com/threerings/whirled/tools/xml/SceneRuleSet.java
index 360450ba2..2cf5164fa 100644
--- a/src/java/com/threerings/whirled/tools/xml/SceneRuleSet.java
+++ b/src/java/com/threerings/whirled/tools/xml/SceneRuleSet.java
@@ -1,17 +1,16 @@
//
-// $Id: SceneRuleSet.java,v 1.1 2001/11/29 19:31:52 mdb Exp $
+// $Id: SceneRuleSet.java,v 1.2 2001/12/05 03:38:09 mdb Exp $
package com.threerings.whirled.tools.xml;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSetBase;
-import com.samskivert.xml.SetFieldRule;
-
-import com.threerings.whirled.data.SceneModel;
+import com.threerings.whirled.tools.EditableScene;
+import com.threerings.whirled.tools.EditableSceneImpl;
/**
- * Used to parse a {@link SceneModel} from XML.
+ * Used to parse an {@link EditableScene} from XML.
*/
public class SceneRuleSet extends RuleSetBase
{
@@ -21,9 +20,9 @@ public class SceneRuleSet extends RuleSetBase
* scene in the following XML file:
*
*
- * <scene>
- * <sceneId>50</sceneId>
- * <version>50</version>
+ * <scene name="Scene Name" version="3">
+ * <neighbor>North Scene</neighbor>
+ * <neighbor>West Scene</neighbor>
* <!-- ... -->
* </scene>
*
@@ -39,26 +38,21 @@ public class SceneRuleSet extends RuleSetBase
*/
public void addRuleInstances (Digester digester)
{
- // this creates the appropriate instance when we encounter our
- // prefix tag
- digester.addObjectCreate(_prefix, getSceneModelClass().getName());
+ // this creates the appropriate instance when we encounter our tag
+ digester.addObjectCreate(_prefix, getSceneClass().getName());
// set up rules to parse and set our fields
- digester.addRule(_prefix + "/sceneId",
- new SetFieldRule(digester, "sceneId"));
- digester.addRule(_prefix + "/version",
- new SetFieldRule(digester, "version"));
- digester.addRule(_prefix + "/neighborIds",
- new SetFieldRule(digester, "neighborIds"));
+ digester.addSetProperties(_prefix);
+ digester.addCallMethod(_prefix + "/neighbor", "addNeighbor", 0);
}
/**
- * This indicates the class (which should derive from {@link
- * SceneModel}) to be instantiated during the parsing process.
+ * This indicates the class (which should implement {@link
+ * EditableScene}) to be instantiated during the parsing process.
*/
- protected Class getSceneModelClass ()
+ protected Class getSceneClass ()
{
- return SceneModel.class;
+ return EditableSceneImpl.class;
}
/** The prefix at which me match our scenes. */
diff --git a/tests/rsrc/whirled/spot/tools/xml/scene.xml b/tests/rsrc/whirled/spot/tools/xml/scene.xml
new file mode 100644
index 000000000..eb0c4914a
--- /dev/null
+++ b/tests/rsrc/whirled/spot/tools/xml/scene.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/rsrc/whirled/tools/xml/scene.xml b/tests/rsrc/whirled/tools/xml/scene.xml
new file mode 100644
index 000000000..542b407f9
--- /dev/null
+++ b/tests/rsrc/whirled/tools/xml/scene.xml
@@ -0,0 +1,8 @@
+
+
+
+
+ Some neighbor
+ Some other neighbor
+ Yet another neighbor
+
diff --git a/tests/src/java/com/threerings/whirled/spot/tools/xml/SpotSceneParserTest.java b/tests/src/java/com/threerings/whirled/spot/tools/xml/SpotSceneParserTest.java
new file mode 100644
index 000000000..c6fefae10
--- /dev/null
+++ b/tests/src/java/com/threerings/whirled/spot/tools/xml/SpotSceneParserTest.java
@@ -0,0 +1,47 @@
+//
+// $Id: SpotSceneParserTest.java,v 1.1 2001/12/05 03:38:09 mdb Exp $
+
+package com.threerings.whirled.tools.spot.xml;
+
+import com.samskivert.test.TestUtil;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+
+import com.threerings.whirled.tools.spot.EditableSpotScene;
+
+public class SpotSceneParserTest extends TestCase
+{
+ public SpotSceneParserTest ()
+ {
+ super(SpotSceneParserTest.class.getName());
+ }
+
+ public void runTest ()
+ {
+ try {
+ SpotSceneParser parser = new SpotSceneParser("scene");
+ String tspath = TestUtil.getResourcePath(TEST_SCENE_PATH);
+ EditableSpotScene scene = parser.parseScene(tspath);
+ System.out.println("Parsed " + scene.getSpotSceneModel() + ".");
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Test threw exception");
+ }
+ }
+
+ public static Test suite ()
+ {
+ return new SpotSceneParserTest();
+ }
+
+ public static void main (String[] args)
+ {
+ SpotSceneParserTest test = new SpotSceneParserTest();
+ test.runTest();
+ }
+
+ protected static final String TEST_SCENE_PATH =
+ "whirled/tools/spot/xml/scene.xml";
+}
diff --git a/tests/src/java/com/threerings/whirled/tools/xml/SceneParserTest.java b/tests/src/java/com/threerings/whirled/tools/xml/SceneParserTest.java
new file mode 100644
index 000000000..a723a2e18
--- /dev/null
+++ b/tests/src/java/com/threerings/whirled/tools/xml/SceneParserTest.java
@@ -0,0 +1,47 @@
+//
+// $Id: SceneParserTest.java,v 1.1 2001/12/05 03:38:10 mdb Exp $
+
+package com.threerings.whirled.tools.xml;
+
+import com.samskivert.test.TestUtil;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+
+import com.threerings.whirled.tools.EditableScene;
+
+public class SceneParserTest extends TestCase
+{
+ public SceneParserTest ()
+ {
+ super(SceneParserTest.class.getName());
+ }
+
+ public void runTest ()
+ {
+ try {
+ SceneParser parser = new SceneParser("scene");
+ String tspath = TestUtil.getResourcePath(TEST_SCENE_PATH);
+ EditableScene scene = parser.parseScene(tspath);
+ System.out.println("Parsed " + scene.getSceneModel() + ".");
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Test threw exception");
+ }
+ }
+
+ public static Test suite ()
+ {
+ return new SceneParserTest();
+ }
+
+ public static void main (String[] args)
+ {
+ SceneParserTest test = new SceneParserTest();
+ test.runTest();
+ }
+
+ protected static final String TEST_SCENE_PATH =
+ "whirled/tools/xml/scene.xml";
+}