//
// $Id: SpotSceneWriter.java,v 1.5 2001/12/07 05:11:25 mdb Exp $

package com.threerings.whirled.tools.spot.xml;

import java.util.Iterator;

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

import com.megginson.sax.DataWriter;
import com.samskivert.util.StringUtil;

import com.threerings.whirled.spot.data.Location;
import com.threerings.whirled.spot.data.Portal;

import com.threerings.whirled.Log;
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;

/**
 * Generates an XML representation of an {@link EditableSpotScene}.
 */
public class SpotSceneWriter extends SceneWriter
{
    protected void addSceneAttributes (
        EditableScene scene, AttributesImpl attrs)
    {
        super.addSceneAttributes(scene, attrs);
        EditableSpotScene sscene = (EditableSpotScene)scene;
        attrs.addAttribute("", "defaultEntranceId", "", "",
                           Integer.toString(sscene.getDefaultEntranceId()));
    }

    protected void writeSceneData (EditableScene scene, DataWriter writer)
        throws SAXException
    {
        // we don't want to write our superclass scene data because it
        // writes out neighbors info which we deal with differently, so we
        // mean not to call super.writeSceneData()

        EditableSpotScene sscene = (EditableSpotScene)scene;

        // write out the location info
        Iterator iter = sscene.getLocations().iterator();
        while (iter.hasNext()) {
            Location loc = (Location)iter.next();
            // 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
        iter = sscene.getPortals().iterator();
        while (iter.hasNext()) {
            EditablePortal port = (EditablePortal)iter.next();
            AttributesImpl attrs = new AttributesImpl();
            addSharedAttrs(attrs, port);
            if (port.targetSceneName != null) {
                attrs.addAttribute("", "targetSceneName", "", "",
                                   port.targetSceneName);
            }
            if (port.targetPortalName != null) {
                attrs.addAttribute("", "targetPortalName", "", "",
                                   port.targetPortalName);
            }
            writer.emptyElement("", "portal", "", attrs);
        }
    }

    /**
     * Adds the attributes that are shared between location and portal
     * elements.
     */
    protected void addSharedAttrs (AttributesImpl attrs, Location loc)
    {
        attrs.addAttribute("", "locationId", "", "",
                           Integer.toString(loc.locationId));
        attrs.addAttribute("", "x", "", "", Integer.toString(loc.x));
        attrs.addAttribute("", "y", "", "", Integer.toString(loc.y));
        attrs.addAttribute("", "orientation", "", "",
                           Integer.toString(loc.orientation));
    }
}

@@ -29,17 +29,17 @@
 ava,v 1.
-5
+6
  2001/12
@@ -50,12 +50,12 @@
 05:1
-1:25
+4:57
  mdb
@@ -2416,198 +2416,174 @@
     
-if (port.targetSceneName != null) %7B%0A                attrs.addAttribute(%22%22, %22targetSceneName%22, %22%22, %22%22,%0A                                   port.targetSceneName);%0A            %7D%0A            if (
+maybeAddAttr(attrs, %22name%22, port.name);%0A            maybeAddAttr(attrs, %22targetSceneName%22, port.targetSceneName);%0A            maybeAddAttr(attrs, %22targetPortalName%22, 
 port
@@ -2603,19 +2603,10 @@
 Name
- != null) %7B
+);
 %0A   
@@ -2618,191 +2618,361 @@
     
-    attrs.addAttribute(%22%22, %22targetP
+writer.emptyElement(%22%22, %22p
 ortal
-Name
 %22, %22%22, 
-%22%22,
+attrs);%0A        %7D
 %0A    
+%7D%0A%0A
     
+/**%0A
      
-                      port.targetPortalName);%0A            %7D%0A            writer.emptyElement(%22%22, %22portal
+* Adds the supplied attribute to the attributes object iff the value%0A     * is non-null.%0A     */%0A    protected void maybeAddAttr (%0A        AttributesImpl attrs, String name, String value)%0A    %7B%0A        if (value != null) %7B%0A            attrs.addAttribute(%22%22, name, %22
 %22, %22%22, 
-attrs
+value
 );%0A 

Was missing the name attribute.