Author: hlship
Date: Thu Dec 13 12:32:04 2007
New Revision: 604019
URL: http://svn.apache.org/viewvc?rev=604019&view=rev
Log:
TAPESTRY-1975: Template parser is insufficiently picky about component ids
Added:
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_block_id.tml
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_component_id.tml
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/templates.apt
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/TemplateParserImplTest.java
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java?rev=604019&r1=604018&r2=604019&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
Thu Dec 13 12:32:04 2007
@@ -385,4 +385,9 @@
{
return MESSAGES.format("base-class-in-wrong-package", parentClassName,
className, suggestedPackage);
}
+
+ static String invalidId(String messageKey, String idValue)
+ {
+ return MESSAGES.format(messageKey, idValue);
+ }
}
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java?rev=604019&r1=604018&r2=604019&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java
Thu Dec 13 12:32:04 2007
@@ -52,6 +52,10 @@
public static final String TAPESTRY_SCHEMA_5_0_0 =
"http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";
+ private static final String ID_REGEXP = "^[a-z]([a-z]|[0-9]|_)*$";
+
+ private static final Pattern ID_PATTERN = Pattern.compile(ID_REGEXP,
Pattern.CASE_INSENSITIVE);
+
private XMLReader _reader;
// Resource being parsed
@@ -287,8 +291,6 @@
return;
}
- // TODO: Handle interpolations inside attributes?
-
startPossibleComponent(attributes, localName, null);
}
@@ -360,6 +362,8 @@
{
String blockId = findSingleParameter("block", "id", attributes);
+ validateId(blockId, "invalid-block-id");
+
// null is ok for blockId
_tokens.add(new BlockToken(blockId, getCurrentLocation()));
@@ -438,6 +442,9 @@
if (name.equalsIgnoreCase(ID_ATTRIBUTE_NAME))
{
id = nullForBlank(value);
+
+ validateId(id, "invalid-component-id");
+
continue;
}
@@ -484,6 +491,17 @@
// elements?
_endTagHandlerStack.push(_addEndElementToken);
+ }
+
+ private void validateId(String id, String messageKey)
+ {
+ if (id == null) return;
+
+ if (ID_PATTERN.matcher(id).matches()) return;
+
+ // Not a match.
+
+ throw new TapestryException(ServicesMessages.invalidId(messageKey,
id), getCurrentLocation(), null);
}
private void startBody()
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties?rev=604019&r1=604018&r2=604019&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
Thu Dec 13 12:32:04 2007
@@ -85,4 +85,5 @@
no-markup-from-page-render=Page %s did not generate any markup when rendered.
This could be because its template file could not be located, or because a \
render phase method in the page prevented rendering.
base-class-in-wrong-package=Base class %s (super class of %s) is not in a
controlled package and is therefore not valid. You should try moving the class
to package %s.
-
+invalid-component-id=Component id '%s' is not valid; component ids must be
valid Java identifiers: start with a letter, and consist of letters, numbers
and underscores.
+invalid-block-id=Block id '%s' is not valid; block ids must be valid Java
identifiers: start with a letter, and consist of letters, numbers and
underscores.
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/templates.apt
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/templates.apt?rev=604019&r1=604018&r2=604019&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/templates.apt
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/site/apt/guide/templates.apt Thu
Dec 13 12:32:04 2007
@@ -185,7 +185,11 @@
A block may be anonymous, or it may have an id (specified with the id
attribute). Non-anonymous blocks may be
{{{inject.html}injected}} into the component.
-
+
+ Ids must be valid Java identifiers: start with a letter, and contain only
letters, numbers and underscores.
+
+ Note that the id parameter is <not> placed in the Tapestry namespace (since
the element always <is> in the Tapestry namespace).
+
* \<parameter\>
A \<parameter\> element is a special kind of block. It is placed inside the
body of an embedded component. The block defined by the
@@ -259,7 +263,8 @@
These attributes are specified inside the t: namespace (i.e.,
<<<t:id="clear">>>).
If the id attribute is ommitted, Tapestry will assign a unique id for the
element.
-
+
+ Ids must be valid Java identifiers: start with a letter, and contain only
letters, numbers and underscores.
Any other attributes are used to {{{parameters.html}bind parameters of the
component}}. These may be formal parameters
or informal parameters. Formal parameters will have a default binding
prefix (usually "prop:"). Informal parameters
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/TemplateParserImplTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/TemplateParserImplTest.java?rev=604019&r1=604018&r2=604019&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/TemplateParserImplTest.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/TemplateParserImplTest.java
Thu Dec 13 12:32:04 2007
@@ -174,14 +174,14 @@
/**
* Test disabled when not online.
*/
- @Test(enabled = false)
+ @Test(enabled = true)
public void html_entity()
{
List<TemplateToken> tokens = tokens("html_entity.tml");
- assertEquals(tokens.size(), 3);
+ assertEquals(tokens.size(), 4);
- TextToken t = get(tokens, 1);
+ TextToken t = get(tokens, 2);
// HTML entities are parsed into values that will ultimately
// be output as numeric entities. This is less than ideal; would like
@@ -645,4 +645,31 @@
assertEquals(t2.getSystemId(), systemId);
}
+ @Test
+ public void invalid_component_id() throws Exception
+ {
+ try
+ {
+ parse("invalid_component_id.tml");
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertMessageContains(ex, "Component id 'not-valid' is not valid");
+ }
+ }
+
+ @Test
+ public void invalid_block_id() throws Exception
+ {
+ try
+ {
+ parse("invalid_block_id.tml");
+ unreachable();
+ }
+ catch (RuntimeException ex)
+ {
+ assertMessageContains(ex, "Block id 'not-valid' is not valid");
+ }
+ }
}
Added:
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_block_id.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_block_id.tml?rev=604019&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_block_id.tml
(added)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_block_id.tml
Thu Dec 13 12:32:04 2007
@@ -0,0 +1,3 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+ <t:block id="not-valid"/>
+</html>
Added:
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_component_id.tml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_component_id.tml?rev=604019&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_component_id.tml
(added)
+++
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/internal/services/invalid_component_id.tml
Thu Dec 13 12:32:04 2007
@@ -0,0 +1,3 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+ <t:foo t:id="not-valid"/>
+</html>