| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
package tsukuba_bunko.util.locator; |
| 11 |
|
|
| 12 |
|
import java.io.File; |
| 13 |
|
import java.io.UnsupportedEncodingException; |
| 14 |
|
|
| 15 |
|
import java.net.URL; |
| 16 |
|
import java.net.URLDecoder; |
| 17 |
|
|
| 18 |
|
import tsukuba_bunko.util.ResourceLocator; |
| 19 |
|
import tsukuba_bunko.util.ResourceDetectionException; |
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
0 |
public class ResourceLocatorImpl extends ResourceLocator { |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
public File findLocation( String resourceName, ClassLoader classLoader ) |
| 35 |
|
throws ResourceDetectionException |
| 36 |
|
{ |
| 37 |
0 |
URL resourceURL = classLoader.getResource( resourceName ); |
| 38 |
0 |
if( resourceURL == null ) { |
| 39 |
0 |
throw new ResourceDetectionException( "fail to find resource from classpath" ); |
| 40 |
|
} |
| 41 |
|
|
| 42 |
0 |
String protocol = resourceURL.getProtocol(); |
| 43 |
0 |
if( "file".equals(protocol) ) { |
| 44 |
0 |
String path = resourceURL.getFile(); |
| 45 |
|
try { |
| 46 |
0 |
path = URLDecoder.decode( path, "UTF-8" ); |
| 47 |
|
} |
| 48 |
0 |
catch( UnsupportedEncodingException uee ) { |
| 49 |
0 |
throw new ResourceDetectionException( "system is not supoort UTF-8. This Java VM may be broken.", uee ); |
| 50 |
0 |
} |
| 51 |
|
|
| 52 |
0 |
int index = path.indexOf( resourceName ); |
| 53 |
0 |
if( index > 0 ) { |
| 54 |
0 |
resourceName = resourceName.substring( 0, index ); |
| 55 |
|
} |
| 56 |
0 |
return new File( resourceName ); |
| 57 |
|
} |
| 58 |
0 |
else if( "jar".equals(protocol) ) { |
| 59 |
0 |
String jarUri = resourceURL.toString(); |
| 60 |
0 |
int delemiterIndex = jarUri.indexOf( "!/" ); |
| 61 |
0 |
if( delemiterIndex == -1 ) { |
| 62 |
0 |
throw new ResourceDetectionException( "invalid JAR URL." ); |
| 63 |
|
} |
| 64 |
|
else { |
| 65 |
0 |
String resourceUri = jarUri.substring( 4, delemiterIndex ); |
| 66 |
0 |
if( !resourceUri.startsWith("file:") ) { |
| 67 |
0 |
throw new ResourceDetectionException( "supported file protocol only" ); |
| 68 |
|
} |
| 69 |
|
else { |
| 70 |
|
try { |
| 71 |
0 |
return new File( URLDecoder.decode( class="keyword">new URL(resourceUri).getFile(), "UTF-8") ); |
| 72 |
|
} |
| 73 |
0 |
catch( Exception e ) { |
| 74 |
0 |
throw new ResourceDetectionException( "invalid URL.", e ); |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
} |
| 78 |
|
} |
| 79 |
|
else { |
| 80 |
0 |
throw new ResourceDetectionException( "supported jar or file protocol only" ); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
} |