View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  			String str = readString( name, null );
41  			return str == null ? def : Integer.parseInt( str );
42  		}
43  		catch( NumberFormatException e )
44  		{
45  		}
46  
47  		return def;
48  	}
49  
50  	public long readLong( String name, int def )
51  	{
52  		if( config == null )
53  			return def;
54  
55  		try
56  		{
57  			String str = readString( name, null );
58  			return str == null ? def : Long.parseLong( str );
59  		}
60  		catch( NumberFormatException e )
61  		{
62  		}
63  
64  		return def;
65  	}
66  
67  	public float readFloat( String name, float def )
68  	{
69  		if( config == null )
70  			return def;
71  
72  		try
73  		{
74  			String str = readString( name, null );
75  			return str == null ? def : Float.parseFloat( str );
76  		}
77  		catch( NumberFormatException e )
78  		{
79  		}
80  
81  		return def;
82  	}
83  
84  	public String readString( String name, String def )
85  	{
86  		if( config == null )
87  			return def;
88  
89  		XmlObject[] paths = config.selectPath( "$this/" + name );
90  		if( paths.length == 1 )
91  		{
92  			XmlCursor cursor = paths[0].newCursor();
93  			String textValue = cursor.getTextValue();
94  			cursor.dispose();
95  			return textValue;
96  		}
97  
98  		return def;
99  	}
100 
101 	public String[] readStrings( String name )
102 	{
103 		if( config == null )
104 			return null;
105 
106 		XmlObject[] paths = config.selectPath( "$this/" + name );
107 		String[] result = new String[paths.length];
108 
109 		for( int c = 0; c < paths.length; c++ )
110 		{
111 			XmlCursor cursor = paths[c].newCursor();
112 			result[c] = cursor.getTextValue();
113 			cursor.dispose();
114 		}
115 
116 		return result;
117 	}
118 
119 	public boolean readBoolean( String name, boolean def )
120 	{
121 		try
122 		{
123 			return Boolean.valueOf( readString( name, String.valueOf( def ) ) );
124 		}
125 		catch( Exception e )
126 		{
127 			return def;
128 		}
129 	}
130 }