Author: simoneg
Date: Thu Dec 17 23:55:13 2009
New Revision: 892015

URL: http://svn.apache.org/viewvc?rev=892015&view=rev
Log:
New head handling makes pieces work better when invoked from a velocity template

Added:
    
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/QuiteReferenceInsertion.java
    
labs/magma/trunk/website-velocity/src/main/resources/META-INF/magma.prod.default.properties
    
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/HeadWritingHtmlPiece.java
    labs/magma/trunk/website-velocity/src/test/resources/META-INF/
    
labs/magma/trunk/website-velocity/src/test/resources/META-INF/magma.properties
    
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPieceHead.vm
Modified:
    
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj
    
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj
    
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/VelocityHtmlProducer.java
    
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java

Modified: 
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj?rev=892015&r1=892014&r2=892015&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj
 (original)
+++ 
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj
 Thu Dec 17 23:55:13 2009
@@ -121,7 +121,8 @@
                return ret;
        }
        
-       after(CompoundableProducer prod, Head h) returning : execution(void 
CompoundableProducer+.head(Head)) && args(h) && this(prod) {
+       void around(HtmlProducer prod, Head h): execution(void 
HtmlProducer+.head(Head)) && args(h) && this(prod) {
+               proceed(prod, h);
                if (prod.compound == null) return;
                for (CompoundRelation rel : prod.compound) {
                        if (rel.prod instanceof HtmlProducer) {

Modified: 
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=892015&r1=892014&r2=892015&view=diff
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj
 (original)
+++ 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj
 Thu Dec 17 23:55:13 2009
@@ -17,6 +17,7 @@
 package org.apache.magma.website.velocity;
 
 import org.apache.magma.beans.BeanData;
+import org.apache.magma.website.Head;
 import org.apache.magma.beans.MagmaBeanSupport;
 import org.apache.magma.beans.PropertyInfo;
 import org.apache.magma.website.htmlpieces.HtmlOutputPiece;
@@ -25,20 +26,22 @@
 import org.apache.commons.lang.StringEscapeUtils;
 
 // TODO change this to use pieces
-public aspect FormatFields percflow(rendering()) {
+public aspect FormatFields percflow(rendering(Head)) {
 
-       pointcut rendering() : execution(boolean ASTReference.render(..));
+       pointcut rendering(Head head) : 
+               execution(boolean ASTReference.render(..)) && 
+               cflow(execution(public void VelocityHtmlProducer.head(Head)) && 
args(head));
        pointcut resolving() : execution(Object ASTReference.execute(..));
        
-       pointcut calledForRendering(MagmaBeanSupport bean) : execution(* 
MagmaBeanSupport+.get*()) 
-               && cflow(rendering())
+       pointcut calledForRendering(MagmaBeanSupport bean, Head head) : 
execution(* MagmaBeanSupport+.get*()) 
+               && cflow(rendering(head))
                && this(bean);
        
        private Object ret = null;
        private boolean nativeType = false;
        private String doreturn = null;
 
-    after (MagmaBeanSupport bean) returning (Object val): 
calledForRendering(bean) {
+    after (MagmaBeanSupport bean, Head head) returning (Object val): 
calledForRendering(bean, head) {
        BeanData beanData = bean.beanData();
        PropertyInfo property = 
beanData.findProperty(((MethodSignature)thisJoinPointStaticPart.getSignature()).getMethod());
        ret = val;
@@ -48,6 +51,7 @@
        nativeType = property.getType().isPrimitive();
        val = property.fromUnderlying(val);
        HtmlOutputPiece piece = property.getHtmlOutputPiece();
+       piece.head(val, head);
        doreturn = piece.output(val);
        /*
        if (val instanceof String && property.getType().equals(String.class)) {

Added: 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/QuiteReferenceInsertion.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/QuiteReferenceInsertion.java?rev=892015&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/QuiteReferenceInsertion.java
 (added)
+++ 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/QuiteReferenceInsertion.java
 Thu Dec 17 23:55:13 2009
@@ -0,0 +1,11 @@
+package org.apache.magma.website.velocity;
+
+import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+
+public class QuiteReferenceInsertion implements ReferenceInsertionEventHandler 
{
+
+       public Object referenceInsert(String reference, Object value) {
+               return value != null ? value : "";
+       }
+
+}

Modified: 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/VelocityHtmlProducer.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/VelocityHtmlProducer.java?rev=892015&r1=892014&r2=892015&view=diff
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/VelocityHtmlProducer.java
 (original)
+++ 
labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/VelocityHtmlProducer.java
 Thu Dec 17 23:55:13 2009
@@ -22,11 +22,13 @@
 import org.apache.magma.website.producers.TemplatingProducer;
 
 import java.beans.Introspector;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.net.URL;
@@ -48,21 +50,8 @@
        }
        
        private String template;
+       private String produced;
 
-       @Override
-       public void produce(Writer writer) throws IOException {
-               String tplname = this.template;
-               if (tplname == null && this.parameters != null) {
-                       tplname = parameters.getTemplateName();
-               }
-               if (tplname == null) throw new MagmaException("Cannot find any 
template");
-               
-               if (!tplname.endsWith(".vm")) tplname += ".vm";
-               
-               
-               sendTemplate(writer, tplname, null);
-       }
-       
        private boolean sendTemplateIfExisting(OutputStream stream, String 
tplname, Map<String, Object> additional) {
                if (parameters != null && getCreatingHandler() != null) {
                        URL resource = 
getCreatingHandler().getClass().getResource(tplname);
@@ -149,6 +138,28 @@
                        tplname += "-head.vm";
                        sendTemplateIfExisting(head.getStream(), tplname, 
additionals);
                }
+               
+               StringWriter writer = new StringWriter();
+               tplname = this.template;
+               if (tplname == null && this.parameters != null) {
+                       tplname = parameters.getTemplateName();
+               }
+               if (tplname == null) throw new MagmaException("Cannot find any 
template");
+               
+               if (!tplname.endsWith(".vm")) tplname += ".vm";
+               
+               sendTemplate(writer, tplname, null);
+               this.produced = writer.toString();
        }
+       
+       @Override
+       public void produce(Writer writer) throws IOException {
+               if (this.produced == null) {
+                       this.head(new Head(new ByteArrayOutputStream()));
+               }
+               writer.write(this.produced);
+       }
+       
+       
 
 }

Added: 
labs/magma/trunk/website-velocity/src/main/resources/META-INF/magma.prod.default.properties
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/main/resources/META-INF/magma.prod.default.properties?rev=892015&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/main/resources/META-INF/magma.prod.default.properties
 (added)
+++ 
labs/magma/trunk/website-velocity/src/main/resources/META-INF/magma.prod.default.properties
 Thu Dec 17 23:55:13 2009
@@ -0,0 +1,15 @@
+#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.
+eventhandler.referenceinsertion.class = 
org.apache.magma.website.velocity.QuiteReferenceInsertion
\ No newline at end of file

Added: 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/HeadWritingHtmlPiece.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/HeadWritingHtmlPiece.java?rev=892015&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/HeadWritingHtmlPiece.java
 (added)
+++ 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/HeadWritingHtmlPiece.java
 Thu Dec 17 23:55:13 2009
@@ -0,0 +1,27 @@
+package org.apache.magma.website.velocity;
+
+import org.apache.magma.website.Head;
+import org.apache.magma.website.htmlpieces.HtmlOutputPiece;
+
+public class HeadWritingHtmlPiece implements HtmlOutputPiece<FakeBean> {
+
+       public HtmlOutputPiece<FakeBean> createFor(Class<? extends FakeBean> 
clazz) {
+               return this;
+       }
+
+       public void head(FakeBean value, Head head) {
+               head.addCss("test.css");
+       }
+
+       public String output(FakeBean value) {
+               return "test content";
+       }
+
+       public boolean outputs(Class<?> clazz) {
+               return FakeBean.class.isAssignableFrom(clazz);
+       }
+
+       public void setStyle(String style) {
+       }
+
+}

Modified: 
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=892015&r1=892014&r2=892015&view=diff
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java
 (original)
+++ 
labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java
 Thu Dec 17 23:55:13 2009
@@ -17,12 +17,14 @@
 package org.apache.magma.website.velocity;
 
 import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.*;
 
 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;
@@ -63,6 +65,8 @@
                VelocityHtmlProducer prod = new 
VelocityHtmlProducer("testGettersFormatting.vm");
                prod.addParameter("bean", fb);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ByteArrayOutputStream heads = new ByteArrayOutputStream();
+               prod.head(new Head(heads));
                prod.produce(baos);
                String str = new String(baos.toByteArray(), "UTF-8");
                assertEquals("1979/03/05", str.trim());
@@ -74,6 +78,8 @@
                VelocityHtmlProducer prod = new 
VelocityHtmlProducer("testPrimitiveFormatting.vm");
                prod.addParameter("bean", fb);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ByteArrayOutputStream heads = new ByteArrayOutputStream();
+               prod.head(new Head(heads));
                prod.produce(baos);
                String str = new String(baos.toByteArray(), "UTF-8");
                assertEquals("50%", str.trim());
@@ -87,6 +93,8 @@
                VelocityHtmlProducer prod = new 
VelocityHtmlProducer("testEscapeHtml.vm");
                prod.addParameter("bean", fb);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ByteArrayOutputStream heads = new ByteArrayOutputStream();
+               prod.head(new Head(heads));
                prod.produce(baos);
                String str = new String(baos.toByteArray(), "UTF-8");
                assertEquals("this is &lt;b&gt;bold&lt;/b&gt;,<p>This is 
richtext and shoud <b>remain untouched</b></p>", str.trim());
@@ -99,10 +107,29 @@
                VelocityHtmlProducer prod = new 
VelocityHtmlProducer("testNested.vm");
                prod.addParameter("bean", fb);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ByteArrayOutputStream heads = new ByteArrayOutputStream();
+               prod.head(new Head(heads));
                prod.produce(baos);
                String str = new String(baos.toByteArray(), "UTF-8");
                assertEquals("this is &lt;b&gt;bold&lt;/b&gt;", str.trim());
        }
        
+       @Test
+       public void headWriting() throws Exception {
+               FakeBean fb = new FakeBean();
+               VelocityHtmlProducer prod = new 
VelocityHtmlProducer("testPieceHead.vm");
+               prod.addParameter("bean", fb);
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ByteArrayOutputStream heads = new ByteArrayOutputStream();
+               Head head = new Head(heads);
+               prod.head(head);
+               head.finished();
+               prod.produce(baos);
+               String str = new String(baos.toByteArray(), "UTF-8");
+               assertEquals("test content", str.trim());
+               assertTrue(new String(heads.toByteArray(), 
"UTF-8").contains("test.css"));
+               
+       }
+       
 
 }

Added: 
labs/magma/trunk/website-velocity/src/test/resources/META-INF/magma.properties
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/resources/META-INF/magma.properties?rev=892015&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/test/resources/META-INF/magma.properties 
(added)
+++ 
labs/magma/trunk/website-velocity/src/test/resources/META-INF/magma.properties 
Thu Dec 17 23:55:13 2009
@@ -0,0 +1,15 @@
+#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.
+org.apache.magma.website.htmlpieces.HtmlOutputPiece.testvelocity=org.apache.magma.website.velocity.HeadWritingHtmlPiece
\ No newline at end of file

Added: 
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPieceHead.vm
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPieceHead.vm?rev=892015&view=auto
==============================================================================
--- 
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPieceHead.vm
 (added)
+++ 
labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPieceHead.vm
 Thu Dec 17 23:55:13 2009
@@ -0,0 +1,16 @@
+## 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.
+
+$bean.me



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

Reply via email to