The amazing refactor forteen hundred billion: eliminated "locations",

portals will by dynamically created, combined display/runtime/editable
scenes into one, enhanced support for modifying scenes and distributing
updates to the clients, various other small stuff that should not be
sweated.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2274 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-02-12 07:23:32 +00:00
parent a54b3e279c
commit 2a4385ad33
67 changed files with 2073 additions and 2724 deletions
@@ -1,5 +1,5 @@
//
// $Id: SceneParser.java,v 1.1 2001/12/05 03:38:09 mdb Exp $
// $Id: SceneParser.java,v 1.2 2003/02/12 07:23:31 mdb Exp $
package com.threerings.whirled.tools.xml;
@@ -9,8 +9,10 @@ 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;
import com.samskivert.util.StringUtil;
import com.threerings.tools.xml.NestableRuleSet;
import com.threerings.whirled.data.AuxModel;
import com.threerings.whirled.data.SceneModel;
/**
* A simple class for parsing an editable scene instance.
@@ -19,40 +21,75 @@ 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.
* path prefix.
*/
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());
// create our scene rule set
SceneRuleSet set = createSceneRuleSet();
// configure our top-level path prefix
if (StringUtil.blank(prefix)) {
_prefix = set.getOuterElement();
} else {
_prefix = prefix + "/" + set.getOuterElement();
}
// add the scene rules
set.addRuleInstances(_prefix, _digester);
// add a rule to grab the finished scene model
_digester.addSetNext(_prefix, "setScene", SceneModel.class.getName());
}
/**
* Parses the XML file at the specified path into an editable scene
* Creates the rule set used to parse our scene.
*/
protected SceneRuleSet createSceneRuleSet ()
{
return new SceneRuleSet();
}
/**
* Adds a {@link NestableRuleSet} for parsing auxiliary scene models.
*/
public void registerAuxRuleSet (NestableRuleSet set)
{
// add their outer element to the prefix
String prefix = _prefix + "/" + set.getOuterElement();
// add the rules to generate the aux scene model
set.addRuleInstances(prefix, _digester);
// and add a rule to grab it
_digester.addSetNext(prefix, "addAuxModel", AuxModel.class.getName());
}
/**
* Parses the XML file at the specified path into a scene model
* instance.
*/
public EditableScene parseScene (String path)
public SceneModel parseScene (String path)
throws IOException, SAXException
{
_scene = null;
_model = null;
_digester.push(this);
_digester.parse(new FileInputStream(path));
return _scene;
return _model;
}
/**
* Called by the parser once the scene is parsed.
*/
public void setScene (EditableScene scene)
public void setScene (SceneModel model)
{
_scene = scene;
_model = model;
}
protected String _prefix;
protected Digester _digester;
protected EditableScene _scene;
protected SceneModel _model;
}
@@ -1,63 +1,43 @@
//
// $Id: SceneRuleSet.java,v 1.2 2001/12/05 03:38:09 mdb Exp $
// $Id: SceneRuleSet.java,v 1.3 2003/02/12 07:23:31 mdb Exp $
package com.threerings.whirled.tools.xml;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSetBase;
import com.threerings.whirled.tools.EditableScene;
import com.threerings.whirled.tools.EditableSceneImpl;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.SetPropertyFieldsRule;
import com.threerings.tools.xml.NestableRuleSet;
import com.threerings.whirled.data.SceneModel;
/**
* Used to parse an {@link EditableScene} from XML.
* Used to parse a {@link SceneModel} from XML.
*/
public class SceneRuleSet extends RuleSetBase
public class SceneRuleSet implements NestableRuleSet
{
/**
* Configures this scene rule set to match scenes with the supplied
* prefix. For example, passing <code>scene</code> will match the
* scene in the following XML file:
*
* <pre>
* &lt;scene name="Scene Name" version="3"&gt;
* &lt;neighbor&gt;North Scene&lt;/neighbor&gt;
* &lt;neighbor&gt;West Scene&lt;/neighbor&gt;
* &lt;!-- ... --&gt;
* &lt;/scene&gt;
* </pre>
*/
public void setPrefix (String prefix)
// documentation inherited from interface
public String getOuterElement ()
{
_prefix = prefix;
return SceneWriter.OUTER_ELEMENT;
}
/**
* Adds the necessary rules to the digester to parse our miso scene
* data.
*/
public void addRuleInstances (Digester digester)
// documentation inherited from interface
public void addRuleInstances (String prefix, Digester digester)
{
// this creates the appropriate instance when we encounter our tag
digester.addObjectCreate(_prefix, getSceneClass().getName());
digester.addObjectCreate(prefix, getSceneClass().getName());
// set up rules to parse and set our fields
digester.addSetProperties(_prefix);
digester.addCallMethod(_prefix + "/neighbor", "addNeighbor", 0);
digester.addRule(prefix, new SetPropertyFieldsRule(digester));
}
/**
* This indicates the class (which should implement {@link
* EditableScene}) to be instantiated during the parsing process.
* This indicates the class (which should extend {@link SceneModel})
* to be instantiated during the parsing process.
*/
protected Class getSceneClass ()
{
return EditableSceneImpl.class;
return SceneModel.class;
}
/** The prefix at which me match our scenes. */
protected String _prefix = DEFAULT_SCENE_PREFIX;
/** The default prefix which matches &lt;scene&gt;. */
protected static final String DEFAULT_SCENE_PREFIX = "scene";
}
@@ -1,11 +1,12 @@
//
// $Id: SceneWriter.java,v 1.3 2003/01/30 19:16:50 mdb Exp $
// $Id: SceneWriter.java,v 1.4 2003/02/12 07:23:32 mdb Exp $
package com.threerings.whirled.tools.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import org.xml.sax.SAXException;
@@ -13,78 +14,92 @@ import org.xml.sax.helpers.AttributesImpl;
import com.megginson.sax.DataWriter;
import com.samskivert.util.StringUtil;
import com.threerings.tools.xml.NestableWriter;
import com.threerings.whirled.tools.EditableScene;
import com.threerings.whirled.Log;
import com.threerings.whirled.data.AuxModel;
import com.threerings.whirled.data.SceneModel;
/**
* Generates an XML representation of an {@link EditableScene}.
* Generates an XML representation of an {@link SceneModel}.
*/
public class SceneWriter
{
/** The outer element used to enclose our scene definition. */
public static final String OUTER_ELEMENT = "scene";
/**
* Writes the data for the supplied {@link EditableScene} to the XML
* writer supplied. The writer will already be configured with the
* appropriate indentation level so that this writer can simply output
* its elements and allow the calling code to determine where in the
* greater scene description file the scene data should live.
* Registers a writer for writing auxiliary scene models of the
* supplied class.
*/
public void writeScene (EditableScene scene, DataWriter writer)
throws SAXException
public void registerAuxWriter (Class aclass, NestableWriter writer)
{
AttributesImpl attrs = new AttributesImpl();
addSceneAttributes(scene, attrs);
writer.startElement("", sceneElementName(), "", attrs);
writeSceneData(scene, writer);
writer.endElement(sceneElementName());
_auxers.put(aclass, writer);
}
/**
* Returns the name of the top-level element that we'll use when we
* write out the scene (defaults to <code>scene</code>).
* Writes the supplied scene out to the specified file.
*/
protected String sceneElementName ()
{
return "scene";
}
/**
* Adds attributes to the top-level element before it gets written.
*/
protected void addSceneAttributes (
EditableScene scene, AttributesImpl attrs)
{
attrs.addAttribute("", "name", "", "", scene.getName());
attrs.addAttribute("", "version", "", "",
Integer.toString(scene.getVersion()));
}
/**
* Writes just the scene data which is handy for derived classes which
* may wish to add their own scene data to the scene output.
*/
protected void writeSceneData (EditableScene scene, DataWriter writer)
throws SAXException
{
Iterator iter = scene.getNeighborNames().iterator();
while (iter.hasNext()) {
writer.dataElement("neighbor", (String)iter.next());
}
}
/**
* Writes the supplied scene out to the specified file using the
* supplied scene writer.
*/
public static void writeScene (File out, SceneWriter writer,
EditableScene scene)
public void writeScene (File out, SceneModel model)
throws IOException, SAXException
{
FileWriter fout = new FileWriter(out);
DataWriter dout = new DataWriter(fout);
dout.setIndentStep(2);
dout.startDocument();
writer.writeScene(scene, dout);
writeSceneModel(model, dout);
dout.endDocument();
fout.close();
}
/**
* Writes the data for the supplied {@link SceneModel} to the XML data
* writer supplied. The writer should already be configured with the
* appropriate indentation level so that this writer can simply output
* its elements and allow the calling code to determine where in the
* greater scene description file the scene data should live.
*/
public void writeSceneModel (SceneModel model, DataWriter writer)
throws SAXException
{
AttributesImpl attrs = new AttributesImpl();
addSceneAttributes(model, attrs);
writer.startElement("", OUTER_ELEMENT, "", attrs);
writeSceneData(model, writer);
writer.endElement(OUTER_ELEMENT);
}
/**
* Adds attributes to the top-level element before it gets written.
*/
protected void addSceneAttributes (
SceneModel model, AttributesImpl attrs)
{
attrs.addAttribute("", "name", "", "", model.name);
attrs.addAttribute("", "version", "", "",
Integer.toString(model.version));
}
/**
* Writes just the scene data which is handy for derived classes which
* may wish to add their own scene data to the scene output.
*/
protected void writeSceneData (SceneModel model, DataWriter writer)
throws SAXException
{
// write out our auxiliary scene models
for (int ii = 0; ii < model.auxModels.length; ii++) {
AuxModel amodel = model.auxModels[ii];
NestableWriter awriter = (NestableWriter)
_auxers.get(amodel.getClass());
if (awriter != null) {
awriter.write(amodel, writer);
} else {
Log.warning("No writer registered for auxiliary scene model " +
"[mclass=" + amodel.getClass() + "].");
}
}
}
protected HashMap _auxers = new HashMap();
}