2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 package com.jme.util.resource;
35 import java.io.IOException;
37 import java.net.URISyntaxException;
39 import java.net.URLEncoder;
42 * This locator takes a base URL for finding resources specified with a relative path. If it cannot find the path
43 * relative to the URL, it successively omits the starting components of the relative path until it can find
44 * a resources with such a trimmed path. If no resource is found with this method null is returned.
46 * @author Joshua Slack
48 public class SimpleResourceLocator implements ResourceLocator {
50 protected URI baseDir;
52 public SimpleResourceLocator(URI baseDir) {
53 if (baseDir == null) {
54 throw new NullPointerException("baseDir can not be null.");
56 this.baseDir = baseDir;
59 public SimpleResourceLocator(URL baseDir) throws URISyntaxException {
60 if (baseDir == null) {
61 throw new NullPointerException("baseDir can not be null.");
63 this.baseDir = baseDir.toURI();
66 public URL locateResource(String resourceName) {
67 // Trim off any prepended local dir.
68 while (resourceName.startsWith("./") && resourceName.length() > 2) {
69 resourceName = resourceName.substring(2);
71 while (resourceName.startsWith(".\\") && resourceName.length() > 2) {
72 resourceName = resourceName.substring(2);
75 // Try to locate using resourceName as is.
77 String spec = URLEncoder.encode( resourceName, "UTF-8" );
78 //this fixes a bug in JRE1.5 (file handler does not decode "+" to spaces)
79 spec = spec.replaceAll( "\\+", "%20" );
81 URL rVal = new URL( baseDir.toURL(), spec );
82 // open a stream to see if this is a valid resource
83 // XXX: Perhaps this is wasteful? Also, what info will determine validity?
84 rVal.openStream().close();
86 } catch (IOException e) {
87 // URL wasn't valid in some way, so try up a path.
88 } catch (IllegalArgumentException e) {
89 // URL wasn't valid in some way, so try up a path.
92 resourceName = trimResourceName(resourceName);
93 if (resourceName == null) {
96 return locateResource(resourceName);
100 protected String trimResourceName(String resourceName) {
101 // we are sure this is part of a URL so using slashes only is fine:
102 final int firstSlashIndex = resourceName.indexOf( '/' );
103 if ( firstSlashIndex >= 0 && firstSlashIndex < resourceName.length() - 1 )
105 return resourceName.substring( firstSlashIndex + 1 );
114 public boolean equals(Object obj) {
115 if (obj instanceof SimpleResourceLocator) {
116 return baseDir.equals(((SimpleResourceLocator)obj).baseDir);