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

de.javakaffee.web.msm.serializer.javolution.XMLArrayFormats Maven / Gradle / Ivy

/*
 * Copyright 2009 Martin Grotzke
 *
 * 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 de.javakaffee.web.msm.serializer.javolution;

import java.lang.reflect.Array;

import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;

/**
 * A class that collects different {@link XMLFormat} implementations for arrays.
 * 
 * @author Martin Grotzke
 */
public class XMLArrayFormats {

    static final XMLFormat BYTE_ARRAY_FORMAT = new XMLFormat( byte[].class ) {

        @Override
        public byte[] newInstance( final Class clazz, final javolution.xml.XMLFormat.InputElement input ) throws XMLStreamException {
            try {
                final int length = input.getAttribute( "length", 0 );
                return (byte[]) Array.newInstance( byte.class, length );
            } catch ( final Exception e ) {
                throw new XMLStreamException( e );
            }
        }

        @Override
        public void read( final javolution.xml.XMLFormat.InputElement input, final byte[] array ) throws XMLStreamException {
            int i = 0;
            while ( input.hasNext() ) {
                array[i++] = input. getNext();
            }
        }

        @Override
        public final void write( final byte[] array, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
            output.setAttribute( "length", array.length );
            for ( final byte item : array ) {
                output.add( item );
            }
        }

    };

    static final XMLFormat CHAR_ARRAY_FORMAT = new XMLFormat( char[].class ) {

        @Override
        public char[] newInstance( final Class clazz, final javolution.xml.XMLFormat.InputElement input ) throws XMLStreamException {
            try {
                final int length = input.getAttribute( "length", 0 );
                return (char[]) Array.newInstance( char.class, length );
            } catch ( final Exception e ) {
                throw new XMLStreamException( e );
            }
        }

        @Override
        public void read( final javolution.xml.XMLFormat.InputElement input, final char[] array ) throws XMLStreamException {
            int i = 0;
            while ( input.hasNext() ) {
                array[i++] = input. getNext();
            }
        }

        @Override
        public final void write( final char[] array, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
            output.setAttribute( "length", array.length );
            for ( final char item : array ) {
                output.add( item );
            }
        }

    };

    static final XMLFormat SHORT_ARRAY_FORMAT = new XMLFormat( short[].class ) {

        @Override
        public short[] newInstance( final Class clazz, final javolution.xml.XMLFormat.InputElement input ) throws XMLStreamException {
            try {
                final int length = input.getAttribute( "length", 0 );
                return (short[]) Array.newInstance( short.class, length );
            } catch ( final Exception e ) {
                throw new XMLStreamException( e );
            }
        }

        @Override
        public void read( final javolution.xml.XMLFormat.InputElement input, final short[] array ) throws XMLStreamException {
            int i = 0;
            while ( input.hasNext() ) {
                array[i++] = input. getNext();
            }
        }

        @Override
        public final void write( final short[] array, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
            output.setAttribute( "length", array.length );
            for ( final short item : array ) {
                output.add( item );
            }
        }

    };

    static final XMLFormat INT_ARRAY_FORMAT = new XMLFormat( int[].class ) {

        @Override
        public int[] newInstance( final Class clazz, final javolution.xml.XMLFormat.InputElement input ) throws XMLStreamException {
            try {
                final int length = input.getAttribute( "length", 0 );
                return (int[]) Array.newInstance( int.class, length );
            } catch ( final Exception e ) {
                throw new XMLStreamException( e );
            }
        }

        @Override
        public void read( final javolution.xml.XMLFormat.InputElement input, final int[] array ) throws XMLStreamException {
            int i = 0;
            while ( input.hasNext() ) {
                array[i++] = input. getNext();
            }
        }

        @Override
        public final void write( final int[] array, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
            output.setAttribute( "length", array.length );
            for ( final int item : array ) {
                output.add( item );
            }
        }

    };

    static final XMLFormat LONG_ARRAY_FORMAT = new XMLFormat( long[].class ) {

        @Override
        public long[] newInstance( final Class clazz, final javolution.xml.XMLFormat.InputElement input ) throws XMLStreamException {
            try {
                final int length = input.getAttribute( "length", 0 );
                return (long[]) Array.newInstance( long.class, length );
            } catch ( final Exception e ) {
                throw new XMLStreamException( e );
            }
        }

        @Override
        public void read( final javolution.xml.XMLFormat.InputElement input, final long[] array ) throws XMLStreamException {
            int i = 0;
            while ( input.hasNext() ) {
                array[i++] = input. getNext();
            }
        }

        @Override
        public final void write( final long[] array, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
            output.setAttribute( "length", array.length );
            for ( final long item : array ) {
                output.add( item );
            }
        }

    };

    static final XMLFormat FLOAT_ARRAY_FORMAT = new XMLFormat( float[].class ) {

        @Override
        public float[] newInstance( final Class clazz, final javolution.xml.XMLFormat.InputElement input ) throws XMLStreamException {
            try {
                final int length = input.getAttribute( "length", 0 );
                return (float[]) Array.newInstance( float.class, length );
            } catch ( final Exception e ) {
                throw new XMLStreamException( e );
            }
        }

        @Override
        public void read( final javolution.xml.XMLFormat.InputElement input, final float[] array ) throws XMLStreamException {
            int i = 0;
            while ( input.hasNext() ) {
                array[i++] = input. getNext();
            }
        }

        @Override
        public final void write( final float[] array, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
            output.setAttribute( "length", array.length );
            for ( final float item : array ) {
                output.add( item );
            }
        }

    };

    static final XMLFormat DOUBLE_ARRAY_FORMAT = new XMLFormat( double[].class ) {

        @Override
        public double[] newInstance( final Class clazz, final javolution.xml.XMLFormat.InputElement input ) throws XMLStreamException {
            try {
                final int length = input.getAttribute( "length", 0 );
                return (double[]) Array.newInstance( double.class, length );
            } catch ( final Exception e ) {
                throw new XMLStreamException( e );
            }
        }

        @Override
        public void read( final javolution.xml.XMLFormat.InputElement input, final double[] array ) throws XMLStreamException {
            int i = 0;
            while ( input.hasNext() ) {
                array[i++] = input. getNext();
            }
        }

        @Override
        public final void write( final double[] array, final javolution.xml.XMLFormat.OutputElement output ) throws XMLStreamException {
            output.setAttribute( "length", array.length );
            for ( final double item : array ) {
                output.add( item );
            }
        }

    };

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy