Combined display/runtime/editable scenes; revamped parser system.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2272 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
//
|
||||
// $Id: MisoSceneParser.java,v 1.3 2003/01/31 23:10:46 mdb Exp $
|
||||
|
||||
package com.threerings.miso.tools.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
import org.apache.commons.digester.Digester;
|
||||
import org.apache.commons.digester.RuleSet;
|
||||
|
||||
import com.threerings.miso.data.MisoSceneModel;
|
||||
|
||||
/**
|
||||
* A simple class for parsing a standalone miso scene model.
|
||||
*/
|
||||
public class MisoSceneParser
|
||||
{
|
||||
/**
|
||||
* Constructs a miso scene parser that parses scenes with the
|
||||
* specified XML path prefix and a standard scene rule set. See the
|
||||
* {@link MisoSceneRuleSet#MisoSceneRuleSet} documentation for more
|
||||
* information.
|
||||
*/
|
||||
public MisoSceneParser (String prefix)
|
||||
{
|
||||
MisoSceneRuleSet set = new MisoSceneRuleSet();
|
||||
set.setPrefix(prefix);
|
||||
init(prefix, set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a miso scene parser that parses scenes with the
|
||||
* specified XML path prefix, using the supplied rule set. The rule
|
||||
* set should leave a {@link MisoSceneModel} at the top of the
|
||||
* digester's stack.
|
||||
*/
|
||||
public MisoSceneParser (String prefix, RuleSet rules)
|
||||
{
|
||||
init(prefix, rules);
|
||||
}
|
||||
|
||||
/** Constructor helper function. */
|
||||
protected void init (String prefix, RuleSet rules)
|
||||
{
|
||||
// create and configure our digester
|
||||
_digester = new Digester();
|
||||
_digester.addRuleSet(rules);
|
||||
_digester.addSetNext(prefix, "setSceneModel",
|
||||
MisoSceneModel.class.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the XML file at the specified path into a miso scene model.
|
||||
*/
|
||||
public MisoSceneModel parseScene (String path)
|
||||
throws IOException, SAXException
|
||||
{
|
||||
_model = null;
|
||||
_digester.push(this);
|
||||
_digester.parse(new FileInputStream(path));
|
||||
return _model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the parser once the scene is parsed.
|
||||
*/
|
||||
public void setSceneModel (MisoSceneModel model)
|
||||
{
|
||||
_model = model;
|
||||
}
|
||||
|
||||
protected Digester _digester;
|
||||
protected MisoSceneModel _model;
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
//
|
||||
// $Id: MisoSceneRuleSet.java,v 1.12 2003/01/31 23:10:46 mdb Exp $
|
||||
|
||||
package com.threerings.miso.tools.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.commons.digester.Digester;
|
||||
import org.apache.commons.digester.RuleSetBase;
|
||||
|
||||
import com.samskivert.xml.CallMethodSpecialRule;
|
||||
import com.samskivert.xml.SetFieldRule;
|
||||
import com.samskivert.xml.SetPropertyFieldsRule;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
import com.threerings.miso.data.MisoSceneModel;
|
||||
import com.threerings.miso.data.ObjectInfo;
|
||||
|
||||
/**
|
||||
* Used to parse a {@link MisoSceneModel} from XML.
|
||||
*/
|
||||
public class MisoSceneRuleSet extends RuleSetBase
|
||||
{
|
||||
/**
|
||||
* Configures the rule set to match scenes with the supplied prefix.
|
||||
* For example, passing <code>scene.miso</code> will match the scene
|
||||
* in the following XML file:
|
||||
*
|
||||
* <pre>
|
||||
* <scene>
|
||||
* <miso>
|
||||
* <width>50</width>
|
||||
* <height>50</height>
|
||||
* <!-- ... -->
|
||||
* </miso>
|
||||
* </scene>
|
||||
* </pre>
|
||||
*/
|
||||
public void setPrefix (String prefix)
|
||||
{
|
||||
_prefix = prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the necessary rules to the digester to parse our miso scene
|
||||
* data.
|
||||
*/
|
||||
public void addRuleInstances (Digester digester)
|
||||
{
|
||||
// this creates the appropriate instance when we encounter our
|
||||
// prefix tag
|
||||
digester.addObjectCreate(_prefix, MisoSceneModel.class.getName());
|
||||
|
||||
// set up rules to parse and set our fields
|
||||
digester.addRule(_prefix + "/width",
|
||||
new SetFieldRule(digester, "width"));
|
||||
digester.addRule(_prefix + "/height",
|
||||
new SetFieldRule(digester, "height"));
|
||||
digester.addRule(_prefix + "/viewwidth",
|
||||
new SetFieldRule(digester, "vwidth"));
|
||||
digester.addRule(_prefix + "/viewheight",
|
||||
new SetFieldRule(digester, "vheight"));
|
||||
digester.addRule(_prefix + "/base",
|
||||
new SetFieldRule(digester, "baseTileIds"));
|
||||
|
||||
digester.addObjectCreate(_prefix + "/objects",
|
||||
ArrayList.class.getName());
|
||||
digester.addObjectCreate(_prefix + "/objects/object",
|
||||
ObjectInfo.class.getName());
|
||||
digester.addSetNext(_prefix + "/objects/object", "add",
|
||||
Object.class.getName());
|
||||
|
||||
digester.addRule(_prefix + "/objects/object",
|
||||
new SetPropertyFieldsRule(digester));
|
||||
|
||||
digester.addRule(_prefix + "/objects", new CallMethodSpecialRule(
|
||||
digester) {
|
||||
public void parseAndSet (String bodyText, Object target)
|
||||
throws Exception
|
||||
{
|
||||
ArrayList ilist = (ArrayList)target;
|
||||
ArrayList ulist = new ArrayList();
|
||||
MisoSceneModel model = (MisoSceneModel)this.digester.peek(1);
|
||||
|
||||
// filter interesting and uninteresting into two lists
|
||||
for (int ii = 0; ii < ilist.size(); ii++) {
|
||||
ObjectInfo info = (ObjectInfo)ilist.get(ii);
|
||||
if (!info.isInteresting()) {
|
||||
ilist.remove(ii--);
|
||||
ulist.add(info);
|
||||
}
|
||||
}
|
||||
|
||||
// now populate the model
|
||||
MisoSceneModel.populateObjects(model, ilist, ulist);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** The prefix at which me match our scenes. */
|
||||
protected String _prefix;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// $Id: SimpleMisoSceneRuleSet.java,v 1.1 2003/02/12 07:21:50 mdb Exp $
|
||||
|
||||
package com.threerings.miso.tools.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import org.xml.sax.Attributes;
|
||||
|
||||
import org.apache.commons.digester.Digester;
|
||||
import org.apache.commons.digester.Rule;
|
||||
|
||||
import com.samskivert.xml.CallMethodSpecialRule;
|
||||
import com.samskivert.xml.SetFieldRule;
|
||||
import com.samskivert.xml.SetPropertyFieldsRule;
|
||||
|
||||
import com.threerings.tools.xml.NestableRuleSet;
|
||||
|
||||
import com.threerings.miso.data.ObjectInfo;
|
||||
import com.threerings.miso.data.SimpleMisoSceneModel;
|
||||
|
||||
/**
|
||||
* Used to parse a {@link SimpleMisoSceneModel} from XML.
|
||||
*/
|
||||
public class SimpleMisoSceneRuleSet implements NestableRuleSet
|
||||
{
|
||||
// documentation inherited from interface
|
||||
public String getOuterElement ()
|
||||
{
|
||||
return SimpleMisoSceneWriter.OUTER_ELEMENT;
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public void addRuleInstances (String prefix, Digester dig)
|
||||
{
|
||||
// this creates the appropriate instance when we encounter our
|
||||
// prefix tag
|
||||
dig.addRule(prefix, new Rule(dig) {
|
||||
public void begin (Attributes attributes) throws Exception {
|
||||
digester.push(createMisoSceneModel());
|
||||
}
|
||||
public void end () throws Exception {
|
||||
digester.pop();
|
||||
}
|
||||
});
|
||||
|
||||
// set up rules to parse and set our fields
|
||||
dig.addRule(prefix + "/width", new SetFieldRule(dig, "width"));
|
||||
dig.addRule(prefix + "/height", new SetFieldRule(dig, "height"));
|
||||
dig.addRule(prefix + "/viewwidth", new SetFieldRule(dig, "vwidth"));
|
||||
dig.addRule(prefix + "/viewheight", new SetFieldRule(dig, "vheight"));
|
||||
dig.addRule(prefix + "/base", new SetFieldRule(dig, "baseTileIds"));
|
||||
|
||||
dig.addObjectCreate(prefix + "/objects", ArrayList.class.getName());
|
||||
dig.addObjectCreate(prefix + "/objects/object",
|
||||
ObjectInfo.class.getName());
|
||||
dig.addSetNext(prefix + "/objects/object", "add",
|
||||
Object.class.getName());
|
||||
|
||||
dig.addRule(prefix + "/objects/object",
|
||||
new SetPropertyFieldsRule(dig));
|
||||
|
||||
dig.addRule(prefix + "/objects", new CallMethodSpecialRule(dig) {
|
||||
public void parseAndSet (String bodyText, Object target)
|
||||
throws Exception
|
||||
{
|
||||
ArrayList ilist = (ArrayList)target;
|
||||
ArrayList ulist = new ArrayList();
|
||||
SimpleMisoSceneModel model = (SimpleMisoSceneModel)
|
||||
digester.peek(1);
|
||||
|
||||
// filter interesting and uninteresting into two lists
|
||||
for (int ii = 0; ii < ilist.size(); ii++) {
|
||||
ObjectInfo info = (ObjectInfo)ilist.get(ii);
|
||||
if (!info.isInteresting()) {
|
||||
ilist.remove(ii--);
|
||||
ulist.add(info);
|
||||
}
|
||||
}
|
||||
|
||||
// now populate the model
|
||||
SimpleMisoSceneModel.populateObjects(model, ilist, ulist);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected SimpleMisoSceneModel createMisoSceneModel ()
|
||||
{
|
||||
return new SimpleMisoSceneModel(0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
+16
-15
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: MisoSceneWriter.java,v 1.9 2003/01/31 23:10:46 mdb Exp $
|
||||
// $Id: SimpleMisoSceneWriter.java,v 1.1 2003/02/12 07:21:50 mdb Exp $
|
||||
|
||||
package com.threerings.miso.tools.xml;
|
||||
|
||||
@@ -8,35 +8,36 @@ 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.miso.data.MisoSceneModel;
|
||||
import com.threerings.miso.data.ObjectInfo;
|
||||
import com.threerings.miso.data.SimpleMisoSceneModel;
|
||||
|
||||
/**
|
||||
* Generates an XML representation of a {@link MisoSceneModel}.
|
||||
* Generates an XML representation of a {@link SimpleMisoSceneModel}.
|
||||
*/
|
||||
public class MisoSceneWriter
|
||||
public class SimpleMisoSceneWriter implements NestableWriter
|
||||
{
|
||||
/**
|
||||
* Writes the data for the supplied {@link MisoSceneModel} 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 miso data should live.
|
||||
*/
|
||||
public void writeScene (MisoSceneModel model, DataWriter writer)
|
||||
/** The element used to enclose scene models written with this
|
||||
* writer. */
|
||||
public static final String OUTER_ELEMENT = "miso";
|
||||
|
||||
// documentation inherited from interface
|
||||
public void write (Object object, DataWriter writer)
|
||||
throws SAXException
|
||||
{
|
||||
writer.startElement("miso");
|
||||
SimpleMisoSceneModel model = (SimpleMisoSceneModel)object;
|
||||
writer.startElement(OUTER_ELEMENT);
|
||||
writeSceneData(model, writer);
|
||||
writer.endElement("miso");
|
||||
writer.endElement(OUTER_ELEMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (MisoSceneModel model, DataWriter writer)
|
||||
protected void writeSceneData (SimpleMisoSceneModel model,
|
||||
DataWriter writer)
|
||||
throws SAXException
|
||||
{
|
||||
writer.dataElement("width", Integer.toString(model.width));
|
||||
Reference in New Issue
Block a user