[Tapestry-commits] CVS: Tapestry/framework/src/net/sf/tapestry/parse TemplateParser.java,1.8.2.1,1.8
Brought to you by:
hship
|
From: Howard L. S. <hs...@us...> - 2002-11-30 03:33:23
|
Update of /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/parse
In directory sc8-pr-cvs1:/tmp/cvs-serv15444/framework/src/net/sf/tapestry/parse
Modified Files:
Tag: hship-2-3
TemplateParser.java OpenToken.java
Log Message:
Add support for providing expressions in component templates.
Index: TemplateParser.java
===================================================================
RCS file: /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/parse/TemplateParser.java,v
retrieving revision 1.8.2.1
retrieving revision 1.8.2.2
diff -C2 -d -r1.8.2.1 -r1.8.2.2
*** TemplateParser.java 29 Nov 2002 20:58:25 -0000 1.8.2.1
--- TemplateParser.java 30 Nov 2002 03:33:20 -0000 1.8.2.2
***************
*** 739,746 ****
if (!isRemoveId)
{
! if (_attributes.isEmpty())
! _tokens.add(new OpenToken(tagName, jwcId, null));
! else
! _tokens.add(new OpenToken(tagName, jwcId, filter(_attributes, JWCID_ATTRIBUTE_NAME)));
if (emptyTag)
--- 739,743 ----
if (!isRemoveId)
{
! addOpenToken(tagName, jwcId);
if (emptyTag)
***************
*** 770,773 ****
--- 767,806 ----
}
+ private void addOpenToken(String tagName, String jwcId)
+ {
+ OpenToken token = new OpenToken(tagName, jwcId);
+ _tokens.add(token);
+
+ if (_attributes.isEmpty())
+ return;
+
+ Iterator i = _attributes.entrySet().iterator();
+ while (i.hasNext())
+ {
+ Map.Entry entry = (Map.Entry)i.next();
+
+ String key = (String)entry.getKey();
+
+ if (key.equalsIgnoreCase(JWCID_ATTRIBUTE_NAME))
+ continue;
+
+ String value = (String)entry.getValue();
+
+ // OGNL expressions look like "[[ expression ]]"
+
+ if (value.startsWith("[[") && value.endsWith("]]"))
+ {
+ value = extractExpression(value);
+
+
+ token.addExpressionValue(key, value);
+ continue;
+ }
+
+ token.addStaticValue(key, value);
+ }
+ }
+
+
/**
* Invoked to handle a closing tag, i.e., </foo>. When a tag closes, it will match against
***************
*** 1046,1049 ****
--- 1079,1139 ----
return null;
+ }
+
+ /**
+ * Conversions needed by {@link #extractExpression(String)}
+ *
+ **/
+
+ private static final String[] CONVERSIONS = {
+ "<", "<",
+ ">", ">",
+ """, "\"",
+ "&", "&"
+ };
+
+ /**
+ * Provided a raw input string that has been recognized to be an expression
+ * (starts with "[[" and ends with "]]"), this removes the outer brackets
+ * and excess white space and converts &amp;, &quot; &lt; and &gt;
+ * to their normal character values (otherwise its impossible to specify
+ * those values in expressions in the template).
+ *
+ **/
+
+ private String extractExpression(String input)
+ {
+ int inputLength = input.length() - 2;
+
+ StringBuffer buffer = new StringBuffer(inputLength - 2);
+
+ int cursor = 2;
+
+
+ while (cursor < inputLength)
+ {
+ outer:
+ for (int i = 0; i < CONVERSIONS.length; i += 2)
+ {
+ String entity = CONVERSIONS[i];
+ int entityLength = entity.length();
+ String value = CONVERSIONS[i + 1];
+
+ if (cursor + entityLength > inputLength)
+ continue;
+
+ if (input.substring(cursor, cursor + entityLength).equals(entity))
+ {
+ buffer.append(value);
+ cursor += entityLength;
+ break outer;
+ }
+ }
+
+ buffer.append(input.charAt(cursor));
+ cursor++;
+ }
+
+ return buffer.toString().trim();
}
Index: OpenToken.java
===================================================================
RCS file: /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/parse/Attic/OpenToken.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -d -r1.1.2.1 -r1.1.2.2
*** OpenToken.java 29 Nov 2002 20:58:25 -0000 1.1.2.1
--- OpenToken.java 30 Nov 2002 03:33:20 -0000 1.1.2.2
***************
*** 1,4 ****
--- 1,5 ----
package net.sf.tapestry.parse;
+ import java.util.HashMap;
import java.util.Map;
***************
*** 19,31 ****
private String _tag;
private String _id;
! private Map _attributes;
/**
! * Creates a new token with the given tag and id. Retains (does not copy)
! * the attributes (which may be null).
*
**/
! public OpenToken(String tag, String id, Map attributes)
{
super(TokenType.OPEN);
--- 20,32 ----
private String _tag;
private String _id;
! private Map _values;
! private Map _expressions;
/**
! * Creates a new token with the given tag and id.
*
**/
! public OpenToken(String tag, String id)
{
super(TokenType.OPEN);
***************
*** 33,47 ****
_tag = tag;
_id = id;
! _attributes = attributes;
}
/**
! * Returns the attributes (if any) for this tag. Do not modify the return value.
*
**/
! public Map getAttributes()
{
! return _attributes;
}
--- 34,86 ----
_tag = tag;
_id = id;
!
}
/**
! * Adds a static value with the given name.
*
**/
! public void addStaticValue(String name, String value)
{
! if (_values == null)
! _values = new HashMap();
!
! _values.put(name, value);
! }
!
! /**
! * Adds an expression with the given name.
! *
! **/
!
! public void addExpressionValue(String name, String value)
! {
! if (_expressions == null)
! _expressions = new HashMap();
!
! _expressions.put(name, value);
! }
!
! /**
! * Returns a Map of static values, keyed on attribute name. Do not modify the returned Map.
! * May return null.
! **/
!
! public Map getStaticValuesMap()
! {
! return _values;
! }
!
! /**
! * Returns a Map of OGNL expressions, keyed on attribute name.
! * Done not modify the returned Map.
! * May return null.
! *
! **/
!
! public Map getExpressionValuesMap()
! {
! return _expressions;
}
***************
*** 57,64 ****
protected void extendDescription(ToStringBuilder builder)
! {
builder.append("id", _id);
builder.append("tag", _tag);
! builder.append("attributes", _attributes);
}
--- 96,104 ----
protected void extendDescription(ToStringBuilder builder)
! {
builder.append("id", _id);
builder.append("tag", _tag);
! builder.append("values", _values);
! builder.append("expressions", _expressions);
}
|