Author: simoneg
Date: Tue Dec 22 14:35:27 2009
New Revision: 893188
URL: http://svn.apache.org/viewvc?rev=893188&view=rev
Log:
Better support for dynamic templating
Modified:
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.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/WebContextTest.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/Dispatch.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java?rev=893188&r1=893187&r2=893188&view=diff
==============================================================================
---
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
(original)
+++
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
Tue Dec 22 14:35:27 2009
@@ -218,9 +218,12 @@
// Try to determine if current request is considered a main or
not
HttpSession session = req.getSession();
+ // Will set this to true when the right template has been found
boolean validTemplate = false;
+ // Check if there is a template on the returned producer
Template t = producer.getTemplate();
if (t == null) {
+ // If there is not, try using the previous template
Class<Template> tplcl = (Class<Template>)
session.getAttribute("__magma_lasttemplate");
if (tplcl != null) {
try {
@@ -231,6 +234,7 @@
throw new MagmaException(e, "Security
is not correctly configured, cannot create an instance of {0}", tplcl);
}
}
+ // Finally, try the default template
if (t == null) {
t = getDefaultTemplate();
validTemplate = true;
@@ -240,6 +244,7 @@
}
t.setCurrent(producer);
+ // Prepare a previous giver, in case the template needs the
previous main page
final String last = (String)
session.getAttribute("__magma_lastmain");
PreviousGiver giver = new PreviousGiver() {
public HtmlProducer getPrevious() {
@@ -254,9 +259,13 @@
return null;
}
};
- t.setPrevious(giver);
+ t.setPrevious(giver);
+ // Launch the template parsing
t.parse();
+ // Unless we consider the template already valid, and the
template does not
+ // conside our main producer as a main, try if the default
template
+ // offer a metter match.
if (!validTemplate && t.wasMain()) {
Template defaultTemplate = getDefaultTemplate();
if (!defaultTemplate.getClass().equals(t.getClass())) {
@@ -267,7 +276,7 @@
t.parse();
}
}
-
+ // Finally produce the output with the right template
t.produce(Cycle.get().getRewriting());
if (t.wasMain() && producer.isRepeatable()) {
Modified:
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java?rev=893188&r1=893187&r2=893188&view=diff
==============================================================================
---
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
(original)
+++
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
Tue Dec 22 14:35:27 2009
@@ -67,6 +67,11 @@
return false;
}
+ public boolean isFrom(WebHandler other) {
+ // TODO is there a better way than strings to do this?
+ return
(this.getCompletePath().startsWith(other.getRelativePath()));
+ }
+
public boolean isFrom(Producer other) {
return isFrom(other.getCreatingHandler().getClass()) &&
other.getCreatingMethod().equals(this.getCreatingMethod());
}
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=893188&r1=893187&r2=893188&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
Tue Dec 22 14:35:27 2009
@@ -41,24 +41,66 @@
import java.util.List;
import java.util.Map;
+/**
+ * Base class implementing the template system.
+ *
+ * @author Simone Gianni <[email protected]>
+ */
public class Template {
+ /**
+ * The HTML file this template is going to read structure from.
+ */
protected String fileName = "template.html";
+
+
protected HtmlProducer current = null;
protected PreviousGiver previous = null;
protected boolean forceMain = false;
+ /**
+ * The zone we are sending to output
+ */
private String currentZone = null;
+
+ /**
+ * Holders, having the zone as the key, and a list of {...@link
ZonePlaceholder}s as value
+ */
private Map<String, List<ZonePlaceholder>> holders = new
HashMap<String, List<ZonePlaceholder>>();
+ /**
+ * "dereferenced" holders, having zone as the key, and a list of
{...@link HtmlProducer}s as value.
+ */
private Map<String, List<HtmlProducer>> zones = new HashMap<String,
List<HtmlProducer>>();
+ /**
+ * Parsed template data.
+ */
private TemplateData data;
private boolean givenWasMain = true;
+ /**
+ * Always adds the given producer in this position.
+ *
+ * This method can be called from inside one of the layout methods.
+ *
+ * The given producer will be rendered in the zone no matter what.
+ *
+ * @param producer The producer to render in the zone.
+ */
protected void always(HtmlProducer producer) {
addInZone(producer);
}
+ /**
+ * Places the producers compounded with main in this position.
+ *
+ * This method can be called from inside one of the layout methods.
+ *
+ * The semantic is the same as {...@link #always(HtmlProducer)}, but
the producers
+ * are found getting those compounded on the main, on the given
compound type.
+ *
+ * @param type The compound type to search producers in.
+ */
protected void mainCompound(CompoundType type) {
List<ZonePlaceholder> list = holders.get(currentZone);
if (list == null) {
@@ -68,6 +110,11 @@
list.add(new ZonePlaceholderCompoundSide(type));
}
+ /**
+ * Places here whatever arrives from the given handler class, if any.
+ *
+ * @param clazz The handler class.
+ */
protected void ifNeeded(Class<? extends WebHandler> clazz) {
if (current != null && current.isFrom(clazz)) {
if (forceMain && previous == null) {
@@ -80,6 +127,30 @@
}
}
+ /**
+ * Places here whatever arrives from the given handler, if any.
+ *
+ * @param handler The handler.
+ */
+ protected void ifNeeded(WebHandler handler) {
+ if (current != null && current.isFrom(handler)) {
+ if (forceMain && previous == null) {
+ // Skip the auxiliary, cause we have to
consider it as a main
+ // TODO log this? it could be a problem.
+ } else {
+ addInZone(current);
+ current = null;
+ }
+ }
+ }
+
+ /**
+ * Places here whatever arrives from the given handler class, if any,
otherwise uses the
+ * given producer.
+ *
+ * @param clazz The class of the handler.
+ * @param def The producer to use by default.
+ */
protected void byDefault(Class<? extends WebHandler> clazz,
HtmlProducer def) {
if (current != null && current.isFrom(clazz)) {
if (forceMain && previous == null) {
@@ -94,6 +165,39 @@
}
}
+ /**
+ * Places here whatever arrives from the given handler, if any,
otherwise uses the
+ * given producer.
+ *
+ * @param handler The handler.
+ * @param def The producer to use by default.
+ */
+ protected void byDefault(WebHandler handler, HtmlProducer def) {
+ if (current != null && current.isFrom(handler)) {
+ if (forceMain && previous == null) {
+ // Skip the auxiliary, cause we have to
consider it as a main
+ // TODO log this? it could be a problem.
+ } else {
+ addInZone(current);
+ current = null;
+ }
+ } else {
+ addInZone(def);
+ }
+ }
+
+ /**
+ * Places here whatever arrives from the handler that created the given
producer,
+ * if any, otherwise uses the given producer.
+ *
+ * @param def The producer to use by default.
+ */
+ protected void byDefault(HtmlProducer def) {
+ byDefault(def.getCreatingHandler(), def);
+ }
+
+
+
private void addInZone(HtmlProducer producer) {
List<ZonePlaceholder> list = holders.get(currentZone);
if (list == null) {
Modified:
labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java?rev=893188&r1=893187&r2=893188&view=diff
==============================================================================
---
labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java
(original)
+++
labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java
Tue Dec 22 14:35:27 2009
@@ -107,7 +107,11 @@
assertThat(prod.getCompletePath(),
equalTo("/test/resend2/normal"));
assertThat(prod.getCreatingHandler(), sameInstance(lev2));
assertThat(prod.getCreatingMethod().getName(),
equalTo("doNormal"));
-
+
+ assertTrue(prod.isFrom(RecallingWebHandler.class));
+ assertTrue(prod.isFrom(lev2));
+ assertTrue(prod.isFrom(lev1));
+ assertTrue(prod.isFrom(prod));
}
}
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=893188&r1=893187&r2=893188&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
Tue Dec 22 14:35:27 2009
@@ -102,6 +102,11 @@
assertStructure(def.out.toString(), "<div id=\"zoneMain",
"ConvertedSessionHandler.doSetUser]", "<div id=\"zoneSide",
"AuxiliaryWebHandler.doAuxSearch]", "AuxiliaryWebHandler.doAuxPoll]");
}
+ /*
+ * USE CASE :
+ * - calling an auxiliary method, the previous template should be used
+ * (the auxiliary method is in this case handled by
Template.byDefault(Class, HtmlProducer) )
+ */
@Test
public void reusingTemplate() throws Exception {
DefaultExpectations def = new DefaultExpectations() {{
@@ -119,6 +124,11 @@
}
+ /*
+ * USE CASE :
+ * - calling an auxiliary method, the previous main should be used
+ * (the auxiliary method is in this case handled by
Template.byDefault(Class, HtmlProducer) )
+ */
@Test
public void simpleTemplateWithReplacedAuxiliary() throws Exception {
DefaultExpectations def = new DefaultExpectations() {{
@@ -134,6 +144,10 @@
assertStructure(def.out.toString(), "<div id=\"zoneMain",
"ConvertedSessionHandler.doSetUser]", "<div id=\"zoneSide",
"AuxiliaryWebHandler.doAuxSearch]", "AuxiliaryWebHandler.doAuxPollResponse]");
}
+ /*
+ * USE CASE :
+ * - compounded producers in a zone
+ */
@Test
public void simpleTemplateWithSideCompound() throws Exception {
DefaultExpectations def = new DefaultExpectations() {{
@@ -147,4 +161,6 @@
assertStructure(def.out.toString(), "<div id=\"zoneMain",
"ConvertedSessionHandler.doCompound]", "<div id=\"zoneSide",
"AuxiliaryWebHandler.doAuxSearch]", "AuxiliaryWebHandler.doAuxPoll]",
".doCompound]", "comp-sidea");
}
+
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]