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 building XmlObject based configurations
20   * 
21   * @author Ole.Matzura
22   */
23  
24  public class XmlObjectConfigurationBuilder
25  {
26  	private XmlObject config;
27  	private XmlCursor cursor;
28  
29  	public XmlObjectConfigurationBuilder( XmlObject config )
30  	{
31  		this.config = config;
32  		cursor = config.newCursor();
33  		cursor.toNextToken();
34  	}
35  	
36  	public XmlObjectConfigurationBuilder()
37  	{
38  		this( XmlObject.Factory.newInstance() );
39  		cursor = config.newCursor();
40  		cursor.toNextToken();
41  	}
42  	
43  	public XmlObjectConfigurationBuilder add( String name, String value )
44  	{
45  		cursor.insertElementWithText( name, value );
46  		return this;
47  	}
48  
49  	public XmlObjectConfigurationBuilder add( String name, int value )
50  	{
51  		cursor.insertElementWithText( name, String.valueOf( value ));
52  		return this;
53  	}
54  	
55  	public XmlObjectConfigurationBuilder add( String name, long value )
56  	{
57  		cursor.insertElementWithText( name, String.valueOf( value ));
58  		return this;
59  	}
60  
61  	public XmlObjectConfigurationBuilder add( String name, float value )
62  	{
63  		cursor.insertElementWithText( name, String.valueOf( value ));
64  		return this;
65  	}
66  	
67  	public XmlObject finish() 
68  	{
69  		cursor.dispose();
70        return config;
71  	}
72  
73  	public XmlObjectConfigurationBuilder add(String name, boolean value)
74  	{
75  		cursor.insertElementWithText( name, String.valueOf( value ));
76  		return this;
77  	}
78  
79  	public void add( String name, String[] values )
80  	{
81  		for( String value : values )
82  			add( name, value );
83  	}
84  }