All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.serializer.AsciiSerializer Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

//@(#) $Id: AsciiSerializer.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.serializer;

import java.io.IOException;

import at.spardat.xma.util.ByteArray;

/**
 * Serializer that produces only ascii characters.
 * 
 * @author YSD, 11.02.2005  
 */
public class AsciiSerializer implements Serializer {
    
    
    /**
     * Is this executing at the server side of xma?
     */
    private boolean                     fIsAtServer = false;
    
    /**
     * Contains output byte array
     */
    private ByteArray                   fBytes;
    
    /**
     * Lines written should be approximately this long
     */
    private static final int            fLineLength = 80;
    
    /**
     * Last LF at position
     */
    private int                         fLastLF = 0;
    
    
    /**
     * Constructor
     * 
     * @param isAtServer is this serializer running at the server side?
     * @param bufferSize initial buffer size
     */
    public AsciiSerializer (boolean isAtServer, int bufferSize) {
        fIsAtServer = isAtServer;
        fBytes = new ByteArray (bufferSize);
    }

    /**
     * @see at.spardat.xma.serializer.Serializer#getResult()
     */
    public ByteArray getResult () {
        return fBytes;
    }
    
    /**
     * @see at.spardat.xma.serializer.Serializer#addHeader()
     */
    public void addHeader() {
        fBytes.addHeader();
    }

    /**
     * @see at.spardat.xma.serializer.Serializer#isAtServer()
     */
    public boolean isAtServer () {
        return fIsAtServer;
    }

    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeString(java.lang.String, java.lang.String)
     */
    public void writeString (String label, String s) throws IOException {
        writeString0 (label, s);
    }
    
    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeStringN(java.lang.String, java.lang.String)
     */
    public void writeStringN (String label, String s) throws IOException {
        writeBoolean (label, s == null);
        if (s != null) {
            writeString (label, s);
        }
    }

    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeInt(java.lang.String, int)
     */
    public void writeInt (String label, int i) throws IOException {
        writeString0 (label, String.valueOf(i));
    }
    
    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeLong(java.lang.String, long)
     */
    public void writeLong (String label, long l) throws IOException {
        writeString0 (label, String.valueOf(l));
    }

    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeShort(java.lang.String, int)
     */
    public void writeShort (String label, int s) throws IOException {
        writeString0 (label, String.valueOf(s));
    }

    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeBoolean(java.lang.String, boolean)
     */
    public void writeBoolean (String label, boolean b) throws IOException {
        if (b) writeString0 (label, "1");
        else writeString0 (label, "0");
    }

    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeByte(java.lang.String, int)
     */
    public void writeByte (String label, int b) throws IOException {
        writeString0 (label, String.valueOf(b));
    }
    
    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeSerializedBytes(java.lang.String, byte[])
     */
    public void writeSerializedBytes (String label, byte[] bytes) throws IOException {
        fBytes.write('{');
        fBytes.write(bytes);
        fBytes.write('}');
        fBytes.write(',');
    }
    
    /**
     * @see at.spardat.xma.serializer.XmaOutput#writeObject(java.lang.String, java.lang.Object)
     */
    public void writeObject (String label, Object obj) throws IOException {
        if (obj instanceof String) {
            writeString ("t", "S");
            writeString (label, (String)obj);
        } else if (obj instanceof Boolean) {
            writeString ("t", "B");
            writeBoolean (label, ((Boolean)obj).booleanValue());
        } else if (obj instanceof Short) {
            writeString ("t", "H");
            writeShort (label, ((Short)obj).shortValue());
        } else if (obj instanceof Integer) {
            writeString ("t", "I");
            writeInt (label, ((Integer)obj).intValue());
        } else if (obj instanceof XmaSerializable) {
            writeString ("t", obj.getClass().getName());
            ((XmaSerializable)obj).serialize(this);
        } else {
            writeString ("t", "J");
            byte[]      bytes = Util.serialize(obj);
            bytes = Util.hexEncode(bytes);
            writeSerializedBytes (label, bytes);
        }
    }
    
    private void writeString0 (String label, String s) {
        writeStringEscaped (label, s);
        // write terminator
        fBytes.write(',');
        // optionally add a LF if linelength is exceeded
        if (fBytes.size() > fLastLF+fLineLength) {
            fLastLF = fBytes.size();
            fBytes.write('\n');
        }
    }

    /**
     * Writes string by escaping all characters so that the resulting
     * byte array contains only US-ASCII characters. In this implementation, 
     * label is ignored.
     */
    protected void writeStringEscaped (String label, String s) {
        for (int len=s.length(), i=0; i= 0x20 && ch <= 0x7E) fBytes.write (ch);
            else {
                // write sequence of \xxxx for four byte hex unicode
                String      hx = getHexCodeFor (ch, 4);
                fBytes.write ('\\');
                fBytes.write (hx.charAt(0));
                fBytes.write (hx.charAt(1));
                fBytes.write (hx.charAt(2));
                fBytes.write (hx.charAt(3));
            }
        }
    }
    
    /**
     * Writes string without escaping. Therefore, the String must not have
     * meta-characters in it.
     */
    protected void writeStringNoEscape (String s) {
        for (int len=s.length(), i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy