001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.support.wsdl;
014:
015: import java.io.InputStream;
016: import java.util.Iterator;
017: import java.util.Map;
018:
019: import javax.wsdl.xml.WSDLLocator;
020:
021: import org.apache.log4j.Logger;
022: import org.apache.xmlbeans.XmlObject;
023: import org.apache.xmlbeans.XmlOptions;
024: import org.w3c.dom.Document;
025: import org.xml.sax.InputSource;
026:
027: import com.eviware.soapui.config.DefinitionCacheConfig;
028: import com.eviware.soapui.config.DefintionPartConfig;
029: import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
030: import com.eviware.soapui.support.Tools;
031:
032: /**
033: * Abstract WSDLLocator for loading definitions from either URL or cache..
034: *
035: * @author ole.matzura
036: */
037:
038: public abstract class WsdlLoader implements WSDLLocator {
039: private final String url;
040: private String last;
041: protected static final Logger log = Logger
042: .getLogger(WsdlLoader.class);
043:
044: public WsdlLoader(String url) {
045: this .url = url;
046: }
047:
048: public InputSource getBaseInputSource() {
049: try {
050: log.debug("Returning baseInputSource [" + url + "]");
051: return new InputSource(load(url));
052: } catch (Exception e) {
053: throw new RuntimeException(e.toString());
054: }
055: }
056:
057: public abstract InputStream load(String url) throws Exception;
058:
059: public XmlObject loadXmlObject(String url, XmlOptions options)
060: throws Exception {
061: if (options == null) {
062: return XmlObject.Factory.parse(load(url), new XmlOptions()
063: .setLoadLineNumbers());
064: } else {
065: options.setLoadLineNumbers();
066: return XmlObject.Factory.parse(load(url), options);
067: }
068: }
069:
070: public String getBaseURI() {
071: log.debug("Returning baseURI [" + url + "]");
072: return url;
073: }
074:
075: public InputSource getImportInputSource(String parent, String imp) {
076: if (isAbsoluteUrl(imp))
077: last = imp;
078: else
079: last = Tools.joinRelativeUrl(parent, imp);
080:
081: try {
082: return new InputSource(load(last));
083: } catch (Exception e) {
084: throw new RuntimeException(e.toString());
085: }
086: }
087:
088: protected boolean isAbsoluteUrl(String tempImp) {
089: tempImp = tempImp.toUpperCase();
090: return tempImp.startsWith("HTTP:")
091: || tempImp.startsWith("HTTPS:")
092: || tempImp.startsWith("FILE:");
093: }
094:
095: public String getLatestImportURI() {
096: String result = last == null ? url : last;
097: log.debug("Returning latest import URI [" + result + "]");
098: return result;
099: }
100:
101: public abstract boolean abort();
102:
103: public abstract boolean isAborted();
104:
105: public static DefinitionCacheConfig cacheWsdl(WsdlLoader loader)
106: throws Exception {
107: DefinitionCacheConfig definitionCache = DefinitionCacheConfig.Factory
108: .newInstance();
109: definitionCache.setRootPart(loader.getBaseURI());
110:
111: Map<String, XmlObject> urls = SchemaUtils
112: .getDefinitionParts(loader);
113:
114: for (Iterator<String> i = urls.keySet().iterator(); i.hasNext();) {
115: DefintionPartConfig definitionPart = definitionCache
116: .addNewPart();
117: String url = i.next();
118: definitionPart.setUrl(url);
119: XmlObject xmlObject = urls.get(url);
120: definitionPart.setContent(xmlObject);
121:
122: Document domNode = (Document) xmlObject.getDomNode();
123: definitionPart.setType(domNode.getDocumentElement()
124: .getNamespaceURI());
125: }
126:
127: return definitionCache;
128: }
129: }
|