Search in sources :

Example 1 with StringList

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("/");
    }
}
Also used : StringList(org.apache.wicket.util.string.StringList)

Example 2 with StringList

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;
            }
        }
    }
}
Also used : MarkupContainer(org.apache.wicket.MarkupContainer) IVisitor(org.apache.wicket.util.visit.IVisitor) StringList(org.apache.wicket.util.string.StringList) Page(org.apache.wicket.Page) Behavior(org.apache.wicket.behavior.Behavior) Component(org.apache.wicket.Component) IVisit(org.apache.wicket.util.visit.IVisit)

Aggregations

StringList (org.apache.wicket.util.string.StringList)2 Component (org.apache.wicket.Component)1 MarkupContainer (org.apache.wicket.MarkupContainer)1 Page (org.apache.wicket.Page)1 Behavior (org.apache.wicket.behavior.Behavior)1 IVisit (org.apache.wicket.util.visit.IVisit)1 IVisitor (org.apache.wicket.util.visit.IVisitor)1