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