Created
September 30, 2011 08:54
-
-
Save quoth/1253158 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
import java.util.ListIterator; | |
import org.apache.wicket.util.resource.IResourceStream; | |
import org.apache.wicket.util.resource.locator.ResourceStreamLocator; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* Locate Wicket resource by path removed a specified package-depth. | |
* <h3>Usage</h3> | |
* <pre> | |
* getResourceSettings().setResourceStreamLocator(CutdownPackageResourceStreamLocator.newInstance(1)); | |
* </pre> | |
* <h4>Example</h4> | |
* In case of setting 2 for package depth. | |
* <ul> | |
* <li>com/example/Homepage.html => HomePage.html</li> | |
* <li>com/example/item/ItemPage.html => item/ItemPage.html</li> | |
* <li>com/SamplePage.properties => com/SamplePage.properties</li> | |
* </ul> | |
* | |
*/ | |
public class CutdownPackageResourceStreamLocator extends ResourceStreamLocator { | |
private static final Logger logger = LoggerFactory.getLogger(CutdownPackageResourceStreamLocator.class); | |
private final int packageDepth; | |
private CutdownPackageResourceStreamLocator(int packageDepth) { | |
this.packageDepth = packageDepth; | |
} | |
public static CutdownPackageResourceStreamLocator newInstance() { | |
return new CutdownPackageResourceStreamLocator(0); | |
} | |
public static CutdownPackageResourceStreamLocator newInstance(int packageDepth) { | |
return new CutdownPackageResourceStreamLocator(packageDepth); | |
} | |
/* | |
* (non-Javadoc) | |
* @see org.apache.wicket.util.resource.locator.ResourceStreamLocator#locate(java.lang.Class, java.lang.String) | |
*/ | |
@Override | |
public IResourceStream locate(Class<?> clazz, String path) { | |
String resolvedPath = cutdownPackageFromPath(clazz, path); | |
logger.debug("searching {}.", resolvedPath); | |
IResourceStream located = super.locate(clazz, resolvedPath); | |
if (located != null) { | |
return located; | |
} | |
return super.locate(clazz, path); | |
} | |
String cutdownPackageFromPath(final Class<?> clazz, final String path) { | |
List<String> packageNameList = | |
Arrays.asList(clazz.getPackage().getName().split("\\.")); | |
int type = packageNameList.size() >= packageDepth ? packageNameList.size() - packageDepth : -1; | |
// case: Number of packages is larger than depth. | |
if (type > 0) { | |
StringBuilder sb = new StringBuilder(); | |
for (ListIterator<String> itor = packageNameList.listIterator(packageDepth); itor.hasNext();) { | |
sb.append(String.valueOf(itor.next())); | |
sb.append('/'); | |
} | |
int start = path.indexOf(sb.toString()); | |
if (start == -1) { | |
return path; | |
} | |
return path.substring(start); | |
// case: Number of packages equals depth. | |
} else if (type == 0) { | |
int start = path.lastIndexOf('/'); | |
if (start == -1) { | |
return path; | |
} | |
return path.substring(start + 1); | |
} else { | |
// do nothing. | |
return path; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment