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.model.propertyexpansion.resolvers;
14  
15  import org.apache.xmlbeans.XmlObject;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.model.TestPropertyHolder;
19  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
20  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
21  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
22  import com.eviware.soapui.model.testsuite.TestProperty;
23  import com.eviware.soapui.support.xml.XmlUtils;
24  
25  public class ResolverUtils
26  {
27  	public static String checkForExplicitReference( String propertyName, String prefix, 
28  				TestPropertyHolder holder, PropertyExpansionContext context, boolean globalOverride )
29  	{
30  		if( holder == null )
31  			return null; 
32  		
33  		if( propertyName.startsWith( prefix ))
34  			propertyName = propertyName.substring( prefix.length() );
35  		else
36  			return null;
37  		
38  		return ResolverUtils.parseProperty( propertyName, holder, context, globalOverride );
39  	}
40  
41  	public static String parseProperty( String name, TestPropertyHolder holder, PropertyExpansionContext context, boolean globalOverride )
42  	{
43  		int sepIx = name.indexOf( PropertyExpansion.PROPERTY_SEPARATOR );
44  		if( sepIx != -1 )
45  		{
46  			String xpath = name.substring( sepIx+1 );
47  			name = name.substring( 0, sepIx );
48  			
49  			if( globalOverride )
50  			{
51  				String value = PropertyExpansionUtils.getGlobalProperty( name );
52  				if( value != null )
53  					return value;
54  			}
55  			
56  			TestProperty property = holder.getProperty( name);
57  			
58  			if( property != null )
59  			{
60  				return context == null ? 
61  					ResolverUtils.extractXPathPropertyValue( property, xpath ) :
62  					ResolverUtils.extractXPathPropertyValue( property, PropertyExpansionUtils.expandProperties( context, xpath ) );
63  			}
64  		}
65  		else
66  		{
67  			if( globalOverride )
68  			{
69  				String value = PropertyExpansionUtils.getGlobalProperty( name );
70  				if( value != null )
71  					return value;
72  			}
73  			
74  			TestProperty property = holder.getProperty( name);
75  			if( property != null )
76  			{
77  				return property.getValue();
78  			}
79  		}
80  		
81  		return null;
82  	}
83  
84  	public static String extractXPathPropertyValue( Object property, String xpath )
85  	{
86  		try
87  		{
88  			String value = property instanceof TestProperty ? ((TestProperty)property).getValue() : property.toString();
89  			XmlObject xmlObject = XmlObject.Factory.parse(  value );
90  			String ns = xpath.trim().startsWith( "declare namespace" ) ? "" : XmlUtils.declareXPathNamespaces( xmlObject );
91  			XmlObject[] paths = xmlObject.selectPath( ns + xpath );
92  			if( paths.length > 0 )
93  				return XmlUtils.getNodeValue( paths[0].getDomNode() );
94  		}
95  		catch( Exception e )
96  		{
97  			SoapUI.logError( e );
98  		}
99  		
100 		return null;
101 	}
102 
103 }