View Javadoc

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