labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r741714 - 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/ test/java/org/apache/magma/website/templating/ test/java/org/apach...
Date Fri, 06 Feb 2009 20:55:19 GMT
Author: simoneg
Date: Fri Feb  6 20:55:19 2009
New Revision: 741714

URL: http://svn.apache.org/viewvc?rev=741714&view=rev
Log:
LABS-251 : HtmlProducers are compound-able by default

Added:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundType.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducer.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholder.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderCompoundSide.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderForHtmlProducer.java
Modified:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/Template.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/ConvertedSessionHandler.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleTemplate.java

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundType.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundType.java?rev=741714&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundType.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundType.java
Fri Feb  6 20:55:19 2009
@@ -0,0 +1,12 @@
+package org.apache.magma.website;
+
+public enum CompoundType {
+	HEAD,
+	BEFORE,
+	AFTER,
+	SIDEA,
+	SIDEB,
+	SIDEC,
+	SIDED,
+	FOOT
+}

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducer.java?rev=741714&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducer.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducer.java
Fri Feb  6 20:55:19 2009
@@ -0,0 +1,13 @@
+package org.apache.magma.website;
+
+import java.util.List;
+
+public interface CompoundableProducer {
+	
+	public CompoundableProducer compoundWith(Producer other, CompoundType type);
+	public List<Producer> findCompoundedOn(CompoundType type);
+	public List<Producer> findRecursivelyCompoundedOn(CompoundType type);
+	public void markDone(Producer other);
+	public void clearDone();
+	
+}

Added: 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=741714&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/CompoundableProducerImpl.aj
Fri Feb  6 20:55:19 2009
@@ -0,0 +1,121 @@
+package org.apache.magma.website;
+
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public aspect CompoundableProducerImpl {
+
+	public static class CompoundRelation {
+		public CompoundType type;
+		public Producer prod;
+		public boolean done = false;
+	}
+	
+	private List<CompoundRelation> CompoundableProducer.compound = null;
+	
+	
+	public CompoundableProducer CompoundableProducer.compoundWith(Producer p, CompoundType t)
{
+		CompoundRelation rel = new CompoundRelation();
+		rel.prod = p;
+		rel.type = t;
+		if (compound == null) compound = new ArrayList<CompoundRelation>();
+		compound.add(rel);
+		return this;
+	}
+
+	public void CompoundableProducer.markDone(Producer p) {
+		if (compound == null) return;
+		for (CompoundRelation rel : this.compound) {
+			if (rel.prod == p) {
+				rel.done = true;
+				break;
+			}
+		}		
+	}
+	
+	public void CompoundableProducer.clearDone() {
+		if (compound == null) return;
+		for (CompoundRelation rel : this.compound) {
+			rel.done = false;
+		}		
+	}
+	
+	
+	public List<Producer> CompoundableProducer.findCompoundedOn(CompoundType type) {
+		if (compound == null) return Collections.EMPTY_LIST;
+		List<Producer> ret = null;
+		for (CompoundRelation rel : this.compound) {
+			if (rel.type == type && !rel.done) {
+				if (ret == null) ret = new ArrayList<Producer>();
+				ret.add(rel.prod);
+			}
+		}
+		if (ret == null) return Collections.EMPTY_LIST;
+		return ret;
+	}
+
+	public List<Producer> CompoundableProducer.findRecursivelyCompoundedOn(CompoundType
type) {
+		if (compound == null) return Collections.EMPTY_LIST;
+		List<Producer> ret = null;
+		for (CompoundRelation rel : this.compound) {
+			if (rel.type == type && !rel.done) {
+				if (ret == null) ret = new ArrayList<Producer>();
+				ret.add(rel.prod);
+			}
+			{
+				if (rel.prod instanceof CompoundableProducer) {
+					List<Producer> subs = ((CompoundableProducer)rel.prod).findRecursivelyCompoundedOn(type);
+					if (subs.size() > 0) {
+						if (ret == null) ret = new ArrayList<Producer>();
+						ret.addAll(subs);
+					}
+				}
+			}
+		}
+		if (ret == null) return Collections.EMPTY_LIST;
+		return ret;
+	}
+	
+	void around(CompoundableProducer prod, OutputStream stream) : execution(void CompoundableProducer+.produce(OutputStream))
&& args(stream) && this(prod) {
+		if (prod.compound == null) {
+			proceed(prod, stream);
+			return;
+		}
+		List<Producer> list = prod.findCompoundedOn(CompoundType.HEAD);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}
+		list = prod.findCompoundedOn(CompoundType.BEFORE);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}		
+		proceed(prod, stream);
+		list = prod.findCompoundedOn(CompoundType.AFTER);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}		
+		list = prod.findCompoundedOn(CompoundType.SIDEA);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}		
+		list = prod.findCompoundedOn(CompoundType.SIDEB);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}		
+		list = prod.findCompoundedOn(CompoundType.SIDEC);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}		
+		list = prod.findCompoundedOn(CompoundType.SIDED);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}		
+		list = prod.findCompoundedOn(CompoundType.FOOT);
+		for (Producer producer : list) {
+			producer.produce(stream);
+		}		
+	}
+	
+}

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java?rev=741714&r1=741713&r2=741714&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
Fri Feb  6 20:55:19 2009
@@ -21,7 +21,7 @@
 import java.io.OutputStream;
 
 
-public abstract class HtmlProducer extends Producer implements Templatable {
+public abstract class HtmlProducer extends Producer implements Templatable, CompoundableProducer
{
 
 	protected boolean repeatable = true;
 	protected Template template;

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=741714&r1=741713&r2=741714&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 Feb  6 20:55:19 2009
@@ -17,6 +17,7 @@
 package org.apache.magma.website.templating;
 
 import org.apache.magma.basics.MagmaException;
+import org.apache.magma.website.CompoundType;
 import org.apache.magma.website.Head;
 import org.apache.magma.website.HtmlProducer;
 import org.apache.magma.website.RootWebHandler;
@@ -43,13 +44,25 @@
 	protected boolean forceMain = false;
 	
 	private String currentZone = null;
+	private Map<String, List<ZonePlaceholder>> holders = new HashMap<String,
List<ZonePlaceholder>>();
 	private Map<String, List<HtmlProducer>> zones = new HashMap<String, List<HtmlProducer>>();
 	private TemplateData data;
 	
+	private boolean givenWasMain = true;
+	
 	protected void always(HtmlProducer producer) {
 		addInZone(producer);
 	}
 	
+	protected void mainCompound(CompoundType type) {
+		List<ZonePlaceholder> list = holders.get(currentZone);
+		if (list == null) {
+			list = new ArrayList<ZonePlaceholder>();
+			holders.put(currentZone, list);
+		}
+		list.add(new ZonePlaceholderCompoundSide(type));		
+	}
+	
 	protected void ifNeeded(Class<? extends WebHandler> clazz) {
 		if (current != null && current.isFrom(clazz)) {
 			if (forceMain && previous == null) {
@@ -77,12 +90,12 @@
 	}
 	
 	private void addInZone(HtmlProducer producer) {
-		List<HtmlProducer> list = zones.get(currentZone);
+		List<ZonePlaceholder> list = holders.get(currentZone);
 		if (list == null) {
-			list = new ArrayList<HtmlProducer>();
-			zones.put(currentZone, list);
+			list = new ArrayList<ZonePlaceholder>();
+			holders.put(currentZone, list);
 		}
-		list.add(producer);
+		list.add(new ZonePlaceholderForHtmlProducer(producer));
 	}
 
 
@@ -91,13 +104,12 @@
 	}
 	
 	public void layoutMain() {
+		if (current == null) {
+			givenWasMain = false;
+			current = previous.getPrevious();
+		}
 		if (current != null) {
 			addInZone(current);
-		} else {
-			HtmlProducer prev = previous.getPrevious();
-			if (prev != null) {
-				addInZone(prev);
-			}
 		}
 	}
 	
@@ -131,7 +143,17 @@
 		}
 		
 		currentZone = "Main";
-		layoutMain();		
+		layoutMain();
+		
+		for (String acZone : data.zones) {
+			List<ZonePlaceholder> list = holders.get(acZone);
+			if (list == null) continue;
+			List<HtmlProducer> producers = new ArrayList<HtmlProducer>();
+			zones.put(acZone, producers);
+			for (ZonePlaceholder zph : list) {
+				zph.resolve(producers, current);
+			}
+		}
 	}
 	
 	public void produce(String baseurl, OutputStream stream) {
@@ -205,7 +227,7 @@
 	}
 
 	public boolean wasMain() {
-		return current != null;
+		return givenWasMain;
 	}
 
 	public void setForceMain(boolean forceMain) {

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholder.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholder.java?rev=741714&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholder.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholder.java
Fri Feb  6 20:55:19 2009
@@ -0,0 +1,11 @@
+package org.apache.magma.website.templating;
+
+import java.util.List;
+
+import org.apache.magma.website.HtmlProducer;
+
+public abstract class ZonePlaceholder {
+
+	public abstract void resolve(List<HtmlProducer> producers, HtmlProducer main);
+	
+}

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderCompoundSide.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderCompoundSide.java?rev=741714&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderCompoundSide.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderCompoundSide.java
Fri Feb  6 20:55:19 2009
@@ -0,0 +1,38 @@
+package org.apache.magma.website.templating;
+
+import java.util.List;
+
+import org.apache.magma.website.CompoundType;
+import org.apache.magma.website.CompoundableProducer;
+import org.apache.magma.website.HtmlProducer;
+import org.apache.magma.website.Producer;
+
+public class ZonePlaceholderCompoundSide extends ZonePlaceholder {
+
+	private CompoundType side;
+
+	public ZonePlaceholderCompoundSide(CompoundType side) {
+		this.side = side;
+	}
+
+	public CompoundType getSide() {
+		return side;
+	}
+
+	@Override
+	public void resolve(List<HtmlProducer> producers, HtmlProducer main) {
+		if (!(main instanceof CompoundableProducer)) return;
+		CompoundableProducer cp = (CompoundableProducer)main;
+		List<Producer> list = cp.findRecursivelyCompoundedOn(side);
+		if (list.size() == 0) return;
+		for (Producer producer : list) {
+			if (producer instanceof HtmlProducer) {
+				cp.markDone(producer);
+				producers.add((HtmlProducer) producer);
+			}
+		}
+	}
+	
+	
+	
+}

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderForHtmlProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderForHtmlProducer.java?rev=741714&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderForHtmlProducer.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/ZonePlaceholderForHtmlProducer.java
Fri Feb  6 20:55:19 2009
@@ -0,0 +1,24 @@
+package org.apache.magma.website.templating;
+
+import java.util.List;
+
+import org.apache.magma.website.HtmlProducer;
+
+public class ZonePlaceholderForHtmlProducer extends ZonePlaceholder {
+
+	private HtmlProducer producer;
+
+	public ZonePlaceholderForHtmlProducer(HtmlProducer producer) {
+		this.producer = producer;
+	}
+
+	public HtmlProducer getProducer() {
+		return producer;
+	}
+
+	@Override
+	public void resolve(List<HtmlProducer> producers, HtmlProducer main) {
+		producers.add(producer);
+	}
+	
+}

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java?rev=741714&r1=741713&r2=741714&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
Fri Feb  6 20:55:19 2009
@@ -262,7 +262,7 @@
 	}	
 
 	@Test
-	public void simpleTemplateClean() throws Exception {
+	public void aspectInject() throws Exception {
 		DefaultExpectations def = new DefaultExpectations();
 		UnitTestDispatch disp = new UnitTestDispatch();
 		disp.setTemplate(new SimpleTemplate());
@@ -274,6 +274,17 @@
 	}
 
 
+	@Test
+	public void compounding() throws Exception {
+		DefaultExpectations def = new DefaultExpectations() {{
+			one(session).setAttribute("user", "Massimo");
+			one(session).getAttribute("user"); will(returnValue("Massimo"));						
+		}};
+		def.setupMinimal("/testSession/compound");
+		Dispatch disp = def.start();		
+		assertStructure(def.out.toString(), "comp-head", "comp-bef", "comp-content", "comp-aft",
"comp-sidea", "comp-foot");
+	}	
+
 	
 	
 }

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=741714&r1=741713&r2=741714&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 Feb  6 20:55:19 2009
@@ -121,4 +121,20 @@
 		
 		assertStructure(def.out.toString(), "<div id=\"zoneMain", "ConvertedSessionHandler.doSetUser]",
"<div id=\"zoneSide", "AuxiliaryWebHandler.doAuxSearch]", "AuxiliaryWebHandler.doAuxPollResponse]");
 	}
+	
+	@Test
+	public void simpleTemplateWithSideCompound() throws Exception {
+		DefaultExpectations def = new DefaultExpectations() {{
+			one(session).setAttribute("user", "Massimo");
+			one(session).getAttribute("user"); will(returnValue("Massimo"));						
+		}};
+		UnitTestDispatch disp = new UnitTestDispatch();
+		disp.setTemplate(new SimpleTemplate());
+		def.disp = disp;
+		def.setupMinimal("/testSession/compound");
+		def.start();
+		
+		assertStructure(def.out.toString(), "<div id=\"zoneMain", "ConvertedSessionHandler.doCompound]",
"<div id=\"zoneSide", "AuxiliaryWebHandler.doAuxSearch]", "AuxiliaryWebHandler.doAuxPoll]",
".doCompound]", "comp-sidea");
+	}
+	
 }

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/ConvertedSessionHandler.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/ConvertedSessionHandler.java?rev=741714&r1=741713&r2=741714&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/ConvertedSessionHandler.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/ConvertedSessionHandler.java
Fri Feb  6 20:55:19 2009
@@ -1,5 +1,6 @@
 package org.apache.magma.website.teststuff;
 
+import org.apache.magma.website.CompoundType;
 import org.apache.magma.website.HtmlProducer;
 import org.apache.magma.website.WebHandler;
 
@@ -25,4 +26,14 @@
 		return new FakeHtmlProducer();
 	}
 	
+	public HtmlProducer doCompound() {
+		HtmlProducer ret = new FakeHtmlProducer("comp-content");
+		ret.compoundWith(new FakeHtmlProducer("comp-head"), CompoundType.HEAD);
+		ret.compoundWith(new FakeHtmlProducer("comp-bef"), CompoundType.BEFORE);
+		ret.compoundWith(new FakeHtmlProducer("comp-aft"), CompoundType.AFTER);
+		ret.compoundWith(new FakeHtmlProducer("comp-sidea"), CompoundType.SIDEA);
+		ret.compoundWith(new FakeHtmlProducer("comp-foot"), CompoundType.FOOT);
+		return ret;
+	}
+	
 }

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java?rev=741714&r1=741713&r2=741714&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java
Fri Feb  6 20:55:19 2009
@@ -52,6 +52,9 @@
 			str += "]";
 			I18nRenderingHelper i18nh = (I18nRenderingHelper) getParameters().getParameters().get("i18n");
 			str += i18nh.tr("test message");
+			if (params != null && params.length > 0) {
+				str += "(" + params[0] + ")";
+			}
 			stream.write(str.getBytes());
 		} catch (IOException e) {
 			throw new MagmaException(e, "Error writing output");

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleTemplate.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleTemplate.java?rev=741714&r1=741713&r2=741714&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleTemplate.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleTemplate.java
Fri Feb  6 20:55:19 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.magma.website.teststuff;
 
+import org.apache.magma.website.CompoundType;
 import org.apache.magma.website.templating.Template;
 
 public class SimpleTemplate extends Template {
@@ -23,6 +24,7 @@
 	public void layoutSide() {
 		always(root().handleAux().doAuxSearch());
 		byDefault(AuxiliaryWebHandler.class, root().handleAux().doAuxPoll());
+		mainCompound(CompoundType.SIDEA);
 	}
 	
 }



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


Mime
View raw message