001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.wsdl11;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.MalformedURLException;
023: import java.net.URISyntaxException;
024: import java.net.URL;
025: import java.net.URLConnection;
026:
027: import javax.wsdl.xml.WSDLLocator;
028:
029: import org.xml.sax.InputSource;
030:
031: import org.apache.cxf.resource.URIResolver;
032:
033: public class WSDLResolver implements WSDLLocator {
034: private String baseUri;
035: private String importedUri;
036: private InputSource inputSource;
037:
038: public WSDLResolver(String baseURI, InputSource is) {
039: this .baseUri = baseURI;
040: inputSource = is;
041: }
042:
043: public InputSource getBaseInputSource() {
044: return inputSource;
045: }
046:
047: public String getBaseURI() {
048: return baseUri;
049: }
050:
051: public String getLatestImportURI() {
052: return importedUri;
053: }
054:
055: public InputSource getImportInputSource(String parent,
056: String importLocation) {
057: this .baseUri = parent;
058: URL parentUrl;
059: try {
060: parentUrl = new URL(parent);
061: URL importUrl = new URL(parentUrl, importLocation);
062: if (importUrl != null
063: && !importUrl.getProtocol().startsWith("file")) {
064: URLConnection con = importUrl.openConnection();
065: con.setUseCaches(false);
066: inputSource = new InputSource(con.getInputStream());
067: } else {
068: File file = new File(importUrl.toURI());
069: if (file.exists()) {
070: URIResolver resolver = new URIResolver(parent
071: .toString(), importLocation);
072: inputSource = new InputSource(resolver
073: .getInputStream());
074: } else {
075: URIResolver resolver = new URIResolver(
076: importLocation);
077: if (resolver.isResolved()) {
078: inputSource = new InputSource(resolver
079: .getInputStream());
080: }
081: }
082: }
083: importedUri = importUrl.toURI().toString();
084:
085: } catch (MalformedURLException e) {
086: //
087: } catch (URISyntaxException e) {
088: //
089: } catch (IOException e) {
090: //
091:
092: }
093: return inputSource;
094:
095: }
096:
097: public void close() {
098: if (inputSource.getByteStream() != null) {
099: try {
100: inputSource.getByteStream().close();
101: } catch (IOException e) {
102: //
103: }
104: }
105:
106: }
107: }
|