/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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 org.codehaus.activemq.transport.xstream;

import com.thoughtworks.xstream.XStream;
import org.codehaus.activemq.io.TextWireFormat;
import org.codehaus.activemq.io.WireFormat;
import org.codehaus.activemq.message.Packet;

import javax.jms.JMSException;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * A {@link WireFormat} implementation which uses the
 * <a href="http://xstream.codehaus.org/>XStream</a> library to marshall
 * commands onto the wire
 *
 * @version $Revision: 1.2 $
 */
public class XStreamWireFormat extends TextWireFormat {
    private XStream xStream;

    public Packet readPacket(DataInput in) throws IOException {
        String text = in.readUTF();
        return (Packet) getXStream().fromXML(text);
    }

    public Packet readPacket(int firstByte, DataInput in) throws IOException {
        String text = in.readUTF();
        return (Packet) getXStream().fromXML(text);
    }

    public void writePacket(Packet packet, DataOutput out) throws IOException, JMSException {
        String text = getXStream().toXML(packet);
        out.writeUTF(text);
    }

    public WireFormat copy() {
        return new XStreamWireFormat();
    }

    public String toString(Packet packet) {
        return getXStream().toXML(packet);
    }

    public Packet fromString(String xml) {
        return (Packet) getXStream().fromXML(xml);
    }
    
    /**
     * Can this wireformat process packets of this version
     * @param version the version number to test
     * @return true if can accept the version
     */
    public boolean canProcessWireFormatVersion(int version){
        return true;
    }
    
    /**
     * @return the current version of this wire format
     */
    public int getCurrentWireFormatVersion(){
        return 1;
    }

    // Properties
    //-------------------------------------------------------------------------
    public XStream getXStream() {
        if (xStream == null) {
            xStream = createXStream();
        }
        return xStream;
    }

    public void setXStream(XStream xStream) {
        this.xStream = xStream;
    }

    // Implementation methods
    //-------------------------------------------------------------------------
    protected XStream createXStream() {
        return new XStream();
    }
}
