//========================================================================
//Copyright 2007-2010 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package com.dyuproject.protostuff;

import static com.dyuproject.protostuff.StringSerializer.STRING;

import java.io.IOException;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

/**
 * An output used for writing data with xml format.
 *
 * @author David Yu
 * @created May 24, 2010
 */
public final class XmlOutput implements Output
{
    
    private final XMLStreamWriter writer;
    private Schema<?> schema;
    
    public XmlOutput(XMLStreamWriter writer)
    {
        this.writer = writer;
    }
    
    public XmlOutput use(Schema<?> schema)
    {
        this.schema = schema;
        return this;
    }
    
    private void write(String name, String value) throws IOException
    {
        try
        {
            writer.writeStartElement(name);
            writer.writeCharacters(value);
            writer.writeEndElement();
        }
        catch (XMLStreamException e)
        {
            throw new XmlOutputException(e);
        }
    }
    
    public void writeInt32(int fieldNumber, int value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeUInt32(int fieldNumber, int value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeSInt32(int fieldNumber, int value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeFixed32(int fieldNumber, int value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeSFixed32(int fieldNumber, int value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeInt64(int fieldNumber, long value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeUInt64(int fieldNumber, long value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeSInt64(int fieldNumber, long value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeFixed64(int fieldNumber, long value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeSFixed64(int fieldNumber, long value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeFloat(int fieldNumber, float value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeDouble(int fieldNumber, double value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeBool(int fieldNumber, boolean value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeEnum(int fieldNumber, int value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), String.valueOf(value));
    }
    
    public void writeString(int fieldNumber, String value, boolean repeated) throws IOException
    {
        write(schema.getFieldName(fieldNumber), value);
    }
    
    public void writeBytes(int fieldNumber, ByteString value, boolean repeated) throws IOException
    {
        writeByteArray(fieldNumber, value.getBytes(), repeated);
    }
    
    public void writeByteArray(int fieldNumber, byte[] value, boolean repeated) throws IOException
    {
        try
        {
            writer.writeStartElement(schema.getFieldName(fieldNumber));
            writer.writeCData(STRING.deser(value));
            writer.writeEndElement();
        }
        catch (XMLStreamException e)
        {
            throw new XmlOutputException(e);
        }
    }
    
    public <T extends Message<T>> void writeMessage(int fieldNumber, T value, boolean repeated) 
    throws IOException
    {
        writeObject(fieldNumber, value, value.cachedSchema(), repeated);
    }
    
    public <T> void writeObject(int fieldNumber, T value, Schema<T> schema, boolean repeated) 
    throws IOException
    {
        final Schema<?> lastSchema = this.schema;
        this.schema = schema;
        
        try
        {
            writer.writeStartElement(lastSchema.getFieldName(fieldNumber));
            
            writer.writeStartElement(schema.messageName());
            schema.writeTo(this, value);
            writer.writeEndElement();
            
            writer.writeEndElement();
        }
        catch (XMLStreamException e)
        {
            throw new XmlOutputException(e);
        }
        
        // restore state
        this.schema = lastSchema;
    }

}

@@ -2016,38 +2016,40 @@
 eldNumber), 
-String.valueOf
+Integer.toString
 (value));%0D%0A 
@@ -2205,38 +2205,40 @@
 eldNumber), 
-String.valueOf
+Integer.toString
 (value));%0D%0A 
@@ -2394,38 +2394,40 @@
 eldNumber), 
-String.valueOf
+Integer.toString
 (value));%0D%0A 
@@ -2584,38 +2584,40 @@
 eldNumber), 
-String.valueOf
+Integer.toString
 (value));%0D%0A 
@@ -2775,38 +2775,40 @@
 eldNumber), 
-String.valueOf
+Integer.toString
 (value));%0D%0A 
@@ -2964,38 +2964,37 @@
 eldNumber), 
-String.valueOf
+Long.toString
 (value));%0D%0A 
@@ -3151,38 +3151,37 @@
 eldNumber), 
-String.valueOf
+Long.toString
 (value));%0D%0A 
@@ -3338,38 +3338,37 @@
 eldNumber), 
-String.valueOf
+Long.toString
 (value));%0D%0A 
@@ -3526,38 +3526,37 @@
 eldNumber), 
-String.valueOf
+Long.toString
 (value));%0D%0A 
@@ -3715,38 +3715,37 @@
 eldNumber), 
-String.valueOf
+Long.toString
 (value));%0D%0A 
@@ -3902,38 +3902,38 @@
 eldNumber), 
-String.valueOf
+Float.toString
 (value));%0D%0A 
@@ -4092,38 +4092,39 @@
 eldNumber), 
-String.valueOf
+Double.toString
 (value));%0D%0A 
@@ -4467,38 +4467,40 @@
 eldNumber), 
-String.valueOf
+Integer.toString
 (value));%0D%0A 

use Integer.toString (faster path)