You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2008/10/29 18:53:21 UTC

svn commit: r708956 - in /labs/magma/trunk/website-velocity: ./ src/main/java/org/apache/magma/website/velocity/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/magma/ src/test/java/org/apache/magma/websit...

Author: simoneg
Date: Wed Oct 29 10:53:21 2008
New Revision: 708956

URL: http://svn.apache.org/viewvc?rev=708956&view=rev
Log:
LABS-191 : formatting of properties taken from beans while velocity is processing it

Added:
    labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj
    labs/magma/trunk/website-velocity/src/test/
    labs/magma/trunk/website-velocity/src/test/java/
    labs/magma/trunk/website-velocity/src/test/java/org/
    labs/magma/trunk/website-velocity/src/test/java/org/apache/
    labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/
    labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/
    labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/
    labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java
    labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java
    labs/magma/trunk/website-velocity/src/test/resources/
    labs/magma/trunk/website-velocity/src/test/resources/org/
    labs/magma/trunk/website-velocity/src/test/resources/org/apache/
    labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/
    labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/
    labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/
    labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testEscapeHtml.vm
    labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testGettersFormatting.vm
Modified:
    labs/magma/trunk/website-velocity/pom.xml

Modified: labs/magma/trunk/website-velocity/pom.xml
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/pom.xml?rev=708956&r1=708955&r2=708956&view=diff
==============================================================================
--- labs/magma/trunk/website-velocity/pom.xml (original)
+++ labs/magma/trunk/website-velocity/pom.xml Wed Oct 29 10:53:21 2008
@@ -1,28 +1,28 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- 
-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.
+	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.
 -->
 <project xmlns="http://maven.apache.org/POM/4.0.0"
-	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 	<modelVersion>4.0.0</modelVersion>
 	<parent>
-	  <groupId>org.apache.magma</groupId>
-	  <artifactId>magma-parent</artifactId>
-	  <version>1</version>
+		<groupId>org.apache.magma</groupId>
+		<artifactId>magma-parent</artifactId>
+		<version>1</version>
 	</parent>
 	<groupId>org.apache.magma</groupId>
 	<artifactId>website-velocity</artifactId>
@@ -47,5 +47,11 @@
 			<artifactId>velocity</artifactId>
 			<version>1.5</version>
 		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.5</version>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
-</project>
+</project>
\ No newline at end of file

Added: labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj?rev=708956&view=auto
==============================================================================
--- labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj (added)
+++ labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj Wed Oct 29 10:53:21 2008
@@ -0,0 +1,46 @@
+package org.apache.magma.website.velocity;
+
+import org.apache.magma.beans.BeanData;
+import org.apache.magma.beans.MagmaBeanSupport;
+import org.apache.magma.beans.PropertyInfo;
+import org.apache.magma.validation.CompoundValidator;
+import org.apache.magma.validation.validators.RichTextValidator;
+import org.apache.velocity.runtime.parser.node.ASTReference;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.apache.commons.lang.StringEscapeUtils;
+
+public aspect FormatFields percflow(rendering()) {
+
+	pointcut rendering() : execution(boolean ASTReference.render(..));
+	pointcut resolving() : execution(Object ASTReference.execute(..));
+	
+	pointcut calledForRendering(MagmaBeanSupport bean) : execution(* MagmaBeanSupport+.get*()) 
+		&& cflow(rendering())
+		&& this(bean);
+	
+	private String doreturn = null;
+
+    after (MagmaBeanSupport bean) returning (Object val): calledForRendering(bean) {
+    	BeanData beanData = bean.beanData();
+    	PropertyInfo property = beanData.findProperty(((MethodSignature)thisJoinPointStaticPart.getSignature()).getMethod());
+    	if (property == null) {    		
+    		return;
+    	}
+    	if (val instanceof String) {
+			CompoundValidator validators = property.getValidator();
+			if (validators == null || validators.getValidator(RichTextValidator.class) == null) {
+				System.out.println("Escaping");
+				doreturn = StringEscapeUtils.escapeHtml((String)val);
+				return;
+			}
+    	}
+    	doreturn = property.toUser(val);
+    }
+    
+    Object around() : resolving() {
+    	Object unfiltered = proceed();
+    	if (doreturn != null) return doreturn;
+    	return unfiltered;
+    }
+	
+}

Added: labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java?rev=708956&view=auto
==============================================================================
--- labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java (added)
+++ labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java Wed Oct 29 10:53:21 2008
@@ -0,0 +1,30 @@
+package org.apache.magma.website.velocity;
+
+import java.util.Date;
+
+import org.apache.magma.beans.MagmaBean;
+import org.apache.magma.i18n.Format;
+
+@MagmaBean
+public class FakeBean {
+	private Date date = null;
+	private String string = null;
+
+	@Format(format="yyyy/MM/dd")
+	public Date getDate() {
+		return date;
+	}
+
+	public void setDate(Date date) {
+		this.date = date;
+	}
+
+	public String getString() {
+		return string;
+	}
+
+	public void setString(String string) {
+		this.string = string;
+	}
+	
+}

Added: labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java?rev=708956&view=auto
==============================================================================
--- labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java (added)
+++ labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java Wed Oct 29 10:53:21 2008
@@ -0,0 +1,43 @@
+package org.apache.magma.website.velocity;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.StringWriter;
+import java.text.SimpleDateFormat;
+
+import org.apache.magma.website.WebHandler;
+import org.junit.Test;
+import static junit.framework.Assert.*;
+
+
+public class TestGettersFormatting extends WebHandler {
+
+	@Test
+	public void formattedDate() throws Exception {
+		FakeBean fb = new FakeBean();
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+		fb.setDate(sdf.parse("1979-03-05"));
+		VelocityHtmlProducer prod = new VelocityHtmlProducer("testGettersFormatting.vm");
+		prod.addParameter("bean", fb);
+		prod.setCreatingHandler(this);
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		prod.produce(baos);
+		String str = new String(baos.toByteArray(), "UTF-8");
+		assertEquals("1979/03/05", str.trim());
+	}
+	
+	@Test
+	public void escapeHtml() throws Exception {
+		FakeBean fb = new FakeBean();
+		fb.setString("this is <b>bold</b>");
+		VelocityHtmlProducer prod = new VelocityHtmlProducer("testEscapeHtml.vm");
+		prod.addParameter("bean", fb);
+		prod.setCreatingHandler(this);
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		prod.produce(baos);
+		String str = new String(baos.toByteArray(), "UTF-8");
+		assertEquals("this is &lt;b&gt;bold&lt;/b&gt;", str.trim());
+	}
+	
+
+}

Added: labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testEscapeHtml.vm
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testEscapeHtml.vm?rev=708956&view=auto
==============================================================================
--- labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testEscapeHtml.vm (added)
+++ labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testEscapeHtml.vm Wed Oct 29 10:53:21 2008
@@ -0,0 +1 @@
+$bean.string
\ No newline at end of file

Added: labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testGettersFormatting.vm
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testGettersFormatting.vm?rev=708956&view=auto
==============================================================================
--- labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testGettersFormatting.vm (added)
+++ labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testGettersFormatting.vm Wed Oct 29 10:53:21 2008
@@ -0,0 +1 @@
+$bean.date
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org