1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.model.propertyexpansion.resolvers;
14
15 import org.apache.xmlbeans.XmlObject;
16 import org.apache.xmlbeans.XmlOptions;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.Node;
19
20 import com.eviware.soapui.SoapUI;
21 import com.eviware.soapui.model.TestPropertyHolder;
22 import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
23 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
24 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
25 import com.eviware.soapui.model.testsuite.TestProperty;
26 import com.eviware.soapui.support.xml.XmlUtils;
27
28 public class ResolverUtils
29 {
30 public static String checkForExplicitReference( String propertyName, String prefix,
31 TestPropertyHolder holder, PropertyExpansionContext context, boolean globalOverride )
32 {
33 if( holder == null )
34 return null;
35
36 if( propertyName.startsWith( prefix ))
37 propertyName = propertyName.substring( prefix.length() );
38 else
39 return null;
40
41 return ResolverUtils.parseProperty( propertyName, holder, context, globalOverride );
42 }
43
44 public static String parseProperty( String name, TestPropertyHolder holder, PropertyExpansionContext context, 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 ?
64 ResolverUtils.extractXPathPropertyValue( property, xpath ) :
65 ResolverUtils.extractXPathPropertyValue( property, PropertyExpansionUtils.expandProperties( context, xpath ) );
66 }
67 }
68 else
69 {
70 if( globalOverride )
71 {
72 String value = PropertyExpansionUtils.getGlobalProperty( name );
73 if( value != null )
74 return value;
75 }
76
77 TestProperty property = holder.getProperty( name);
78 if( property != null )
79 {
80 return property.getValue();
81 }
82 }
83
84 return null;
85 }
86
87 public static String extractXPathPropertyValue( Object property, String xpath )
88 {
89 try
90 {
91 String value = property instanceof TestProperty ? ((TestProperty)property).getValue() : property.toString();
92 XmlObject xmlObject = XmlObject.Factory.parse( value );
93 String ns = xpath.trim().startsWith( "declare namespace" ) ? "" : XmlUtils.declareXPathNamespaces( xmlObject );
94 XmlObject[] paths = xmlObject.selectPath( ns + xpath );
95 if( paths.length > 0 )
96 {
97 Node domNode = paths[0].getDomNode();
98 if( domNode.getNodeType() == Node.ELEMENT_NODE && XmlUtils.getChildElements((Element) domNode).getLength() > 0 )
99 return paths[0].xmlText(new XmlOptions().setSaveOuter());
100 else
101 return XmlUtils.getNodeValue( domNode );
102 }
103 }
104 catch( Exception e )
105 {
106 SoapUI.logError( e );
107 }
108
109 return null;
110 }
111
112 }