use of org.apache.wicket.util.string.StringList in project wicket by apache.
the class Packages method absolutePath.
/**
* Takes a package and a path to a resource and returns an absolute path to the resource. For
* example, if the given package was java.lang and the relative path was "../util/List", then
* "java/util/List" would be returned. An already absolute path stays absolute.
* <p>
* Note: The returned absolute path does not start with a slash ("/").
*
* @param packageName
* The package to start at
* @param path
* The path to the resource
* @return The absolute path
*/
public static String absolutePath(final String packageName, final String path) {
// Is path already absolute?
if (path.startsWith("/")) {
return path.substring(1);
} else {
// Break package into list of package names
final StringList absolutePath = StringList.tokenize(packageName, ".");
// Break path into folders
final StringList folders = StringList.tokenize(path, "/\\");
// Iterate through folders
for (int i = 0, size = folders.size(); i < size; i++) {
// Get next folder
final String folder = folders.get(i);
// Up one?
if ("..".equals(folder)) {
// Pop off stack
if (absolutePath.size() > 0) {
absolutePath.removeLast();
} else {
throw new IllegalArgumentException("Invalid path " + path);
}
} else {
// Add to stack
absolutePath.add(folder);
}
}
// Return absolute path
return absolutePath.join("/");
}
}
use of org.apache.wicket.util.string.StringList in project wicket by apache.
the class StatelessChecker method onBeforeRender.
/**
* @see org.apache.wicket.application.IComponentOnBeforeRenderListener#onBeforeRender(org.apache.wicket.Component)
*/
@Override
public void onBeforeRender(final Component component) {
if (mustCheck(component)) {
final IVisitor<Component, Component> visitor = new IVisitor<Component, Component>() {
@Override
public void component(final Component comp, final IVisit<Component> visit) {
if ((component instanceof Page) && mustCheck(comp)) {
// Do not go deeper, because this component will be
// checked by checker
// itself.
// Actually we could go deeper but that would mean we
// traverse it twice
// (for current component and for inspected one).
// We go deeper for Page because full tree will be
// inspected during
// isPageStateless call.
visit.dontGoDeeper();
} else if (!comp.isStateless()) {
visit.stop(comp);
} else {
// continue
}
}
};
if (component.isStateless() == false) {
StringList statefulBehaviors = new StringList();
for (Behavior b : component.getBehaviors()) {
if (b.getStatelessHint(component) == false) {
statefulBehaviors.add(Classes.name(b.getClass()));
}
}
String reason;
if (statefulBehaviors.size() == 0) {
reason = " Possible reason: no stateless hint";
} else {
reason = " Stateful behaviors: " + statefulBehaviors.join();
}
fail(new StatelessCheckFailureException(component, reason));
return;
}
if (component instanceof MarkupContainer) {
MarkupContainer container = ((MarkupContainer) component);
// Traverse children
final Object o = container.visitChildren(visitor);
if (o != null) {
fail(new StatelessCheckFailureException(container, " Offending component: " + o));
return;
}
}
if (component instanceof Page) {
final Page p = (Page) component;
if (!p.isBookmarkable()) {
fail(new StatelessCheckFailureException(p, " Only bookmarkable pages can be stateless"));
return;
}
if (!p.isPageStateless()) {
fail(new StatelessCheckFailureException(p, " for unknown reason"));
return;
}
}
}
}
Aggregations