cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gkossakow...@apache.org
Subject svn commit: r670345 - in /cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src: changes/ main/java/org/apache/cocoon/template/instruction/ test/java/org/apache/cocoon/template/jxtg/ test/resources/org/apache/cocoon/template/jxtg/
Date Sun, 22 Jun 2008 11:49:22 GMT
Author: gkossakowski
Date: Sun Jun 22 04:49:21 2008
New Revision: 670345

URL: http://svn.apache.org/viewvc?rev=670345&view=rev
Log:
COCOON-2212: Added check for name correctnes in jx:attribute.

Thanks to Kamal Bhatt for providing a patch.

Added:
    cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToEmptyName.xml
    cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToInvalidName.xml
Modified:
    cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/changes/changes.xml
    cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/main/java/org/apache/cocoon/template/instruction/Attribute.java
    cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/java/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java

Modified: cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/changes/changes.xml?rev=670345&r1=670344&r2=670345&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/changes/changes.xml (original)
+++ cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/changes/changes.xml Sun Jun
22 04:49:21 2008
@@ -28,6 +28,9 @@
       <action dev="gkossakowski" type="add" fixes-bug="COCOON-2211" due-to="Kamal Bhatt"
due-to-email="bhatt@tt.com.au">
         Added support for jx:element - the instructions that allow dynamic element creation.
       </action>
+      <action dev="gkossakowski" type="add" fixes-bug="COCOON-2212" due-to="Kamal Bhatt"
due-to-email="bhatt@tt.com.au">
+        Added check for name correctnes in jx:attribute.
+      </action>
     </release>
     <release version="1.0.0-RC1" date="2007-??-??" description="unreleased">  
       

Modified: cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/main/java/org/apache/cocoon/template/instruction/Attribute.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/main/java/org/apache/cocoon/template/instruction/Attribute.java?rev=670345&r1=670344&r2=670345&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/main/java/org/apache/cocoon/template/instruction/Attribute.java
(original)
+++ cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/main/java/org/apache/cocoon/template/instruction/Attribute.java
Sun Jun 22 04:49:21 2008
@@ -21,7 +21,6 @@
 
 import org.apache.cocoon.el.objectmodel.ObjectModel;
 import org.apache.cocoon.el.parsing.Subst;
-import org.apache.cocoon.template.JXTemplateGenerator;
 import org.apache.cocoon.template.environment.ExecutionContext;
 import org.apache.cocoon.template.environment.ParsingContext;
 import org.apache.cocoon.template.script.Invoker;
@@ -31,35 +30,28 @@
 import org.apache.cocoon.xml.ContentHandlerWrapper;
 import org.apache.cocoon.xml.XMLConsumer;
 import org.apache.cocoon.xml.util.NamespacesTable;
+import org.apache.commons.lang.StringUtils;
 import org.apache.xml.serialize.TextSerializer;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
-import org.xml.sax.helpers.AttributesImpl;
 
 /**
  * @version $Id$
  */
 public class Attribute extends Instruction {
-
+    public static  final String XML_ATTR_NAME_BLANK = "parameter: \"name\" is required";
+    public static final String XML_ATTR_NAME_INVALID = "parameter: \"name\" is an invalid
XML attribute name";
+    
     private Subst name;
     private Subst value;
 
     public Attribute(ParsingContext parsingContext, StartElement raw, Attributes attrs, Stack
stack)
             throws SAXException {
         super(raw);
-
-        Locator locator = getLocation();
-        String name = attrs.getValue("name");
-        if (name == null) {
-            throw new SAXParseException("parameter: \"name\" is required", locator, null);
-        }
-        this.name = parsingContext.getStringTemplateParser().compileExpr(name, "parameter:
\"name\": ", locator);
-
-        String value = attrs.getValue("value");
-
-        this.value = parsingContext.getStringTemplateParser().compileExpr(value, "parameter:
\"value\": ", locator);
+        this.name = getSubst("name", attrs, parsingContext, true);
+        this.value = getSubst("value", attrs, parsingContext, false);
     }
 
     public Event execute(final XMLConsumer consumer, ObjectModel objectModel,
@@ -70,13 +62,16 @@
         String valueStr = "";
         try {
             nameStr = this.name.getStringValue(objectModel);
-
+          
+            if (StringUtils.isBlank(nameStr))
+                throw new SAXParseException(XML_ATTR_NAME_BLANK, getLocation());
+
+            if (!nameStr.matches("[A-Za-z][^\\s:]*"))
+                throw new SAXParseException(XML_ATTR_NAME_INVALID, getLocation());  
+            
             if (this.value != null)
                 valueStr = this.value.getStringValue(objectModel);
             else {
-                final Attributes EMPTY_ATTRS = new AttributesImpl();
-                String elementName = "attribute";
-
                 TextSerializer serializer = new TextSerializer();
                 StringWriter writer = new StringWriter();
                 serializer.setOutputCharStream(writer);
@@ -84,23 +79,34 @@
                 ContentHandlerWrapper contentHandler = new ContentHandlerWrapper(serializer,
serializer);
                 contentHandler.startDocument();
 
-                // TODO is root element necessary for TextSerializer?
-                contentHandler.startElement(JXTemplateGenerator.NS, elementName, elementName,
EMPTY_ATTRS);
                 Invoker.execute(contentHandler, objectModel, executionContext, macroContext,
namespaces, this.getNext(), this
                         .getEndInstruction());
-                contentHandler.endElement(JXTemplateGenerator.NS, elementName, elementName);
                 contentHandler.endDocument();
                 valueStr = writer.toString();
             }
         } catch (Exception exc) {
             throw new SAXParseException(exc.getMessage(), getLocation(), exc);
         }
-        if (consumer instanceof AttributeAwareXMLConsumer) {
+        if (consumer instanceof AttributeAwareXMLConsumer) {           
             AttributeAwareXMLConsumer c = (AttributeAwareXMLConsumer) consumer;
             c.attribute("", nameStr, nameStr, "CDATA", valueStr == null ? "" : valueStr);
         } else
             throw new SAXParseException("consumer is not attribute aware", getLocation());
+        
         return getEndInstruction().getNext();
     }
+    
+    private Subst getSubst(String attrName, Attributes attrs, ParsingContext parsingContext,
boolean isRequired)
+            throws SAXParseException {
+        Locator locator = getLocation();
+        String value = attrs.getValue(attrName);
+        if (isRequired && value == null) {
+            throw new SAXParseException("parameter: \"" + attrName + "\" is required", locator,
null);
+        } else if (!isRequired && value == null) {
+            return null;
+        }
+
+        return parsingContext.getStringTemplateParser().compileExpr(value, "parameter: \""
+ attrName + "\": ", locator);
+    }     
 
 }

Modified: cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/java/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/java/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java?rev=670345&r1=670344&r2=670345&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/java/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java
(original)
+++ cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/java/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java
Sun Jun 22 04:49:21 2008
@@ -28,6 +28,7 @@
 import org.xml.sax.SAXParseException;
 
 import org.apache.cocoon.template.instruction.Element;
+import org.apache.cocoon.template.instruction.Attribute;
 
 /**
  * @version SVN $Id$
@@ -185,6 +186,35 @@
         getNewObjectModel().cleanupLocalContext();
     }
 
+    public void testAttributeFailDueToEmptyName() throws Exception {
+	String inputURI = docBase + "jxAttribute-failDueToEmptyName.xml";
+        getNewObjectModel().markLocalContext();
+        boolean error = false;
+        
+        try {
+            generate(JX, inputURI, EMPTY_PARAMS);
+        } catch (SAXParseException e) {            
+            if (e.getMessage().equals(Attribute.XML_ATTR_NAME_BLANK))
+        	error = true;        
+        }
+        assertTrue("should throw sax exception due to empty name", error);      
+        getNewObjectModel().cleanupLocalContext();
+    }    
+    
+    public void testAttributeFailDueToInvalidName() throws Exception {
+	String inputURI = docBase + "jxAttribute-failDueToInvalidName.xml";
+        getNewObjectModel().markLocalContext();
+        boolean error = false;    
+        try {
+            generate(JX, inputURI, EMPTY_PARAMS);
+        } catch (SAXParseException e) {          
+            if (e.getMessage().equals(Attribute.XML_ATTR_NAME_INVALID))
+        	error = true;        
+        }
+        assertTrue("should throw sax exception due to invalid name", error);      
+        getNewObjectModel().cleanupLocalContext();
+    }    
+    
     public void testFormatDate() throws Exception {
         String inputURI = docBase + "formatDate.xml";
         String outputURI = docBase + "formatDate-output.xml";
@@ -269,7 +299,7 @@
         getNewObjectModel().cleanupLocalContext();
     }
 
-    public void testPrefixMappingFailDueToNoNameSpecified() throws Exception {
+    public void testElementFailDueToNoNameSpecified() throws Exception {
         String inputURI = docBase + "jxElement-noNameSpecified.xml";
         getNewObjectModel().markLocalContext();
         boolean error = false;
@@ -283,7 +313,7 @@
         getNewObjectModel().cleanupLocalContext();
     }
     
-    public void testPrefixMappingFailDueToInvalidNameSpecified() throws Exception {
+    public void testElementFailDueToInvalidNameSpecified() throws Exception {
         String inputURI = docBase + "jxElement-invalidNameSpecified.xml";
         getNewObjectModel().markLocalContext();
         boolean error = false;
@@ -298,7 +328,7 @@
         getNewObjectModel().cleanupLocalContext();
     }
     
-    public void testPrefixMappingFailDueToMissingNamespaceSpecifedPrefix() throws Exception
{
+    public void testElementFailDueToMissingNamespaceSpecifedPrefix() throws Exception {
 	String inputURI = docBase + "jxElement-missingNamespacePrefixSpecified.xml";
         getNewObjectModel().markLocalContext();
         boolean error = false;
@@ -313,7 +343,7 @@
         getNewObjectModel().cleanupLocalContext();
     }    
     
-    public void testPrefixMappingFailDueToEmptyName() throws Exception {
+    public void testElementFailDueToEmptyName() throws Exception {
 	String inputURI = docBase + "jxElement-failDueToEmptyName.xml";
         getNewObjectModel().markLocalContext();
         boolean error = false;
@@ -328,7 +358,7 @@
         getNewObjectModel().cleanupLocalContext();
     }      
     
-    public void testPrefixMappingFailDueToInvalidPrefix() throws Exception {
+    public void testElementFailDueToInvalidPrefix() throws Exception {
 	String inputURI = docBase + "jxElement-failDueToInvalidPrefix.xml";
         getNewObjectModel().markLocalContext();
         boolean error = false;

Added: cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToEmptyName.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToEmptyName.xml?rev=670345&view=auto
==============================================================================
--- cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToEmptyName.xml
(added)
+++ cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToEmptyName.xml
Sun Jun 22 04:49:21 2008
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
+    <jx:set var="a" value=""/>
+    <one>
+      <jx:attribute name="${a}">asdf</jx:attribute>
+    </one>
+</root>

Added: cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToInvalidName.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToInvalidName.xml?rev=670345&view=auto
==============================================================================
--- cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToInvalidName.xml
(added)
+++ cocoon/trunk/blocks/cocoon-template/cocoon-template-impl/src/test/resources/org/apache/cocoon/template/jxtg/jxAttribute-failDueToInvalidName.xml
Sun Jun 22 04:49:21 2008
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
+  <jx:set var="a" value="Invalid Name"/>
+  <one>
+    <jx:attribute name="${a}">asdf</jx:attribute>
+  </one>
+</root>



Mime
View raw message