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  /***
    33   * Abstract WSDLLocator for loading definitions from either URL or cache..
    34   *
    35   * @author ole.matzura
    36   */
    37
    38  public abstract class WsdlLoader implements WSDLLocator
    39  {
    40  	private final String url;
    41  	private String last;
    42  	protected static final Logger log = Logger.getLogger( WsdlLoader.class );
    43
    44  	public WsdlLoader( String url )
    45  	{
    46  		this.url = url;
    47  	}
    48
    49  	public InputSource getBaseInputSource()
    50  	{
    51  		try
    52  		{
    53  			log.debug( "Returning baseInputSource [" + url + "]" );
    54  			return new InputSource( load( url ) );
    55  		}
    56  		catch (Exception e)
    57  		{
    58  			throw new RuntimeException( e.toString() );
    59  		}
    60  	}
    61
    62  	public abstract InputStream load( String url ) throws Exception;
    63
    64  	public XmlObject loadXmlObject( String url, XmlOptions options ) throws Exception
    65  	{
    66  		if( options == null )
    67  		{
    68  			return XmlObject.Factory.parse( load( url ), new XmlOptions().setLoadLineNumbers());
    69  		}
    70  		else
    71  		{
    72  			options.setLoadLineNumbers();
    73  			return XmlObject.Factory.parse( load( url ), options );
    74  		}
    75  	}
    76
    77  	public String getBaseURI()
    78  	{
    79  		log.debug( "Returning baseURI [" + url + "]" );
    80  		return url;
    81  	}
    82
    83  	public InputSource getImportInputSource(String parent, String imp)
    84  	{
    85  		if( isAbsoluteUrl( imp ))
    86  			last = imp;
    87  		else
    88  			last = Tools.joinRelativeUrl( parent, imp);
    89
    90  		try
    91  		{
    92  			return new InputSource( load( last ) );
    93  		}
    94  		catch (Exception e)
    95  		{
    96  			throw new RuntimeException( e.toString() );
    97  		}
    98  	}
    99
    100 	protected boolean isAbsoluteUrl(String tempImp)
    101 	{
    102 		tempImp = tempImp.toUpperCase();
    103 		return tempImp.startsWith( "HTTP:" ) || tempImp.startsWith( "HTTPS:" ) || tempImp.startsWith( "FILE:" );
    104 	}
    105
    106 	public String getLatestImportURI()
    107 	{
    108 		String result = last == null ? url : last;
    109 		log.debug( "Returning latest import URI [" + result + "]" );
    110 		return result;
    111 	}
    112
    113 	public abstract boolean abort();
    114
    115 	public abstract boolean isAborted();
    116
    117 	public static DefinitionCacheConfig cacheWsdl( WsdlLoader loader ) throws Exception
    118 	{
    119 		DefinitionCacheConfig definitionCache = DefinitionCacheConfig.Factory.newInstance();
    120 		definitionCache.setRootPart( loader.getBaseURI() );
    121
    122 		Map<String, XmlObject> urls = SchemaUtils.getDefinitionParts( loader );
    123
    124 		for( Iterator<String> i = urls.keySet().iterator(); i.hasNext(); )
    125 		{
    126 			DefintionPartConfig definitionPart = definitionCache.addNewPart();
    127 			String url = i.next();
    128 			definitionPart.setUrl( url );
    129 			XmlObject xmlObject = urls.get( url );
    130 			definitionPart.setContent( xmlObject);
    131
    132 			Document domNode = (Document) xmlObject.getDomNode();
    133 			definitionPart.setType( domNode.getDocumentElement().getNamespaceURI());
    134 		}
    135
    136 		return definitionCache;
    137 	}
    138 }