1
2
3
4
5
6
7
8
9
10
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 }