Modified scene writer code.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@742 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-12-05 04:45:13 +00:00
parent 3af703d31b
commit a8e66a4191
2 changed files with 102 additions and 49 deletions
@@ -1,59 +1,86 @@
// //
// $Id: SpotSceneWriter.java,v 1.2 2001/11/29 06:38:35 mdb Exp $ // $Id: SpotSceneWriter.java,v 1.3 2001/12/05 04:45:13 mdb Exp $
package com.threerings.whirled.tools.spot.xml; package com.threerings.whirled.tools.spot.xml;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import com.megginson.sax.DataWriter; import com.megginson.sax.DataWriter;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
import com.threerings.whirled.spot.data.Location;
import com.threerings.whirled.spot.data.Portal;
import com.threerings.whirled.tools.EditableScene;
import com.threerings.whirled.tools.spot.EditablePortal;
import com.threerings.whirled.tools.spot.EditableSpotScene;
import com.threerings.whirled.tools.xml.SceneWriter; import com.threerings.whirled.tools.xml.SceneWriter;
import com.threerings.whirled.spot.data.SpotSceneModel;
/** /**
* Generates an XML representation of a {@link SpotSceneModel}. * Generates an XML representation of an {@link EditableSpotScene}.
*/ */
public class SpotSceneWriter extends SceneWriter public class SpotSceneWriter extends SceneWriter
{ {
/** protected void addSceneAttributes (
* Writes the data for the supplied {@link SpotSceneModel} to the XML EditableScene scene, AttributesImpl attrs)
* writer supplied. The writer will already be configured with the {
* appropriate indentation level so that this writer can simply output super.addSceneAttributes(scene, attrs);
* its elements and allow the calling code to determine where in the EditableSpotScene sscene = (EditableSpotScene)scene;
* greater scene description file the scene data should live. attrs.addAttribute("", "defaultEntranceId", "", "",
*/ Integer.toString(sscene.getDefaultEntranceId()));
public void writeScene (SpotSceneModel model, DataWriter writer) }
protected void writeSceneData (EditableScene scene, DataWriter writer)
throws SAXException throws SAXException
{ {
writer.startElement("spot"); // we don't want to write our superclass scene data because it
writeSceneData(model, writer); // writes out neighbors info which we deal with differently, so we
writer.endElement("spot"); // mean not to call super.writeSceneData()
EditableSpotScene sscene = (EditableSpotScene)scene;
// write out the location info
Location[] locs = sscene.getLocations();
for (int i = 0; i < locs.length; i++) {
Location loc = locs[i];
// skip portals as we'll get those on the next run
if (loc instanceof EditablePortal) {
continue;
}
AttributesImpl attrs = new AttributesImpl();
addSharedAttrs(attrs, loc);
attrs.addAttribute("", "clusterIndex", "", "",
Integer.toString(loc.clusterIndex));
writer.emptyElement("", "location", "", attrs);
}
// write out the portal info
Portal[] ports = sscene.getPortals();
for (int i = 0; i < ports.length; i++) {
EditablePortal port = (EditablePortal)ports[i];
AttributesImpl attrs = new AttributesImpl();
addSharedAttrs(attrs, port);
attrs.addAttribute("", "targetSceneName", "", "",
port.targetSceneName);
attrs.addAttribute("", "targetPortalName", "", "",
port.targetPortalName);
writer.emptyElement("", "location", "", attrs);
}
} }
/** /**
* Writes just the scene data which is handy for derived classes which * Adds the attributes that are shared between location and portal
* may wish to add their own scene data to the scene output. * elements.
*/ */
protected void writeSceneData (SpotSceneModel model, DataWriter writer) protected void addSharedAttrs (AttributesImpl attrs, Location loc)
throws SAXException
{ {
super.writeSceneData(model, writer); attrs.addAttribute("", "locationId", "", "",
Integer.toString(loc.locationId));
writer.dataElement("locationIds", attrs.addAttribute("", "x", "", "", Integer.toString(loc.x));
StringUtil.toString(model.locationIds, "", "")); attrs.addAttribute("", "y", "", "", Integer.toString(loc.y));
writer.dataElement("locationX", attrs.addAttribute("", "orientation", "", "",
StringUtil.toString(model.locationX, "", "")); Integer.toString(loc.orientation));
writer.dataElement("locationY",
StringUtil.toString(model.locationY, "", ""));
writer.dataElement("locationOrients",
StringUtil.toString(model.locationOrients, "", ""));
writer.dataElement("locationClusters",
StringUtil.toString(model.locationClusters, "", ""));
writer.dataElement("defaultEntranceId",
Integer.toString(model.defaultEntranceId));
writer.dataElement("portalIds",
StringUtil.toString(model.portalIds, "", ""));
writer.dataElement("targetLocIds",
StringUtil.toString(model.targetLocIds, "", ""));
} }
} }
@@ -1,44 +1,70 @@
// //
// $Id: SceneWriter.java,v 1.1 2001/11/29 06:36:29 mdb Exp $ // $Id: SceneWriter.java,v 1.2 2001/12/05 04:45:13 mdb Exp $
package com.threerings.whirled.tools.xml; package com.threerings.whirled.tools.xml;
import java.util.Iterator;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import com.megginson.sax.DataWriter; import com.megginson.sax.DataWriter;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
import com.threerings.whirled.data.SceneModel; import com.threerings.whirled.tools.EditableScene;
/** /**
* Generates an XML representation of a {@link SceneModel}. * Generates an XML representation of an {@link EditableScene}.
*/ */
public class SceneWriter public class SceneWriter
{ {
/** /**
* Writes the data for the supplied {@link SceneModel} to the XML * Writes the data for the supplied {@link EditableScene} to the XML
* writer supplied. The writer will already be configured with the * writer supplied. The writer will already be configured with the
* appropriate indentation level so that this writer can simply output * appropriate indentation level so that this writer can simply output
* its elements and allow the calling code to determine where in the * its elements and allow the calling code to determine where in the
* greater scene description file the scene data should live. * greater scene description file the scene data should live.
*/ */
public void writeScene (SceneModel model, DataWriter writer) public void writeScene (EditableScene scene, DataWriter writer)
throws SAXException throws SAXException
{ {
writer.startElement("scene"); AttributesImpl attrs = new AttributesImpl();
writeSceneData(model, writer); addSceneAttributes(scene, attrs);
writer.endElement("scene"); writer.startElement("", sceneElementName(), "", attrs);
writeSceneData(scene, writer);
writer.endElement(sceneElementName());
}
/**
* Returns the name of the top-level element that we'll use when we
* write out the scene (defaults to <code>scene</code>).
*/
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 * Writes just the scene data which is handy for derived classes which
* may wish to add their own scene data to the scene output. * may wish to add their own scene data to the scene output.
*/ */
protected void writeSceneData (SceneModel model, DataWriter writer) protected void writeSceneData (EditableScene scene, DataWriter writer)
throws SAXException throws SAXException
{ {
writer.dataElement("sceneId", Integer.toString(model.sceneId)); Iterator iter = scene.getNeighborNames().iterator();
writer.dataElement("version", Integer.toString(model.version)); while (iter.hasNext()) {
writer.dataElement("neighborIds", writer.dataElement("neighbor", (String)iter.next());
StringUtil.toString(model.neighborIds, "", "")); }
} }
} }