Author: mck Date: Tue Feb 25 10:48:17 2014 New Revision: 1571643 URL: http://svn.apache.org/r1571643 Log: Use blank strings instead of nulls as placeholder values when replacing placeholders.
Reference from mailing list: http://thread.gmane.org/gmane.comp.apache.tiles.user/685 > <definition name="layout" template="/WEB-INF/layout/web.jsp"/> > <definition name="layout.mob" template="/WEB-INF/layout/mob.jsp"/> > <definition name="layout.tab" template="/WEB-INF/layout/tab.jsp"/> > <definition name="REGEXP:home(\.(mob|tab))?" extends="layout{1}"> > <put-attribute name="heading" value="Home" /> > <put-list-attribute name="body"> > <add-attribute value="/WEB-INF/views/home.jsp" /> > </put-list-attribute> > </definition> > > The above rules match a pattern employed with spring-mobile where > views can be resolved based on (mob)ile/(tab)let/normal , as normal is > a fallback it does not contain a suffix. > > I can understand why this fails as the capture {1} does not exist in > the found pattern, instead I'd like it to recognise this and evaluate > to an empty string. > > Full stack-trace is as follows: > java.lang.NullPointerException > at > org.apache.tiles.BasicAttributeContext.inherit(BasicAttributeContext.java:212) > at > org.apache.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO.getDefinitionFromResolver(ResolvingLocaleUrlDefinitionDAO.java:84) > … The code ends up looking for "layoutnull" instead of "layout". This comes from in RegexpDefinitionPatternMatcher.createDefinition(..) the call to matcher.group(i) returning null. Rather than fix it there though i'd rather put the fix in lower down in PatternUtil.replacePlaceholders(..) so that any nulls in the vars parameter are always replaced with blanks. Modified: tiles/framework/branches/TILES_3_0_X/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java Modified: tiles/framework/branches/TILES_3_0_X/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java URL: http://svn.apache.org/viewvc/tiles/framework/branches/TILES_3_0_X/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java?rev=1571643&r1=1571642&r2=1571643&view=diff ============================================================================== --- tiles/framework/branches/TILES_3_0_X/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java (original) +++ tiles/framework/branches/TILES_3_0_X/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java Tue Feb 25 10:48:17 2014 @@ -67,12 +67,15 @@ public final class PatternUtil { * * @param d The definition to replace. * @param name The name of the definition to be created. - * @param vars The variables to be substituted. + * @param varsOrig The variables to be substituted. * @return The definition that can be rendered. * @since 2.2.0 */ public static Definition replacePlaceholders(Definition d, String name, - Object... vars) { + Object... varsOrig) { + + Object[] vars = replaceNullsWithBlank(varsOrig); + Definition nudef = new Definition(); nudef.setExtends(replace(d.getExtends(), vars)); @@ -229,4 +232,12 @@ public final class PatternUtil { } return st; } + + private static Object[] replaceNullsWithBlank(Object[] varsOrig) { + Object[] vars = new Object[varsOrig.length]; + for(int i = 0; i < varsOrig.length; ++i) { + vars[i] = null != varsOrig[i] ? varsOrig[i] : ""; + } + return vars; + } }
