Author: simoneg
Date: Wed Apr 21 14:24:43 2010
New Revision: 936320

URL: http://svn.apache.org/viewvc?rev=936320&view=rev
Log:
More control on Template activity

Modified:
    
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/Template.java

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=936320&r1=936319&r2=936320&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
 Wed Apr 21 14:24:43 2010
@@ -21,6 +21,7 @@ import org.apache.magma.basics.context.C
 import org.apache.magma.basics.context.RunningContext;
 import org.apache.magma.basics.context.SubRunningContext;
 import org.apache.magma.website.CompoundType;
+import org.apache.magma.website.CompoundableProducer;
 import org.apache.magma.website.Head;
 import org.apache.magma.website.HtmlProducer;
 import org.apache.magma.website.Producer;
@@ -100,22 +101,25 @@ public class Template {
         * are found getting those compounded on the main, on the given 
compound type.
         * 
         * @param type The compound type to search producers in.
+        * @return true if there was something in the compound side, false 
otherwise
         */
-       protected void mainCompound(CompoundType type) {
+       protected boolean 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));                
+               list.add(new ZonePlaceholderCompoundSide(type));
+               return current != null && current instanceof 
CompoundableProducer && 
((CompoundableProducer)current).findCompoundedOn(type).size() > 0;
        }
        
        /**
         * Places here whatever arrives from the given handler class, if any.
         * 
         * @param clazz The handler class.
+        * @return true if main was from given handler
         */
-       protected void ifNeeded(Class<? extends WebHandler> clazz) {
+       protected boolean ifNeeded(Class<? extends WebHandler> clazz) {
                if (current != null && current.isFrom(clazz)) {
                        if (forceMain && previous == null) {
                                // Skip the auxiliary, cause we have to 
consider it as a main
@@ -123,16 +127,19 @@ public class Template {
                        } else {
                                addInZone(current);
                                current = null;
+                               return true;
                        }
                }
+               return false;
        }
        
        /**
         * Places here whatever arrives from the given handler, if any.
         * 
         * @param handler The handler.
+        * @return true if main was from given handler
         */
-       protected void ifNeeded(WebHandler handler) {
+       protected boolean 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
@@ -140,8 +147,10 @@ public class Template {
                        } else {
                                addInZone(current);
                                current = null;
+                               return true;
                        }
                }
+               return false;
        }
        
        /**
@@ -150,8 +159,9 @@ public class Template {
         * 
         * @param clazz The class of the handler.
         * @param def The producer to use by default.
+        * @return true if provided alternative was used 
         */
-       protected void byDefault(Class<? extends WebHandler> clazz, 
HtmlProducer def) {
+       protected boolean byDefault(Class<? extends WebHandler> clazz, 
HtmlProducer def) {
                if (current != null && current.isFrom(clazz)) {
                        if (forceMain && previous == null) {
                                // Skip the auxiliary, cause we have to 
consider it as a main
@@ -162,7 +172,9 @@ public class Template {
                        }
                } else {
                        addInZone(def);
+                       return true;
                }
+               return false;
        }
        
        /**
@@ -171,8 +183,9 @@ public class Template {
         * 
         * @param handler The handler.
         * @param def The producer to use by default.
+        * @return true if provided alternative was used 
         */
-       protected void byDefault(WebHandler handler, HtmlProducer def) {
+       protected boolean 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
@@ -183,7 +196,9 @@ public class Template {
                        }
                } else {
                        addInZone(def);
+                       return true;
                }
+               return false;
        }
        
        /**
@@ -191,9 +206,10 @@ public class Template {
         * if any, otherwise uses the given producer.
         * 
         * @param def The producer to use by default.
+        * @return true if provided alternative was used 
         */
-       protected void byDefault(HtmlProducer def) {
-               byDefault(def.getCreatingHandler(), def);
+       protected boolean byDefault(HtmlProducer def) {
+               return byDefault(def.getCreatingHandler(), def);
        }
        
        
@@ -272,51 +288,67 @@ public class Template {
                        rewrite.setTemplatePrefix("template/" + 
getClass().getPackage().getName() + "/");
                        OutputStreamWriter osw = new 
OutputStreamWriter(rewrite, Charset.forName("UTF-8"));
                        for (int i = 0; i < data.getZoneCount(); i++) {
-                               rewrite.setBaseUrl(rewrite.getTemplatePrefix());
-                               osw.write(data.getRaw(i));
-                               osw.flush();
+                               sendRaw(i, rewrite);
                                String zone = data.getZoneName(i);
                                if (zone.equals("__head_content")) {
-                                       Head head = new Head(rewrite);
-                                       for (List<HtmlProducer> producers : 
zones.values()) {
-                                               for (HtmlProducer producer : 
producers) {
-                                                       if (producer != null) {
-                                                               
producer.head(head);
-                                                               rewrite.flush();
-                                                       }
-                                               }
-                                       }
-                                       head.finished();
+                                       sendAllHead(rewrite);
                                } else {
-                                       List<HtmlProducer> producers = 
zones.get(zone);
-                                       if (producers != null) {
-                                               for (HtmlProducer producer : 
producers) {
-                                                       if (producer != null) {
-                                                               if 
(!zone.equals("ExtraContent")) openWrapper(zone, producer, osw);
-                                                               osw.flush();
-                                                               
producer.produce(rewrite);
-                                                               rewrite.flush();
-                                                               if 
(!zone.equals("ExtraContent")) closeWrapper(zone, producer, osw);
-                                                               osw.flush();
-                                                       }
-                                               }
-                                       }
+                                       sendZone(zone, rewrite);
                                }
                        }
-                       rewrite.setBaseUrl(rewrite.getTemplatePrefix());
-                       osw.write(data.getRaw(data.getZoneCount()));
-                       osw.flush();
-                       rewrite.flush();
+                       sendRaw(data.getZoneCount(), rewrite);
                } catch (IOException e) {
                        throw new MagmaException(e, "Error sending output");
                }
        }
        
-       protected void closeWrapper(String zone, HtmlProducer producer, 
OutputStreamWriter osw) throws IOException {
+       public void sendZone(String zone, URLRewritingStream rewrite) throws 
IOException {
+               List<HtmlProducer> producers = zones.get(zone);
+               if (producers != null) {
+                       for (HtmlProducer producer : producers) {
+                               if (producer != null) {
+                                       sendProducer(zone, producer, rewrite);
+                               }
+                       }
+               }
+       }
+
+       public void sendProducer(String zone, HtmlProducer producer, 
URLRewritingStream rewrite) throws IOException {
+               if (!zone.equals("ExtraContent")) openWrapper(zone, producer, 
rewrite);
+               rewrite.flush();
+               producer.produce(rewrite);
+               if (!zone.equals("ExtraContent")) closeWrapper(zone, producer, 
rewrite);
+               rewrite.flush();
+       }
+
+       protected void sendAllHead(URLRewritingStream rewrite) throws 
IOException {
+               Head head = new Head(rewrite);
+               for (List<HtmlProducer> producers : zones.values()) {
+                       for (HtmlProducer producer : producers) {
+                               if (producer != null) {
+                                       sendHead(producer, head, rewrite);
+                               }
+                       }
+               }
+               head.finished();
+       }
+       
+       protected void sendHead(HtmlProducer producer, Head head, 
URLRewritingStream rewrite) throws IOException {
+               producer.head(head);
+               rewrite.flush();                
+       }
+
+       protected void sendRaw(int i, URLRewritingStream rewrite) throws 
IOException {
+               rewrite.setBaseUrl(rewrite.getTemplatePrefix());
+               rewrite.write(data.getRaw(i));
+               rewrite.flush();                
+       }
+       
+       protected void closeWrapper(String zone, HtmlProducer producer, 
URLRewritingStream osw) throws IOException {
                osw.write("</div></div>");
        }
 
-       protected void openWrapper(String zone, HtmlProducer producer, 
OutputStreamWriter osw) throws IOException {
+       protected void openWrapper(String zone, HtmlProducer producer, 
URLRewritingStream osw) throws IOException {
                osw.write("<div class=\"" + 
computeCompleteProducerClasses(producer) + " zonedElement\">");
                osw.write("<div class=\"zonedElementInner\">");
        }



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

Reply via email to