View Javadoc

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