Files
narya/src/java/com/threerings/media/tile/XMLTileSetParser.java
T
Walter Korman 2572535f95 General updating per initial code review. Created EditableSceneView
to separate scene display from editing functionality.  Load and
initialize managers in a fashion that more appropriately hides the
implementation details.  Tidied comments up a bit.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@103 542714f4-19e9-0310-aa3c-eee0fc999fb1
2001-07-23 18:52:51 +00:00

104 lines
2.7 KiB
Java

//
// $Id: XMLTileSetParser.java,v 1.5 2001/07/23 18:52:51 shaper Exp $
package com.threerings.miso.tile;
import com.threerings.miso.Log;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.XMLUtil;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
/**
* Parse an XML tileset description file and construct tileset objects
* for each valid description. Does not currently perform validation
* on the input XML stream, though the parsing code assumes the XML
* document is well-formed.
*/
public class XMLTileSetParser extends DefaultHandler
implements TileSetParser
{
public void startElement (String uri, String localName,
String qName, Attributes attributes)
{
_tag = qName;
}
public void endElement (String uri, String localName, String qName)
{
// construct the tileset object on tag close
if (qName.equals("tileset")) {
TileSet tset = new TileSet(
_tsName, _tsTsid, _tsImgFile, _tsRowHeight, _tsTileCount);
_tilesets.add(tset);
}
// note that we're not within a tag to avoid considering any
// characters during this quiescent time
_tag = null;
}
public void characters (char ch[], int start, int length)
{
// bail if we're not within a meaningful tag
if (_tag == null) return;
String str = String.copyValueOf(ch, start, length);
// store the value associated with the current tag for use
// when we construct the tileset object.
if (_tag.equals("name")) {
_tsName = str;
} else if (_tag.equals("tsid")) {
try {
_tsTsid = Integer.parseInt(str);
} catch (NumberFormatException nfe) {
Log.warning("Malformed integer tilesetid [str=" + str + "].");
_tsTsid = -1;
}
} else if (_tag.equals("imagefile")) {
_tsImgFile = str;
} else if (_tag.equals("rowheight")) {
_tsRowHeight = StringUtil.parseIntArray(str);
} else if (_tag.equals("tilecount")) {
_tsTileCount = StringUtil.parseIntArray(str);
}
}
public ArrayList loadTileSets (InputStream tis) throws IOException
{
try {
XMLUtil.parse(this, tis);
return _tilesets;
} catch (ParserConfigurationException pce) {
throw new IOException(pce.toString());
} catch (SAXException saxe) {
throw new IOException(saxe.toString());
}
}
/** The XML element tag currently being processed. */
protected String _tag;
/** The tilesets constructed thus far. */
protected ArrayList _tilesets = new ArrayList();
// temporary storage of tileset object values
protected String _tsName;
protected int _tsTsid;
protected String _tsImgFile;
protected int[] _tsRowHeight, _tsTileCount;
}