Author: simoneg
Date: Thu Jan 14 00:53:12 2010
New Revision: 899011

URL: http://svn.apache.org/viewvc?rev=899011&view=rev
Log:
Velocity based producer for creating raw data (like XML or json) instead of 
HTML.

Added:
    
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/RawVelocityProducer.java
    
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestRawVelocity.java
    
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testRaw.vm

Added: 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/RawVelocityProducer.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/RawVelocityProducer.java?rev=899011&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/RawVelocityProducer.java
 (added)
+++ 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/RawVelocityProducer.java
 Thu Jan 14 00:53:12 2010
@@ -0,0 +1,118 @@
+package org.apache.magma.website.velocity;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.Map;
+
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.basics.context.RunningContext;
+import org.apache.magma.website.context.WebMethodContextElement;
+import org.apache.magma.website.producers.ParametrizableProducerBase;
+import org.apache.magma.website.utils.LinkHelper;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+
+public class RawVelocityProducer extends ParametrizableProducerBase {
+
+       public RawVelocityProducer(String path, String mimeType) {
+               this.mimeType = mimeType;
+               if (path != null && path.length() == 0) path = null;
+               WebMethodContextElement ele = 
RunningContext.get().getLast(WebMethodContextElement.class);
+               if (ele == null) return;
+
+               if (path == null) {
+                       path = 
LinkHelper.linkNameFromMethodName(ele.getMethod().getName());
+               }
+               
+               String complete = null;
+               if (!path.startsWith("/")) {
+                       complete = 
ele.getHandlerInstance().getClass().getName();
+                       complete = complete.substring(0, 
complete.lastIndexOf('.'));
+                       complete = complete.replace('.','/');
+                       complete = '/' + complete;
+                       complete += '/';
+                       complete += path;
+               } else {
+                       complete = path;
+               }
+               
+               this.template = complete;
+       }
+       
+       private String template;
+       private String mimeType;
+
+       private void sendTemplate(OutputStream stream, String tplname, 
Map<String, Object> additional) {
+               OutputStreamWriter ow;
+               try {
+                       ow = new OutputStreamWriter(stream, "UTF-8");
+               } catch (UnsupportedEncodingException e) {
+                       throw new MagmaException(e, "Something is very wrong if 
we cannot find UTF-8");
+               }
+               try {
+                       sendTemplate(ow, tplname, additional);
+                       ow.flush();
+               } catch (IOException e) {
+                       throw new MagmaException(e, "Error sending velocity 
template");
+               }
+       }
+       
+       
+       private void sendTemplate(Writer writer, String tplname, Map<String, 
Object> additional) {
+               VelocityContext ctx = new VelocityContext();
+               if (parameters != null) {
+                       Map<String, Object> map = parameters.getParameters();
+                       for (Map.Entry<String, Object> entry : map.entrySet()) {
+                               ctx.put(entry.getKey(), entry.getValue());
+                       }
+               }
+               if (additional != null) {
+                       for (Map.Entry<String, Object> entry : 
additional.entrySet()) {
+                               ctx.put(entry.getKey(), entry.getValue());
+                       }
+               }
+               
+               if (parameters != null && getCreatingHandler() != null) {
+                       InputStream inputStream = 
getCreatingHandler().getClass().getResourceAsStream(tplname);
+                       if (inputStream != null) {
+                               InputStreamReader inreader = new 
InputStreamReader(inputStream, Charset.forName("UTF-8"));
+                               try {
+                                       Velocity.evaluate(ctx, writer, tplname, 
inreader);
+                                       return;
+                               } catch (Throwable e) {
+                                       throw new MagmaException(e, "Error 
executing template {0} loaded from resource {1}", tplname, 
getCreatingHandler().getClass().getResource(tplname));
+                               }
+                       }
+               }
+               
+               try {
+                       Velocity.mergeTemplate(tplname, "UTF-8", ctx, writer);
+               } catch (Exception e) {
+                       throw new MagmaException(e, "Error executing velocity 
template {0} loaded by default velocity loader", tplname);
+               }
+       }
+
+       @Override
+       public void produce(OutputStream stream) {
+               String tplname = this.template;
+               if (tplname == null && this.parameters != null) {
+                       tplname = parameters.getTemplateName();
+               }
+               if (!tplname.endsWith(".vm")) tplname += ".vm";
+               
+               sendTemplate(stream, tplname, null);
+       }
+
+       @Override
+       public String getMimeType() {
+               return mimeType;
+       }
+       
+       
+}

Added: 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestRawVelocity.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestRawVelocity.java?rev=899011&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestRawVelocity.java
 (added)
+++ 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestRawVelocity.java
 Thu Jan 14 00:53:12 2010
@@ -0,0 +1,58 @@
+package org.apache.magma.website.velocity;
+
+import static junit.framework.Assert.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.lang.reflect.Method;
+import java.text.SimpleDateFormat;
+
+import org.apache.magma.basics.context.RunningContext;
+import org.apache.magma.website.Head;
+import org.apache.magma.website.HtmlProducer;
+import org.apache.magma.website.WebHandler;
+import org.apache.magma.website.context.WebMethodContextElement;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class TestRawVelocity extends WebHandler {
+
+       @AfterClass
+       public static void cleanAll() {
+               RunningContext.cleanup();
+       }
+       
+       @Before
+       public void setupContext() throws Exception {
+               Method m = this.getClass().getMethod("doFake");
+               WebMethodContextElement wce = new WebMethodContextElement(m, 
new Object[0], this);
+               RunningContext.get().push(wce);
+       }
+       
+       @After
+       public void cleanContext() {
+               RunningContext.get().pop(WebMethodContextElement.class);
+       }
+       
+       public HtmlProducer doFake() {
+               return null;
+       }
+       
+       @Test
+       public void formattedDate() throws Exception {
+               FakeBean fb = new FakeBean();
+               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+               fb.setDate(sdf.parse("1979-03-05"));
+               fb.setString("String < with stràngè chars");
+               RawVelocityProducer prod = new 
RawVelocityProducer("testRaw.vm", "text/plain");
+               prod.addParameter("bean", fb);
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               prod.produce(baos);
+               String str = new String(baos.toByteArray(), "UTF-8");
+               assertEquals("date:" + fb.getDate().toString() +"\nstr:" + 
fb.getString(), str.trim());
+       }
+
+
+}

Added: 
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testRaw.vm
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testRaw.vm?rev=899011&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testRaw.vm
 (added)
+++ 
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testRaw.vm
 Thu Jan 14 00:53:12 2010
@@ -0,0 +1,2 @@
+date:$bean.date
+str:$bean.string
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to