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

bibliothek.util.xml.XContainer Maven / Gradle / Ivy

There is a newer version: 1.1.2p6a
Show newest version
/*
 * Bibliothek - DockingFrames
 * Library built on Java/Swing, allows the user to "drag and drop"
 * panels containing any Swing-Component the developer likes to add.
 * 
 * Copyright (C) 2007 Benjamin Sigg
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Benjamin Sigg
 * [email protected]
 * CH - Switzerland
 */
package bibliothek.util.xml;

/**
 * An element that can have some content. The content can be a {@link String},
 * byte-array or a primitive value type.
 * @author Benjamin Sigg
 *
 */
public class XContainer {
    /** the value of this container */
    private String value = "";

    /**
     * Creates an independent copy of this container.
     * @return the new copy
     */
    public XContainer copy(){
    	XContainer copy = new XContainer();
    	copy.copy( this );
    	return copy;
    }
    
    /**
     * Copies all the settings of original into this container.
     * @param original the container to read
     */
    protected void copy( XContainer original ){
    	this.value = original.value;
    }
    
    /**
     * Sets the exact value that is stored in this container.
     * @param value the value that will not be encoded by this method
     */
    public void setValue( String value ) {
        this.value = value;
    }
    
    /**
     * Gets the exact value that is stored in this container.
     * @return the value that is not encoded
     */
    public String getValue() {
        return value;
    }
    
    /**
     * Sets the value of this container.
     * @param b the value
     */
    public void setByte( byte b ){
        value = String.valueOf( b );
    }
    
    /**
     * Sets the value of this container.
     * @param s the value
     */
    public void setShort( short s ){
        value = String.valueOf( s );
    }
    
    /**
     * Sets the value of this container.
     * @param i the value
     */
    public void setInt( int i ){
        value = String.valueOf( i );
    }
    
    /**
     * Sets the value of this container.
     * @param l the value
     */
    public void setLong( long l ){
        value = String.valueOf( l );
    }
    
    /**
     * Sets the value of this container.
     * @param b the value
     */
    public void setBoolean( boolean b ){
        value = String.valueOf( b );
    }
    
    /**
     * Sets the value of this container.
     * @param c the value
     */
    public void setChar( char c ){
        value = String.valueOf( c );
    }
    
    /**
     * Sets the value of this container.
     * @param s the value
     */
    public void setString( String s ){
        if( s == null )
            throw new IllegalArgumentException( "value must not be null" );
        value = s;
    }
    
    /**
     * Sets the value of this container.
     * @param f the value
     */
    public void setFloat( float f ){
        value = String.valueOf( f );
    }
    
    /**
     * Sets the value of this container.
     * @param d the value
     */
    public void setDouble( double d ){
        value = String.valueOf( d );
    }
    
    /**
     * Sets the value of this container.
     * @param bs the value
     */
    public void setByteArray( byte[] bs ){
        StringBuilder builder = new StringBuilder( bs.length*2 );
        for( byte b : bs ){
            builder.append( Integer.toHexString( (b >> 4) & 15 ) );
            builder.append( Integer.toHexString( b & 15 ));
        }
        value = builder.toString();
    }
    
    
    
    /**
     * Gets the value of this container as byte.
     * @return the value
     * @throws XException if value is not a byte
     */
    public byte getByte(){
        try{
            return Byte.parseByte( value );
        }
        catch( NumberFormatException ex ){
            throw new XException( ex );
        }
    }
    
    /**
     * Gets the value of this container as short.
     * @return the value
     * @throws XException if value is not a short
     */
    public short getShort(){
        try{
            return Short.parseShort( value );
        }
        catch( NumberFormatException ex ){
            throw new XException( ex );
        }
    }
    
    /**
     * Gets the value of this container as integer.
     * @return the value
     * @throws XException if value is not an integer
     */
    public int getInt(){
        try{
            return Integer.parseInt( value );
        }
        catch( NumberFormatException ex ){
            throw new XException( ex );
        }
    }
    
    /**
     * Gets the value of this container as long.
     * @return the value
     * @throws XException if value is not a long
     */
    public long getLong(){
        try{
            return Long.parseLong( value );
        }
        catch( NumberFormatException ex ){
            throw new XException( ex );
        }
    }
    
    /**
     * Gets the value of this container as float.
     * @return the value
     * @throws XException if value is not a float
     */
    public float getFloat(){
        try{
            return Float.parseFloat( value );
        }
        catch( NumberFormatException ex ){
            throw new XException( ex );
        }
    }
    
    /**
     * Gets the value of this container as double.
     * @return the value
     * @throws XException if value is not a double
     */
    public double getDouble(){
        try{
            return Double.parseDouble( value );
        }
        catch( NumberFormatException ex ){
            throw new XException( ex );
        }
    }
    
    /**
     * Gets the value of this container as character.
     * @return the value
     * @throws XException if value is not a character
     */
    public char getChar(){
        if( value.length() != 1 )
            throw new XException( "not a character: " + value );
        
        return value.charAt( 0 );
    }
    
    /**
     * Gets the value of this container as string.
     * @return the value
     * @throws XException if value is not a string
     */
    public String getString(){
        return value;
    }
    
    /**
     * Gets the value of this container as boolean.
     * @return the value
     * @throws XException if value is not a boolean
     */
    public boolean getBoolean(){
        String value = this.value.trim().toLowerCase();
        
        if( "true".equals( value ))
            return true;
        if( "on".equals( value ))
            return true;
        if( "1".equals( value ))
            return true;
        if( "yes".equals( value ))
            return true;
        
        if( "false".equals( value ))
            return false;
        if( "off".equals( value ))
            return false;
        if( "0".equals( value ))
            return false;
        if( "no".equals( value ))
            return false;
        
        throw new XException( "not a boolean: " + value );
    }
    
    /**
     * Gets the value of this container as byte-array.
     * @return the value
     * @throws XException if value is not a byte-array
     */
    public byte[] getByteArray(){
        if( value.length() % 2 != 0 )
            throw new XException( "value is not a byte-array: " + value );
        
        byte[] result = new byte[ value.length()/2 ];
        for( int i = 0, n = value.length(); i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy