Updated Branches: refs/heads/master d97cee38b -> f6b22863c
WICKET-4145 Improve AutoLinkResolver Revert my fix and apply Andrea's patch because it is more efficient. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/f6b22863 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/f6b22863 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/f6b22863 Branch: refs/heads/master Commit: f6b22863c1743eec85ec642b16ccbdca1b3c9477 Parents: d97cee3 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Thu Feb 23 15:47:27 2012 +0100 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Thu Feb 23 15:47:27 2012 +0100 ---------------------------------------------------------------------- .../wicket/markup/resolver/AutoLinkResolver.java | 129 ++++++++------ 1 files changed, 74 insertions(+), 55 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/f6b22863/wicket-core/src/main/java/org/apache/wicket/markup/resolver/AutoLinkResolver.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/resolver/AutoLinkResolver.java b/wicket-core/src/main/java/org/apache/wicket/markup/resolver/AutoLinkResolver.java index a6814b7..bf3f073 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/resolver/AutoLinkResolver.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/resolver/AutoLinkResolver.java @@ -74,9 +74,7 @@ public final class AutoLinkResolver implements IComponentResolver { /** * Creates a new auto component that references a package resource. - * - * @param container - * the parent container + * * @param autoId * the automatically generated id for the auto component * @param pathInfo @@ -86,9 +84,11 @@ public final class AutoLinkResolver implements IComponentResolver * @return a new auto component or null if the path was absolute */ protected final Component newPackageResourceReferenceAutoComponent( - final MarkupContainer container, final String autoId, final PathInfo pathInfo, + final String autoId, final PathInfo pathInfo, final String attribute) { + final MarkupContainer container = pathInfo.getContainer(); + if (!pathInfo.absolute && (pathInfo.path != null) && (pathInfo.path.length() > 0)) { // Href is relative. Create a resource reference pointing at this file @@ -98,7 +98,7 @@ public final class AutoLinkResolver implements IComponentResolver // header has been added to (e.g. the Page). What we need // however, is the component (e.g. a Panel) which // contributed it. - MarkupStream markupStream = new MarkupStream(container.getMarkup()); + MarkupStream markupStream = pathInfo.getMarkupStream(); Class<? extends Component> clazz = markupStream.getContainerClass(); // However if the markup stream is a merged markup stream (inheritance), than we @@ -208,8 +208,6 @@ public final class AutoLinkResolver implements IComponentResolver * the autoId assigned as it's id. Should return null in case the component could not be * created as expected and the default resolving should take place. * - * @param container - * the parent container * @param autoId * the automatically generated id for the auto component * @param pathInfo @@ -217,8 +215,7 @@ public final class AutoLinkResolver implements IComponentResolver * @return a new auto component or null in case this method couldn't resolve to a proper * auto component */ - Component newAutoComponent(final MarkupContainer container, final String autoId, - final PathInfo pathInfo); + Component newAutoComponent(final String autoId, final PathInfo pathInfo); } /** @@ -246,15 +243,23 @@ public final class AutoLinkResolver implements IComponentResolver /** The original reference (e.g the full value of a href attribute). */ private final String reference; + /** The container for this path */ + private final MarkupContainer container; + + /** Parent markup stream */ + private final MarkupStream markupStream; + /** * Construct. * * @param reference * the original reference (e.g the full value of a href attribute) */ - public PathInfo(final String reference) + public PathInfo(final String reference, MarkupContainer container, MarkupStream markupStream) { this.reference = reference; + this.container = container; + this.markupStream = markupStream; // If href contains URL query parameters .. String infoPath; // get the query string @@ -369,6 +374,26 @@ public final class AutoLinkResolver implements IComponentResolver { return absolute; } + + /** + * Gets container. + * + * @return container + */ + public MarkupContainer getContainer() + { + return container; + } + + /** + * Gets markup stream + * + * @return markup stream + */ + public MarkupStream getMarkupStream() + { + return markupStream; + } } /** @@ -399,21 +424,21 @@ public final class AutoLinkResolver implements IComponentResolver } /** - * @see org.apache.wicket.markup.resolver.AutoLinkResolver.IAutolinkResolverDelegate#newAutoComponent(org.apache.wicket.MarkupContainer, - * java.lang.String, org.apache.wicket.markup.resolver.AutoLinkResolver.PathInfo) + * @see org.apache.wicket.markup.resolver.AutoLinkResolver.IAutolinkResolverDelegate#newAutoComponent(java.lang.String, + * org.apache.wicket.markup.resolver.AutoLinkResolver.PathInfo) */ @Override @SuppressWarnings("unchecked") - public Component newAutoComponent(final MarkupContainer container, final String autoId, - PathInfo pathInfo) + public Component newAutoComponent(final String autoId, PathInfo pathInfo) { + final MarkupContainer container = pathInfo.getContainer(); + if ((pathInfo.extension != null) && supportedPageExtensions.contains(pathInfo.extension)) { // Obviously a href like href="myPkg.MyLabel.html" will do as // well. Wicket will not throw an exception. It accepts it. - Page page = container.getPage(); final IClassResolver defaultClassResolver = page.getApplication() .getApplicationSettings() @@ -482,8 +507,7 @@ public final class AutoLinkResolver implements IComponentResolver { // not a registered type for bookmarkable pages; create a link // to a resource instead - return newPackageResourceReferenceAutoComponent(container, autoId, pathInfo, - attribute); + return newPackageResourceReferenceAutoComponent(autoId, pathInfo, attribute); } // fallthrough @@ -576,24 +600,19 @@ public final class AutoLinkResolver implements IComponentResolver this.parent = parent; this.attribute = attribute; - ResourceReference reference = null; // Check whether it is a valid resource reference - Class<?> cursor = clazz; - // iterate all parents because the auto linked resource may come from - // inherited markup - while (cursor != null && cursor != Object.class) + if (PackageResource.exists(clazz, href, getLocale(), getStyle(), getVariation())) { - if (PackageResource.exists(cursor, href, getLocale(), getStyle(), getVariation())) + // Create the component implementing the link + resourceReference = new PackageResourceReference(clazz, href, getLocale(), + getStyle(), getVariation()); + } + else { - // Create the component implementing the link - reference = new PackageResourceReference(cursor, href, getLocale(), getStyle(), - getVariation()); - break; + // The resource does not exist. Set to null and ignore when + // rendering. + resourceReference = null; } - cursor = cursor.getSuperclass(); - - } - resourceReference = reference; } /** @@ -670,14 +689,13 @@ public final class AutoLinkResolver implements IComponentResolver } /** - * @see org.apache.wicket.markup.resolver.AutoLinkResolver.IAutolinkResolverDelegate#newAutoComponent(org.apache.wicket.MarkupContainer, - * java.lang.String, org.apache.wicket.markup.resolver.AutoLinkResolver.PathInfo) + * @see org.apache.wicket.markup.resolver.AutoLinkResolver.IAutolinkResolverDelegate#newAutoComponent(java.lang.String, + * org.apache.wicket.markup.resolver.AutoLinkResolver.PathInfo) */ @Override - public Component newAutoComponent(final MarkupContainer container, final String autoId, - final PathInfo pathInfo) + public Component newAutoComponent(final String autoId, final PathInfo pathInfo) { - return newPackageResourceReferenceAutoComponent(container, autoId, pathInfo, attribute); + return newPackageResourceReferenceAutoComponent(autoId, pathInfo, attribute); } } @@ -809,10 +827,25 @@ public final class AutoLinkResolver implements IComponentResolver // Must be marked as autolink tag if (tag.isAutolinkEnabled()) { + // get the reference resolver + ITagReferenceResolver referenceResolver = tagNameToTagReferenceResolvers.get(tag.getName()); + if (referenceResolver == null) + { + // fallback on default + referenceResolver = DEFAULT_ATTRIBUTE_RESOLVER; + } + + // get the reference, which is typically the value of e.g. a href or src + // attribute + String reference = referenceResolver.getReference(tag); + + // create the path info object + PathInfo pathInfo = new PathInfo(reference, container, markupStream); + // Try to find the Page matching the href // Note: to not use tag.getId() because it will be modified while // resolving the link and hence the 2nd render will fail. - Component link = resolveAutomaticLink(container, WicketLinkTagHandler.AUTOLINK_ID, tag); + Component link = resolveAutomaticLink(pathInfo, WicketLinkTagHandler.AUTOLINK_ID, tag); if (log.isDebugEnabled()) { @@ -834,7 +867,7 @@ public final class AutoLinkResolver implements IComponentResolver * <p> * None html references are treated similar. * - * @param container + * @param pathInfo * The container where the link is * @param id * the name of the component @@ -842,9 +875,10 @@ public final class AutoLinkResolver implements IComponentResolver * the component tag * @return A BookmarkablePageLink<?> to handle the href */ - private final Component resolveAutomaticLink(final MarkupContainer container, final String id, + private final Component resolveAutomaticLink(final PathInfo pathInfo, final String id, final ComponentTag tag) { + final MarkupContainer container = pathInfo.getContainer(); final Page page = container.getPage(); // Make the id (page-)unique @@ -859,28 +893,13 @@ public final class AutoLinkResolver implements IComponentResolver { tag.setAutoComponentTag(true); } - // tag.setId(autoId); - - // get the reference resolver - ITagReferenceResolver referenceResolver = tagNameToTagReferenceResolvers.get(tagName); - if (referenceResolver == null) - { - // fallback on default - referenceResolver = DEFAULT_ATTRIBUTE_RESOLVER; - } - - // get the reference, which is typically the value of e.g. a href or src - // attribute - String reference = referenceResolver.getReference(tag); - // create the path info object - PathInfo pathInfo = new PathInfo(reference); // now get the resolver delegate IAutolinkResolverDelegate autolinkResolverDelegate = tagNameToAutolinkResolverDelegates.get(tagName); Component autoComponent = null; if (autolinkResolverDelegate != null) { - autoComponent = autolinkResolverDelegate.newAutoComponent(container, autoId, pathInfo); + autoComponent = autolinkResolverDelegate.newAutoComponent(autoId, pathInfo); } if (autoComponent == null)
