View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.support.scripting.js;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.model.ModelItem;
17  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
18  import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
19  import com.eviware.soapui.support.scripting.SoapUIScriptEngineFactory;
20  import com.eviware.soapui.support.scripting.SoapUIScriptGenerator;
21  import com.eviware.soapui.support.types.StringToStringMap;
22  import com.eviware.soapui.support.xml.XPathData;
23  
24  /***
25   * Factory for creating Javascript ScriptEngines
26   * 
27   * @author ole.matzura
28   */
29  
30  public class JsScriptEngineFactory implements SoapUIScriptEngineFactory, SoapUIScriptGenerator
31  {
32  	public static final String ID = "Javascript";
33  
34  	public SoapUIScriptEngine createScriptEngine( ModelItem modelItem )
35  	{
36  		return new JsScriptEngine( SoapUI.class.getClassLoader() );
37  	}
38  
39  	public SoapUIScriptGenerator createCodeGenerator( ModelItem modelItem )
40  	{
41  		return this;
42  	}
43  
44  	public String createContextExpansion( String name, PropertyExpansion expansion )
45  	{
46  		return "var " + name + " = context.expand( \"" + expansion + "\" );\n";
47  	}
48  
49  	public String createScriptAssertionForExists( XPathData xpathData )
50  	{
51  		String script = "var holder = new com.eviware.soapui.support.XmlHolder( messageExchange.responseContentAsXml );\n";
52  
53  		StringToStringMap nsMap = xpathData.getNamespaceMap();
54  		for( String ns : nsMap.keySet() )
55  		{
56  			script += "holder.namespaces.put(\"" + nsMap.get( ns ) + "\", \"" + ns + "\" );\n";
57  		}
58  
59  		script += "var node = holder.getDomNode( \"" + xpathData.getPath() + "\" );\n";
60  		script += "if( node == null )\n   throw new java.lang.Exception( \"Missing node\" );\n";
61  
62  		return script;
63  	}
64  }