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.impl.support.definition.support;
14  
15  import java.io.File;
16  import java.io.InputStream;
17  import java.util.List;
18  
19  import org.apache.xmlbeans.XmlException;
20  import org.apache.xmlbeans.XmlObject;
21  import org.apache.xmlbeans.XmlOptions;
22  
23  import com.eviware.soapui.impl.support.definition.DefinitionCache;
24  import com.eviware.soapui.impl.support.definition.InterfaceDefinitionPart;
25  import com.eviware.soapui.impl.wsdl.support.wsdl.AbstractWsdlDefinitionLoader;
26  
27  /***
28   * WsdlLoader for cached definitions
29   * 
30   * @author ole.matzura
31   */
32  
33  public class InterfaceCacheDefinitionLoader extends AbstractWsdlDefinitionLoader
34  {
35  	private String rootInConfig = "";
36  	private DefinitionCache config;
37  
38  	public InterfaceCacheDefinitionLoader( DefinitionCache config )
39  	{
40  		super( config.getRootPart().getUrl() );
41  		this.config = config;
42  	}
43  
44  	public InputStream load( String url ) throws Exception
45  	{
46  		XmlObject xmlObject = loadXmlObject( url, null );
47  		return xmlObject == null ? null : xmlObject.newInputStream();
48  	}
49  
50  	public XmlObject loadXmlObject( String url, XmlOptions options ) throws Exception
51  	{
52  		// required for backwards compatibility when the entire path was stored
53  		if( url.endsWith( config.getRootPart().getUrl() ) )
54  		{
55  			rootInConfig = url.substring( 0, url.length() - config.getRootPart().getUrl().length() );
56  		}
57  
58  		List<InterfaceDefinitionPart> partList = config.getDefinitionParts();
59  		for( InterfaceDefinitionPart part : partList )
60  		{
61  			if( ( rootInConfig + part.getUrl() ).equalsIgnoreCase( url ) )
62  			{
63  				return getPartContent( part );
64  			}
65  		}
66  
67  		// hack: this could be due to windows -> unix, try again with replaced '/'
68  		if( File.separatorChar == '/' )
69  		{
70  			url = url.replace( '/', '//' );
71  
72  			for( InterfaceDefinitionPart part : partList )
73  			{
74  				if( ( rootInConfig + part.getUrl() ).equalsIgnoreCase( url ) )
75  				{
76  					return getPartContent( part );
77  				}
78  			}
79  		}
80  		// or the other way around..
81  		else if( File.separatorChar == '//' )
82  		{
83  			url = url.replace( '//', '/' );
84  
85  			for( InterfaceDefinitionPart part : partList )
86  			{
87  				if( ( rootInConfig + part.getUrl() ).equalsIgnoreCase( url ) )
88  				{
89  					return getPartContent( part );
90  				}
91  			}
92  		}
93  
94  		log.error( "Failed to find [" + url + "] in InterfaceCache" );
95  		
96  		return null;
97  	}
98  
99  	public static XmlObject getPartContent( InterfaceDefinitionPart part ) throws XmlException
100 	{
101 		return XmlObject.Factory.parse( part.getContent(), new XmlOptions().setLoadLineNumbers() );
102 	}
103 
104 	public void close()
105 	{
106 	}
107 
108 	public void setNewBaseURI( String uri )
109 	{
110 		// not implemented
111 	}
112 
113 	public String getFirstNewURI()
114 	{
115 		return getBaseURI();
116 	}
117 }