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