PatternUtil { public static Definition replacePlaceholders(Definition d, String name, Object... varsOrig) { Object[] vars = replaceNullsWithBlank(varsOrig); Definition nudef = new Definition(); nudef.setExtends(replace(d.getExtends(), vars)); nudef.setName(name); nudef.setPreparer(replace(d.getPreparer(), vars)); Attribute templateAttribute = d.getTemplateAttribute(); if (templateAttribute != null) { nudef.setTemplateAttribute(replaceVarsInAttribute( templateAttribute, vars)); } Set<String> attributeNames = d.getLocalAttributeNames(); if (attributeNames != null && !attributeNames.isEmpty()) { for (String attributeName : attributeNames) { Attribute attr = d.getLocalAttribute(attributeName); Attribute nuattr = replaceVarsInAttribute(attr, vars); nudef.putAttribute(replace(attributeName, vars), nuattr); } } attributeNames = d.getCascadedAttributeNames(); if (attributeNames != null && !attributeNames.isEmpty()) { for (String attributeName : attributeNames) { Attribute attr = d.getCascadedAttribute(attributeName); Attribute nuattr = replaceVarsInAttribute(attr, vars); nudef.putAttribute(replace(attributeName, vars), nuattr, true); } } return nudef; } private  PatternUtil(); static Definition replacePlaceholders(Definition d, String name,
            Object... varsOrig); static Map<K, V> createExtractedMap(Map<K, V> map, Set<K> keys);  }
@Test public void testReplacePlaceholders() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); attributes.put("attrib1", new Attribute("value{2}")); attributes.put("attrib2", new Attribute("value{2}{3}")); attributes.put("attrib3", new Attribute(null, Expression .createExpression("expr{1}", "EL"), null, null)); Definition definition = new Definition("definitionName", new Attribute( "template{1}"), attributes); definition.setExtends("{2}ext"); definition.setPreparer("{3}prep"); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertEquals("value2ext", nudef.getExtends()); assertEquals("value3prep", nudef.getPreparer()); Attribute attribute = nudef.getTemplateAttribute(); assertEquals("templatevalue1", attribute.getValue()); attribute = nudef.getAttribute("attrib1"); assertEquals("valuevalue2", attribute.getValue()); attribute = nudef.getAttribute("attrib2"); assertEquals("valuevalue2value3", attribute.getValue()); attribute = nudef.getAttribute("attrib3"); assertEquals("exprvalue1", attribute.getExpressionObject().getExpression()); }
@Test public void testReplacePlaceholdersNullTemplate() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); attributes.put("attrib1", new Attribute("value{2}")); attributes.put("attrib2", new Attribute("value{2}{3}")); Definition definition = new Definition("definitionName", (Attribute) null, attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertNull(nudef.getTemplateAttribute()); Attribute attribute = nudef.getAttribute("attrib1"); assertEquals("valuevalue2", attribute.getValue()); attribute = nudef.getAttribute("attrib2"); assertEquals("valuevalue2value3", attribute.getValue()); }
@Test public void testReplacePlaceholdersCascadedAttributes() { Definition definition = new Definition("definitionName", new Attribute( "template{1}"), null); definition.putAttribute("attrib1", new Attribute("value{2}"), true); definition.putAttribute("attrib2", new Attribute("value{2}{3}"), true); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); Attribute attribute = nudef.getTemplateAttribute(); assertEquals("templatevalue1", attribute.getValue()); attribute = nudef.getAttribute("attrib1"); assertEquals("valuevalue2", attribute.getValue()); attribute = nudef.getAttribute("attrib2"); assertEquals("valuevalue2value3", attribute.getValue()); }
@Test public void testReplacePlaceholdersListAttribute() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); ListAttribute listAttribute = new ListAttribute(); ListAttribute internalListAttribute = new ListAttribute(); listAttribute.setInherit(true); attributes.put("myList", listAttribute); listAttribute.add(new Attribute("value{2}")); listAttribute.add(new Attribute("value{2}{3}")); listAttribute.add(internalListAttribute); internalListAttribute.add(new Attribute("secondvalue{2}")); internalListAttribute.add(new Attribute("secondvalue{2}{3}")); Definition definition = new Definition("definitionName", new Attribute( "template{1}"), attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); Attribute attribute = nudef.getTemplateAttribute(); assertEquals("templatevalue1", attribute.getValue()); ListAttribute nuListAttribute = (ListAttribute) nudef.getAttribute("myList"); assertTrue(nuListAttribute.isInherit()); List<Attribute> list = nuListAttribute.getValue(); assertEquals(LIST_ATTRIBUTE_SIZE, list.size()); attribute = list.get(0); assertEquals("valuevalue2", attribute.getValue()); attribute = list.get(1); assertEquals("valuevalue2value3", attribute.getValue()); ListAttribute evaluatedListAttribute = (ListAttribute) list.get(2); assertFalse(evaluatedListAttribute.isInherit()); list = evaluatedListAttribute.getValue(); assertEquals(2, list.size()); attribute = list.get(0); assertEquals("secondvaluevalue2", attribute.getValue()); attribute = list.get(1); assertEquals("secondvaluevalue2value3", attribute.getValue()); }
@Test public void testReplacePlaceholdersEL_0() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); Attribute attribute = new Attribute("some-{1}-${requestScope.someVariable}.jsp"); attribute.setExpressionObject(new Expression((String)attribute.getValue())); attributes.put("something", attribute); Definition definition = new Definition("definitionName", new Attribute("template"), attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertEquals( "some-value1-${requestScope.someVariable}.jsp", nudef.getAttribute("something").getValue()); assertEquals( "some-value1-${requestScope.someVariable}.jsp", nudef.getAttribute("something").getExpressionObject().getExpression()); }
@Test public void testReplacePlaceholdersEL_1() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); Attribute attribute = new Attribute("some-{1}-${requestScope.someVariable}-other-{2}.jsp"); attribute.setExpressionObject(new Expression((String)attribute.getValue())); attributes.put("something", attribute); Definition definition = new Definition("definitionName", new Attribute("template"), attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertEquals( "some-value1-${requestScope.someVariable}-other-value2.jsp", nudef.getAttribute("something").getValue()); assertEquals( "some-value1-${requestScope.someVariable}-other-value2.jsp", nudef.getAttribute("something").getExpressionObject().getExpression()); }
@Test public void testReplacePlaceholdersEL_2() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); Attribute attribute = new Attribute("some-${requestScope.someVariable}-other-{1}-${requestScope.someOtherVariable}.jsp"); attribute.setExpressionObject(new Expression((String)attribute.getValue())); attributes.put("something", attribute); Definition definition = new Definition("definitionName", new Attribute("template"), attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertEquals( "some-${requestScope.someVariable}-other-value1-${requestScope.someOtherVariable}.jsp", nudef.getAttribute("something").getValue()); assertEquals( "some-${requestScope.someVariable}-other-value1-${requestScope.someOtherVariable}.jsp", nudef.getAttribute("something").getExpressionObject().getExpression()); }
@Test public void testReplacePlaceholdersEL_conditional() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); Attribute attribute = new Attribute("{1}/some-other-{2}-${requestScope.someBoolean ? 'a' : 'b'}.jsp"); attribute.setExpressionObject(new Expression((String)attribute.getValue())); attributes.put("something", attribute); Definition definition = new Definition("definitionName", new Attribute("template"), attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertEquals( "value1/some-other-value2-${requestScope.someBoolean ? 'a' : 'b'}.jsp", nudef.getAttribute("something").getValue()); assertEquals( "value1/some-other-value2-${requestScope.someBoolean ? 'a' : 'b'}.jsp", nudef.getAttribute("something").getExpressionObject().getExpression()); }
@Test public void testReplacePlaceholdersEL_twice() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); Attribute attribute = new Attribute("some-${requestScope.firstVariable}-${requestScope.secondVariable}.jsp"); attribute.setExpressionObject(new Expression((String)attribute.getValue())); attributes.put("something", attribute); Definition definition = new Definition("definitionName", new Attribute("template"), attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertEquals( "some-${requestScope.firstVariable}-${requestScope.secondVariable}.jsp", nudef.getAttribute("something").getValue()); assertEquals( "some-${requestScope.firstVariable}-${requestScope.secondVariable}.jsp", nudef.getAttribute("something").getExpressionObject().getExpression()); }
@Test public void testReplacePlaceholdersEL_options() { Map<String, Attribute> attributes = new HashMap<String, Attribute>(); Attribute attribute = new Attribute("{1}/{options[my_fallback}}/some-other-{2}-${requestScope.someVariable}.jsp"); attribute.setExpressionObject(new Expression((String)attribute.getValue())); attributes.put("something", attribute); Definition definition = new Definition("definitionName", new Attribute("template"), attributes); Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef", "value0", "value1", "value2", "value3"); assertEquals("nudef", nudef.getName()); assertEquals( "value1/{options[my_fallback}}/some-other-value2-${requestScope.someVariable}.jsp", nudef.getAttribute("something").getValue()); assertEquals( "value1/{options[my_fallback}}/some-other-value2-${requestScope.someVariable}.jsp", nudef.getAttribute("something").getExpressionObject().getExpression()); }