View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.support.xml;
    14
    15  import org.apache.xmlbeans.XmlCursor;
    16  import org.apache.xmlbeans.XmlObject;
    17
    18  /***
    19   * Support class for reading XmlObject based configurations..
    20   *
    21   * @author Ole.Matzura
    22   */
    23
    24  public class XmlObjectConfigurationReader
    25  {
    26  	private final XmlObject config;
    27
    28  	public XmlObjectConfigurationReader(XmlObject config)
    29  	{
    30  		this.config = config;
    31  	}
    32
    33  	public int readInt(String name, int def )
    34  	{
    35  		if( config == null )
    36  			return def;
    37
    38  		try
    39  		{
    40  			return Integer.parseInt(readString( name, null));
    41  		}
    42  		catch (NumberFormatException e)
    43  		{
    44  		}
    45
    46        return def;
    47  	}
    48
    49  	public long readLong(String name, int def )
    50  	{
    51  		if( config == null )
    52  			return def;
    53
    54  		try
    55  		{
    56
    57  			return Long.parseLong(readString( name, null));
    58  		}
    59  		catch (NumberFormatException e)
    60  		{
    61  		}
    62
    63        return def;
    64  	}
    65
    66  	public float readFloat(String name, float def )
    67  	{
    68  		if( config == null )
    69  			return def;
    70
    71  		try
    72  		{
    73  			return Float.parseFloat(readString( name, null ));
    74  		}
    75  		catch (NumberFormatException e)
    76  		{
    77  		}
    78
    79        return def;
    80  	}
    81
    82  	public String readString(String name, String def )
    83  	{
    84  		if( config == null )
    85  			return def;
    86
    87  		XmlObject[] paths = config.selectPath("$this/" + name);
    88  		if (paths.length == 1)
    89  		{
    90  			XmlCursor cursor = paths[0].newCursor();
    91  			String textValue = cursor.getTextValue();
    92  			cursor.dispose();
    93  			return textValue;
    94  		}
    95
    96        return def;
    97  	}
    98
    99  	public String [] readStrings(String name )
    100 	{
    101 		if( config == null )
    102 			return null;
    103
    104 		XmlObject[] paths = config.selectPath("$this/" + name);
    105 		String [] result = new String[paths.length];
    106
    107 		for( int c = 0; c < paths.length; c++ )
    108 		{
    109 			XmlCursor cursor = paths[c].newCursor();
    110 			result[c] = cursor.getTextValue();
    111 			cursor.dispose();
    112 		}
    113
    114       return result;
    115 	}
    116
    117 	public boolean readBoolean(String name, boolean def)
    118 	{
    119 		try
    120 		{
    121 			return Boolean.valueOf(readString(name, String.valueOf(def)));
    122 		}
    123 		catch (Exception e)
    124 		{
    125 			return def;
    126 		}
    127 	}
    128 }