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.openejb.server.axis2.util;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.net.URI;
024: import java.net.URISyntaxException;
025: import java.net.URL;
026:
027: public class SimpleUriResolver {
028: private URI uri;
029: private URL url;
030: private InputStream is;
031:
032: public SimpleUriResolver() {
033: }
034:
035: public SimpleUriResolver(String path) throws IOException {
036: this ("", path);
037: }
038:
039: public SimpleUriResolver(String baseUriStr, String uriStr)
040: throws IOException {
041: if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
042: tryJar(baseUriStr, uriStr);
043: } else if (uriStr.startsWith("jar:")) {
044: tryJar(uriStr);
045: } else {
046: tryFileSystem(baseUriStr, uriStr);
047: }
048: }
049:
050: public void resolve(String baseUriStr, String uriStr)
051: throws IOException {
052: this .uri = null;
053: this .url = null;
054: this .is = null;
055:
056: if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
057: tryJar(baseUriStr, uriStr);
058: } else if (uriStr.startsWith("jar:")) {
059: tryJar(uriStr);
060: } else {
061: tryFileSystem(baseUriStr, uriStr);
062: }
063: }
064:
065: private void tryFileSystem(String baseUriStr, String uriStr)
066: throws IOException {
067: try {
068: URI relative;
069: File uriFile = new File(uriStr);
070: uriFile = new File(uriFile.getAbsolutePath());
071:
072: if (uriFile.exists()) {
073: relative = uriFile.toURI();
074: } else {
075: relative = new URI(uriStr.replaceAll(" ", "%20"));
076: }
077:
078: if (relative.isAbsolute()) {
079: uri = relative;
080: url = relative.toURL();
081: is = url.openStream();
082: } else if (baseUriStr != null) {
083: URI base;
084: File baseFile = new File(baseUriStr);
085:
086: if (!baseFile.exists()
087: && baseUriStr.startsWith("file:/")) {
088: baseFile = new File(baseUriStr.substring(6));
089: }
090:
091: if (baseFile.exists()) {
092: base = baseFile.toURI();
093: } else {
094: base = new URI(baseUriStr);
095: }
096:
097: base = base.resolve(relative);
098: if (base.isAbsolute()) {
099: uri = base;
100: url = base.toURL();
101: is = url.openStream();
102: }
103: }
104: } catch (URISyntaxException e) {
105: // do nothing
106: }
107: }
108:
109: private void tryJar(String baseStr, String uriStr)
110: throws IOException {
111: int i = baseStr.indexOf('!');
112: if (i == -1) {
113: tryFileSystem(baseStr, uriStr);
114: }
115:
116: String jarBase = baseStr.substring(0, i + 1);
117: String jarEntry = baseStr.substring(i + 1);
118: try {
119: URI u = new URI(jarEntry).resolve(uriStr);
120:
121: tryJar(jarBase + u.toString());
122:
123: if (is != null) {
124: if (u.isAbsolute()) {
125: url = u.toURL();
126: }
127: return;
128: }
129: } catch (URISyntaxException e) {
130: // do nothing
131: }
132:
133: tryFileSystem("", uriStr);
134: }
135:
136: private void tryJar(String uriStr) throws IOException {
137: int i = uriStr.indexOf('!');
138: if (i == -1) {
139: return;
140: }
141:
142: url = new URL(uriStr);
143: try {
144: is = url.openStream();
145: try {
146: uri = url.toURI();
147: } catch (URISyntaxException ex) {
148: // ignore
149: }
150: } catch (IOException e) {
151: // do nothing
152: }
153: }
154:
155: public URI getURI() {
156: return uri;
157: }
158:
159: public URL getURL() {
160: return url;
161: }
162:
163: public InputStream getInputStream() {
164: return is;
165: }
166:
167: public boolean isResolved() {
168: return is != null;
169: }
170: }
|