labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r930193 - in /labs/magma/trunk/foundation-website/src: main/java/org/apache/magma/website/ main/java/org/apache/magma/website/templating/ test/java/org/apache/magma/website/templating/ test/java/org/apache/magma/website/teststuff/ test/reso...
Date Fri, 02 Apr 2010 09:12:23 GMT
Author: simoneg
Date: Fri Apr  2 09:12:22 2010
New Revision: 930193

URL: http://svn.apache.org/viewvc?rev=930193&view=rev
Log:
Support velocity in raw template zones

Added:
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/VelocityTemplate.java
    labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/velocityTemplate.vm
Modified:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/Template.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplateDataTest.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj?rev=930193&r1=930192&r2=930193&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
Fri Apr  2 09:12:22 2010
@@ -39,6 +39,8 @@ import org.aspectj.lang.reflect.FieldSig
 import org.aspectj.lang.reflect.MethodSignature;
 import org.apache.magma.website.context.Contextualizer;
 
+import org.apache.magma.website.templating.Template;
+
 
 public aspect HandlerSetup percflow(inRequest()) {
 
@@ -101,6 +103,21 @@ public aspect HandlerSetup percflow(inRe
 		return ret;
 	}
 	
+	pointcut fetchingTemplate(Template t) : 
+		(get(private * Template+.*) || get(protected * Template+.*)) 
+		&& target(t);
+	
+	Object around(Template t) : fetchingTemplate(t) {
+		FieldSignature sign = (FieldSignature)thisJoinPointStaticPart.getSignature();
+		String element = sign.getName();
+		Interaction inter = OnDemandBinder.interactsWith(element);
+		if (inter == null) return proceed(t);
+		Class type = sign.getFieldType();
+		String unique = Integer.toString(t.getClass().getName().hashCode());
+		Object ret = binder.fetch(element, type, inter, unique);
+		return ret;
+	}	
+	
 	pointcut storing(WebHandler h, Object value) : 
 		(set(private * WebHandler+.*) || set(protected * WebHandler+.*)) 
 		&& target(h)

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/Template.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/Template.java?rev=930193&r1=930192&r2=930193&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/Template.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/Template.java
Fri Apr  2 09:12:22 2010
@@ -233,7 +233,7 @@ public class Template {
 	public void parse() {
 		data = parseContent();
 		Method[] methods = getClass().getMethods();
-		for (String acZone : data.zones) {
+		for (String acZone : data.getZoneNames()) {
 			currentZone = acZone;
 			if (currentZone.equals("__head_content")) continue;
 			if (currentZone.equals("Main")) continue;
@@ -255,7 +255,7 @@ public class Template {
 		layoutMain();
 		current.clearDone();
 		
-		for (String acZone : data.zones) {
+		for (String acZone : data.getZoneNames()) {
 			List<ZonePlaceholder> list = holders.get(acZone);
 			if (list == null) continue;
 			List<HtmlProducer> producers = new ArrayList<HtmlProducer>();
@@ -271,11 +271,11 @@ public class Template {
 			// TODO use complete class name here, so that TemplateResourceHandler can check it to
provide security
 			rewrite.setTemplatePrefix("template/" + getClass().getPackage().getName() + "/");
 			OutputStreamWriter osw = new OutputStreamWriter(rewrite, Charset.forName("UTF-8"));
-			for (int i = 0; i < data.zones.size(); i++) {
+			for (int i = 0; i < data.getZoneCount(); i++) {
 				rewrite.setBaseUrl(rewrite.getTemplatePrefix());
-				osw.write(data.raws.get(i));
+				osw.write(data.getRaw(i));
 				osw.flush();
-				String zone = data.zones.get(i);
+				String zone = data.getZoneName(i);
 				if (zone.equals("__head_content")) {
 					Head head = new Head(rewrite);
 					for (List<HtmlProducer> producers : zones.values()) {
@@ -304,7 +304,7 @@ public class Template {
 				}
 			}
 			rewrite.setBaseUrl(rewrite.getTemplatePrefix());
-			osw.write(data.raws.get(data.zones.size()));
+			osw.write(data.getRaw(data.getZoneCount()));
 			osw.flush();
 			rewrite.flush();
 		} catch (IOException e) {
@@ -329,7 +329,7 @@ public class Template {
 			InputStream inputStream = getClass().getResourceAsStream(fileName);
 			try {
 				InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
-				return new TemplateData(reader);
+				return new TemplateData(reader, this);
 			} finally {
 				try {
 					inputStream.close();

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java?rev=930193&r1=930192&r2=930193&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
Fri Apr  2 09:12:22 2010
@@ -17,10 +17,14 @@
 package org.apache.magma.website.templating;
 
 import org.apache.magma.basics.MagmaException;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.context.Context;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.Reader;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -33,10 +37,12 @@ import java.util.List;
  */
 public class TemplateData {
 
-	public List<String> zones = new ArrayList<String>();
-	public List<String> raws = new ArrayList<String>();
+	private List<String> zones = new ArrayList<String>();
+	private List<String> raws = new ArrayList<String>();
+	private Template template = null;
 	
-	public TemplateData(Reader reader) {
+	public TemplateData(Reader reader, Template template) {
+		this.template = template;
 		init(reader);
 	}
 	
@@ -127,4 +133,36 @@ public class TemplateData {
 			return lp + rp;
 		}
 	}
+	
+	public String getZoneName(int i) {
+		return this.zones.get(i);
+	}
+	
+	public String getRaw(int i) {
+		String ret = this.raws.get(i);
+		if (ret.indexOf('$') != -1) {
+			Context ctx = new VelocityContext();
+			ctx.put("template", template);
+			StringWriter sw = new StringWriter();
+			try {
+				Velocity.evaluate(ctx, sw, "template", ret);
+			} catch (Exception e) {
+				throw new MagmaException(e, "Error while evaluating velocity in template");
+			}
+			sw.flush();
+			ret = sw.toString();
+		}
+		return ret;
+	}
+	
+	public List<String> getZoneNames() {
+		return this.zones;
+	}
+	
+	public int getZoneCount() {
+		return this.zones.size();
+	}
+	public int getRawsCount() {
+		return this.raws.size();
+	}
 }

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplateDataTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplateDataTest.java?rev=930193&r1=930192&r2=930193&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplateDataTest.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplateDataTest.java
Fri Apr  2 09:12:22 2010
@@ -71,18 +71,18 @@ public class TemplateDataTest {
 		public void checkParsing(String[] set) {
 			String html = set[0];
 			StringReader sr = new StringReader(html);
-			TemplateData td = new TemplateData(sr);
+			TemplateData td = new TemplateData(sr,null);
 
 			StringBuffer full = new StringBuffer();
-			for (String string : td.raws) {
-				full.append(string);
+			for (int i = 0; i < td.getRawsCount(); i++) {
+				full.append(td.getRaw(i));
 			}
 			assertEquals(html, full.toString());
 			int z = 0;
 			while (set.length < 1 + (z * 3)) {
-				String bef = td.raws.get(z);
-				String zone = td.zones.get(z);
-				String aft = td.raws.get(z + 1);
+				String bef = td.getRaw(z);
+				String zone = td.getZoneName(z);
+				String aft = td.getZoneName(z + 1);
 				assertTrue(bef.endsWith(set[1 + (z * 3)]));
 				assertEquals(set[2 + (z * 3)], zone);
 				assertTrue(aft.startsWith(set[3 + (z * 3)]));

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java?rev=930193&r1=930192&r2=930193&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java
Fri Apr  2 09:12:22 2010
@@ -28,6 +28,7 @@ import org.apache.magma.website.UnitTest
 import org.apache.magma.website.teststuff.Person;
 import org.apache.magma.website.teststuff.SimpleSecondTemplate;
 import org.apache.magma.website.teststuff.SimpleTemplate;
+import org.apache.magma.website.teststuff.VelocityTemplate;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
@@ -161,6 +162,19 @@ public class TemplatingTest {
 		assertStructure(def.out.toString(), "<div id=\"zoneMain", "ConvertedSessionHandler.doCompound]",
"<div id=\"zoneSide", "AuxiliaryWebHandler.doAuxSearch]", "AuxiliaryWebHandler.doAuxPoll]",
".doCompound]", "comp-sidea");
 	}
 	
+	@Test
+	public void velocityTemplate() throws Exception {
+		DefaultExpectations def = new DefaultExpectations() {{
+			one(session).setAttribute("user", Person.class.getName() + ":Simone");
+		}};
+		UnitTestDispatch disp = new UnitTestDispatch();
+		disp.setTemplate(new VelocityTemplate());
+		def.disp = disp;
+		def.setupMinimal("/testSession/setUser");
+		def.start();
+		
+		assertStructure(def.out.toString(), "<title>", "This title", "Simone", "</title>");
+	}
 	
 	
 }

Added: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/VelocityTemplate.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/VelocityTemplate.java?rev=930193&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/VelocityTemplate.java
(added)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/VelocityTemplate.java
Fri Apr  2 09:12:22 2010
@@ -0,0 +1,24 @@
+package org.apache.magma.website.teststuff;
+
+import org.apache.magma.website.CompoundType;
+import org.apache.magma.website.templating.Template;
+
+public class VelocityTemplate extends Template {
+	
+	private Person sessionUser;
+	
+	public VelocityTemplate() {
+		super.fileName = "velocityTemplate.vm";
+	}
+	
+	public void layoutSide() {
+		always(root().handleAux().doAuxSearch());
+		byDefault(AuxiliaryWebHandler.class, root().handleAux().doAuxPoll());
+		mainCompound(CompoundType.SIDEA);
+	}
+	
+	public String getTitle() {
+		return "This title is generated dynamically for " + sessionUser.getName();
+	}
+
+}

Added: labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/velocityTemplate.vm
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/velocityTemplate.vm?rev=930193&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/velocityTemplate.vm
(added)
+++ labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/velocityTemplate.vm
Fri Apr  2 09:12:22 2010
@@ -0,0 +1,27 @@
+<html>
+<!-- 
+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.
+-->
+  <head>
+  	<title>$template.title</title>
+  </head>
+  <body>
+    <div id="zoneMain">
+    </div>
+    <div id="zoneSide">
+    </div>
+  </body>
+</html>



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


Mime
View raw message