OSDN Git Service

bef646ff0c324ac21a5f061ff1387fdcb86584a4
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / util / resource / SimpleResourceLocator.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32
33 package com.jme.util.resource;
34
35 import java.io.IOException;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.net.URL;
39 import java.net.URLEncoder;
40
41 /**
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.
45  * 
46  * @author Joshua Slack
47  */
48 public class SimpleResourceLocator implements ResourceLocator {
49
50     protected URI baseDir;
51
52     public SimpleResourceLocator(URI baseDir) {
53         if (baseDir == null) {
54             throw new NullPointerException("baseDir can not be null.");
55         }
56         this.baseDir = baseDir;
57     }
58
59     public SimpleResourceLocator(URL baseDir) throws URISyntaxException {
60         if (baseDir == null) {
61             throw new NullPointerException("baseDir can not be null.");
62         }
63         this.baseDir = baseDir.toURI();
64     }
65     
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);
70         }
71         while (resourceName.startsWith(".\\") && resourceName.length() > 2) {
72             resourceName = resourceName.substring(2);
73         }
74
75         // Try to locate using resourceName as is.
76         try {
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" );
80
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();
85             return rVal;
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.
90         }
91     
92         resourceName = trimResourceName(resourceName);
93         if (resourceName == null) {
94             return null;
95         } else {
96             return locateResource(resourceName);
97         }
98     }
99
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 )
104         {
105             return resourceName.substring( firstSlashIndex + 1 );
106         }
107         else
108         {
109             return null;
110         }
111     }
112
113     @Override
114     public boolean equals(Object obj) {
115         if (obj instanceof SimpleResourceLocator) {
116             return baseDir.equals(((SimpleResourceLocator)obj).baseDir);
117         }
118         return false;
119     }
120 }