View Javadoc

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