<commit_before>//
// $Id: XMLSceneWriter.java,v 1.7 2001/08/10 21:17:07 shaper Exp $

package com.threerings.miso.scene.xml;

import java.awt.Point;
import java.io.*;
import java.util.ArrayList;

import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;

import com.megginson.sax.DataWriter;

import com.threerings.miso.Log;
import com.threerings.miso.scene.*;
import com.threerings.miso.tile.Tile;

/**
 * The <code>XMLSceneWriter</code> writes a <code>Scene</code> object
 * to an XML file.
 *
 * <p> The scene id is omitted as the scene id is assigned when the
 * scene template is actually loaded into a server.  Similarly, exit
 * points are specified by scene name rather than scene id.
 */
public class XMLSceneWriter extends DataWriter
{
    /**
     * Construct an XMLSceneWriter object.
     */
    public XMLSceneWriter ()
    {
        setIndentStep(2);
    }

    /**
     * Write the scenes to the specified output file in XML format.
     *
     * @param fname the file to write to.
     */
    public void saveScene (Scene scene, String fname) throws IOException
    {
        FileOutputStream fos = new FileOutputStream(fname);
        setOutput(new OutputStreamWriter(fos));

        try {
            startDocument();

            startElement("scene");

            dataElement("name", scene.getName());
            dataElement("version", "" + Scene.VERSION);
            dataElement("locations", getLocationData(scene));

	    startElement("clusters");
	    writeClusters(scene);
	    endElement("clusters");

            dataElement("exits", getExitData(scene));

            startElement("tiles");
            for (int lnum = 0; lnum < Scene.NUM_LAYERS; lnum++) {
                writeLayer(scene, lnum);
            }
            endElement("tiles");

            endElement("scene");
            endDocument();

        } catch (SAXException saxe) {
            Log.warning("Exception writing scene to file " +
                        "[scene=" + scene + ", fname=" + fname +
                        ", saxe=" + saxe + "].");
        }
    }

    /**
     * Output XML detailing the cluster objects.  Clusters are
     * described by a list of location object indexes that are
     * contained within the cluster.
     *
     * @param scene the scene object.
     */
    protected void writeClusters (Scene scene) throws SAXException
    {
	ArrayList clusters = scene.getClusters();
	ArrayList locs = scene.getLocations();

	int size = clusters.size();
	for (int ii = 0; ii < size; ii++) {

	    StringBuffer buf = new StringBuffer();

	    Cluster cluster = (Cluster)clusters.get(ii);
	    ArrayList clusterlocs = cluster.getLocations();

	    int clustersize = clusterlocs.size();
	    for (int jj = 0; jj < clustersize; jj++) {
		buf.append(locs.indexOf(clusterlocs.get(jj)));
		if (jj < clustersize - 1) buf.append(",");
	    }

	    dataElement("cluster", buf.toString());
	}
    }

    /**
     * Output XML detailing the tiles at the specified layer in the
     * given scene.  The first layer is outputted in its entirety,
     * whereas subsequent layers are outputted in a sparse notation
     * that details each contiguous horizontal chunk of tiles in each
     * row separately. 
     *
     * @param scene the scene object.
     * @param lnum the layer number.
     */
    protected void writeLayer (Scene scene, int lnum) throws SAXException
    {
        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "lnum", "", "CDATA", "" + lnum);

        startElement("", "layer", "", attrs);
        for (int yy = 0; yy < Scene.TILE_HEIGHT; yy++) {
            if (lnum == Scene.LAYER_BASE) {
                writeTileRowData(scene, yy, lnum);
            } else {
                writeSparseTileRowData(scene, yy, lnum);
            }
        }
        endElement("layer");
    }

    /**
     * Return a string representation of the exits in the scene.  Each
     * exit is specified by a comma-delimited tuple of (location
     * index, scene name) values.
     *
     * @param scene the scene object.
     *
     * @return the exits in String format.
     */
    protected String getExitData (Scene scene)
    {
	ArrayList locs = scene.getLocations();
	ArrayList exits = scene.getExits();

        StringBuffer buf = new StringBuffer();
	int size = exits.size();
        for (int ii = 0; ii < size; ii++) {
	    Exit exit = (Exit)exits.get(ii);
	    buf.append(locs.indexOf(exit.loc)).append(",");
	    buf.append(exit.name);
            if (ii < size - 1) buf.append(",");
        }

        return buf.toString();
    }

    /**
     * Return a string representation of the locations in the scene.
     * Each location is specified by a comma-delimited triplet of
     * (x, y, orientation) values.
     *
     * @return the locations in String format.
     */
    protected String getLocationData (Scene scene)
    {
	ArrayList locs = scene.getLocations();

        StringBuffer buf = new StringBuffer();
	int size = locs.size();
        for (int ii = 0; ii < size; ii++) {
	    Location loc = (Location)locs.get(ii);
	    buf.append(loc.x).append(",");
	    buf.append(loc.y).append(",");
	    buf.append(loc.orient);
            if (ii < size - 1) buf.append(",");
        }<commit_msg>Fixed reference to old exit member data.<commit_after>//
// $Id: XMLSceneWriter.java,v 1.8 2001/08/11 00:01:40 shaper Exp $

package com.threerings.miso.scene.xml;

import java.awt.Point;
import java.io.*;
import java.util.ArrayList;

import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;

import com.megginson.sax.DataWriter;

import com.threerings.miso.Log;
import com.threerings.miso.scene.*;
import com.threerings.miso.tile.Tile;

/**
 * The <code>XMLSceneWriter</code> writes a <code>Scene</code> object
 * to an XML file.
 *
 * <p> The scene id is omitted as the scene id is assigned when the
 * scene template is actually loaded into a server.  Similarly, exit
 * points are specified by scene name rather than scene id.
 */
public class XMLSceneWriter extends DataWriter
{
    /**
     * Construct an XMLSceneWriter object.
     */
    public XMLSceneWriter ()
    {
        setIndentStep(2);
    }

    /**
     * Write the scenes to the specified output file in XML format.
     *
     * @param fname the file to write to.
     */
    public void saveScene (Scene scene, String fname) throws IOException
    {
        FileOutputStream fos = new FileOutputStream(fname);
        setOutput(new OutputStreamWriter(fos));

        try {
            startDocument();

            startElement("scene");

            dataElement("name", scene.getName());
            dataElement("version", "" + Scene.VERSION);
            dataElement("locations", getLocationData(scene));

	    startElement("clusters");
	    writeClusters(scene);
	    endElement("clusters");

            dataElement("exits", getExitData(scene));

            startElement("tiles");
            for (int lnum = 0; lnum < Scene.NUM_LAYERS; lnum++) {
                writeLayer(scene, lnum);
            }
            endElement("tiles");

            endElement("scene");
            endDocument();

        } catch (SAXException saxe) {
            Log.warning("Exception writing scene to file " +
                        "[scene=" + scene + ", fname=" + fname +
                        ", saxe=" + saxe + "].");
        }
    }

    /**
     * Output XML detailing the cluster objects.  Clusters are
     * described by a list of location object indexes that are
     * contained within the cluster.
     *
     * @param scene the scene object.
     */
    protected void writeClusters (Scene scene) throws SAXException
    {
	ArrayList clusters = scene.getClusters();
	ArrayList locs = scene.getLocations();

	int size = clusters.size();
	for (int ii = 0; ii < size; ii++) {

	    StringBuffer buf = new StringBuffer();

	    Cluster cluster = (Cluster)clusters.get(ii);
	    ArrayList clusterlocs = cluster.getLocations();

	    int clustersize = clusterlocs.size();
	    for (int jj = 0; jj < clustersize; jj++) {
		buf.append(locs.indexOf(clusterlocs.get(jj)));
		if (jj < clustersize - 1) buf.append(",");
	    }

	    dataElement("cluster", buf.toString());
	}
    }

    /**
     * Output XML detailing the tiles at the specified layer in the
     * given scene.  The first layer is outputted in its entirety,
     * whereas subsequent layers are outputted in a sparse notation
     * that details each contiguous horizontal chunk of tiles in each
     * row separately. 
     *
     * @param scene the scene object.
     * @param lnum the layer number.
     */
    protected void writeLayer (Scene scene, int lnum) throws SAXException
    {
        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute("", "lnum", "", "CDATA", "" + lnum);

        startElement("", "layer", "", attrs);
        for (int yy = 0; yy < Scene.TILE_HEIGHT; yy++) {
            if (lnum == Scene.LAYER_BASE) {
                writeTileRowData(scene, yy, lnum);
            } else {
                writeSparseTileRowData(scene, yy, lnum);
            }
        }
        endElement("layer");
    }

    /**
     * Return a string representation of the exits in the scene.  Each
     * exit is specified by a comma-delimited tuple of (location
     * index, scene name) values.
     *
     * @param scene the scene object.
     *
     * @return the exits in String format.
     */
    protected String getExitData (Scene scene)
    {
	ArrayList locs = scene.getLocations();
	ArrayList exits = scene.getExits();

        StringBuffer buf = new StringBuffer();
	int size = exits.size();
        for (int ii = 0; ii < size; ii++) {
	    Exit exit = (Exit)exits.get(ii);
	    buf.append(locs.indexOf(exit)).append(",");
	    buf.append(exit.name);
            if (ii < size - 1) buf.append(",");
        }

        return buf.toString();
    }

    /**
     * Return a string representation of the locations in the scene.
     * Each location is specified by a comma-delimited triplet of
     * (x, y, orientation) values.
     *
     * @return the locations in String format.
     */
    protected String getLocationData (Scene scene)
    {
	ArrayList locs = scene.getLocations();

        StringBuffer buf = new StringBuffer();
	int size = locs.size();
        for (int ii = 0; ii < size; ii++) {
	    Location loc = (Location)locs.get(ii);
	    buf.append(loc.x).append(",");
	    buf.append(loc.y).append(",");
	    buf.append(loc.orient);
            if (ii < size - 1) buf.append(",");
        }
{'fraction_non_alphanumeric': 0.12098226983976486, 'fraction_numerical': 0.004930311937043709, 'mean_word_length': 2.3096956385315344, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7485075', 'n_tokens_mistral': 3177, 'n_tokens_neox': 3017, 'n_words': 926}