View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.impl.wsdl.support.wsdl;
14  
15  import java.io.InputStream;
16  import java.util.Iterator;
17  import java.util.Map;
18  
19  import javax.wsdl.xml.WSDLLocator;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.xmlbeans.XmlObject;
23  import org.apache.xmlbeans.XmlOptions;
24  import org.w3c.dom.Document;
25  import org.xml.sax.InputSource;
26  
27  import com.eviware.soapui.config.DefinitionCacheConfig;
28  import com.eviware.soapui.config.DefintionPartConfig;
29  import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
30  import com.eviware.soapui.support.Tools;
31  
32  public abstract class WsdlLoader implements WSDLLocator
33  {
34  	private final String url;
35  	private String last;
36  	protected static final Logger log = Logger.getLogger( WsdlLoader.class );
37  
38  	public WsdlLoader( String url )
39  	{
40  		this.url = url;
41  	}
42  
43  	public InputSource getBaseInputSource()
44  	{
45  		try
46  		{
47  			log.debug( "Returning baseInputSource [" + url + "]" );
48  			return new InputSource( load( url ) );
49  		}
50  		catch (Exception e)
51  		{
52  			throw new RuntimeException( e.toString() );
53  		}
54  	}
55  
56  	public abstract InputStream load( String url ) throws Exception;
57  	
58  	public XmlObject loadXmlObject( String url, XmlOptions options ) throws Exception
59  	{
60  		if( options == null )
61  		{
62  			return XmlObject.Factory.parse( load( url ), new XmlOptions().setLoadLineNumbers());
63  		}
64  		else
65  		{
66  			options.setLoadLineNumbers();
67  			return XmlObject.Factory.parse( load( url ), options );
68  		}
69  	}
70  
71  	public String getBaseURI()
72  	{
73  		log.debug( "Returning baseURI [" + url + "]" );
74  		return url;
75  	}
76  
77  	public InputSource getImportInputSource(String parent, String imp)
78  	{
79  		if( isAbsoluteUrl( imp ))
80  			last = imp;
81  		else
82  			last = Tools.joinRelativeUrl( parent, imp);
83  		
84  		try
85  		{
86  			return new InputSource( load( last ) );
87  		}
88  		catch (Exception e)
89  		{
90  			throw new RuntimeException( e.toString() );
91  		}
92  	}
93  
94  	protected boolean isAbsoluteUrl(String tempImp)
95  	{
96  		tempImp = tempImp.toUpperCase();
97  		return tempImp.startsWith( "HTTP:" ) || tempImp.startsWith( "HTTPS:" ) || tempImp.startsWith( "FILE:" );
98  	}
99  
100 	public String getLatestImportURI()
101 	{
102 		String result = last == null ? url : last;
103 		log.debug( "Returning latest import URI [" + result + "]" );
104 		return result;
105 	}
106 
107 	public abstract boolean abort();
108 
109 	public abstract boolean isAborted();
110 
111 	public static DefinitionCacheConfig cacheWsdl( WsdlLoader loader ) throws Exception
112 	{
113 		DefinitionCacheConfig definitionCache = DefinitionCacheConfig.Factory.newInstance();
114 		definitionCache.setRootPart( loader.getBaseURI() );
115 		
116 		Map<String, XmlObject> urls = SchemaUtils.getDefinitionParts( loader );
117 		
118 		for( Iterator<String> i = urls.keySet().iterator(); i.hasNext(); )
119 		{
120 			DefintionPartConfig definitionPart = definitionCache.addNewPart();
121 			String url = i.next();
122 			definitionPart.setUrl( url );
123 			XmlObject xmlObject = urls.get( url );
124 			definitionPart.setContent( xmlObject);
125 			
126 			Document domNode = (Document) xmlObject.getDomNode();
127 			definitionPart.setType( domNode.getDocumentElement().getNamespaceURI());
128 		}		
129 		
130 		return definitionCache;
131 	}
132 }