From 7060b5ff7692fd5b68fd27580b90fb933db8fb0d Mon Sep 17 00:00:00 2001 From: "Eric J. Bowersox" Date: Mon, 26 Jul 2004 07:00:26 +0000 Subject: [PATCH] revamped the post box tag to use a Velocity template to render with --- .../venice/ui/conf/jsp/PostBoxTag.java | 156 ++++++------------ .../venice/ui/velocity/ParamMapWrapper.java | 70 ++++++++ .../venice/ui/velocity/StdObject.java | 42 +++++ templates/VM_global_library.vm | 8 + templates/conf/post_box.vm | 65 ++++++++ 5 files changed, 238 insertions(+), 103 deletions(-) create mode 100644 src/com/silverwrist/venice/ui/velocity/ParamMapWrapper.java create mode 100644 templates/conf/post_box.vm diff --git a/src/com/silverwrist/venice/ui/conf/jsp/PostBoxTag.java b/src/com/silverwrist/venice/ui/conf/jsp/PostBoxTag.java index 23f3b8a..e339072 100644 --- a/src/com/silverwrist/venice/ui/conf/jsp/PostBoxTag.java +++ b/src/com/silverwrist/venice/ui/conf/jsp/PostBoxTag.java @@ -19,11 +19,13 @@ package com.silverwrist.venice.ui.conf.jsp; import java.io.IOException; import java.util.*; +import javax.servlet.ServletException; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import org.apache.log4j.*; import com.silverwrist.venice.ui.*; import com.silverwrist.venice.ui.helpers.HTMLRendering; +import com.silverwrist.venice.ui.velocity.*; public class PostBoxTag extends VeniceConfBodyTagSupport { @@ -41,17 +43,9 @@ public class PostBoxTag extends VeniceConfBodyTagSupport private String m_action = null; private String m_type = "servlet"; - private int m_real_type = -1; - private HashMap m_params = new HashMap(); - private String m_newtopic_name = null; - private String m_newtopic_value = null; - private String m_pseud_name = null; - private String m_pseud_value = null; - private String m_attach_name = null; - private boolean m_attach_value = false; - private String m_text_name = null; - private String m_text_value = null; - private ArrayList m_buttons = new ArrayList(); + private HashMap m_params = null; + private ArrayList m_buttons = null; + private HashMap m_render_data = null; /*-------------------------------------------------------------------------------- * Overrides from class BodyTagSupport @@ -65,9 +59,15 @@ public class PostBoxTag extends VeniceConfBodyTagSupport if (m_action==null) throw new JspTagException(" action= attribute not specified!"); HTMLRendering html = (HTMLRendering)(getRequestInput().queryService(HTMLRendering.class)); - m_real_type = html.convertLinkType(m_type); - if (m_real_type<0) + if (html.convertLinkType(m_type)<0) throw new JspTagException(" type= attribute not a valid link type"); + m_render_data = new HashMap(); + m_params = new HashMap(); + m_buttons = new ArrayList(); + m_render_data.put("action",m_action); + m_render_data.put("action_type",m_type); + m_render_data.put("params",m_params); + m_render_data.put("buttons",m_buttons); return EVAL_BODY_BUFFERED; } // end doStartTag @@ -84,97 +84,37 @@ public class PostBoxTag extends VeniceConfBodyTagSupport { if (logger.isDebugEnabled()) logger.debug("PostBoxTag.doEndTag(): entry"); - if (m_pseud_name==null) + if (!(m_render_data.containsKey("pseud_name"))) throw new JspTagException(" tag has no tag inside it!"); - if (m_attach_name==null) + if (!(m_render_data.containsKey("attach_name"))) throw new JspTagException(" tag has no tag inside it!"); - if (m_text_name==null) + if (!(m_render_data.containsKey("text_name"))) throw new JspTagException(" tag has no tag inside it!"); if (m_buttons.isEmpty()) throw new JspTagException(" tag has no tags inside it!"); - HTMLRendering html = (HTMLRendering)(getRequestOutput().queryService(HTMLRendering.class)); - try - { // write out what we came here to accomplish + { // render this as a template JspWriter out = pageContext.getOut(); - out.write("
\n"); - Iterator it = m_params.entrySet().iterator(); - while (it.hasNext()) - { // write the hidden parameters - Map.Entry ntry = (Map.Entry)(it.next()); - out.write("\n"); - - } // end while - - String my_font = html.getFontTag(html.CONTENT_FOREGROUND,"content"); - out.write("\n"); - if (m_newtopic_name!=null) - { // write the "new topic name" field - out.write("\n"); - - } // end if - - out.write("\n\n\n\n\n" - + "\n\n
\n" - + my_font + "New topic name:
\n" - + "\n
\n" - + my_font + "Your name/header:
\n" - + "\n" + my_font + " Attach a file\n
\n" - + my_font + "Message:\n" + my_font - + "\nHTML Guide\n
\n\n
\n"); - boolean first = true; - it = m_buttons.iterator(); - while (it.hasNext()) - { // write out all the buttons - String id = (String)(it.next()); - if (first) - first = false; - else - out.write(" "); - out.write(html.getButtonInput(id) + "\n"); - - } // end while - - out.write("
\n
\n"); + VelocityRenderer vr = VelocityRenderer.get(pageContext.getServletContext()); + vr.formatTemplate("conf/post_box.vm",new ParamMapWrapper(m_render_data),getRequestOutput(),out); } // end try catch (IOException e) { // convert the I/O error into something the servlet engine can deal with throw new JspTagException("error writing post box - " + e.getMessage()); + } // end catch + catch (ServletException e) + { // convert the I/O error into something the servlet engine can deal with + throw new JspTagException("error writing post box - " + e.getMessage()); + } // end catch // anything set by subtags must be cleared in doEndTag, not release - m_params.clear(); - m_newtopic_name = null; - m_newtopic_value = null; - m_pseud_name = null; - m_pseud_value = null; - m_attach_name = null; - m_attach_value = false; - m_text_name = null; - m_text_value = null; - m_buttons.clear(); + m_render_data = null; + m_params = null; + m_buttons = null; return EVAL_PAGE; } // end doEndTag @@ -186,7 +126,6 @@ public class PostBoxTag extends VeniceConfBodyTagSupport super.release(); m_action = null; m_type = "servlet"; - m_real_type = -1; } // end release @@ -204,7 +143,6 @@ public class PostBoxTag extends VeniceConfBodyTagSupport public void setType(String v) { m_type = v; - m_real_type = -1; } // end setType @@ -213,8 +151,10 @@ public class PostBoxTag extends VeniceConfBodyTagSupport *-------------------------------------------------------------------------------- */ - final void addParam(String name, String value) + final void addParam(String name, String value) throws JspException { + if (m_params==null) + throw new JspTagException("addParam() called at wrong time!"); if (logger.isDebugEnabled()) logger.debug("PostBoxTag.addParam(\"" + name + "\",\"" + value + "\") entry"); m_params.put(name,value); @@ -223,54 +163,64 @@ public class PostBoxTag extends VeniceConfBodyTagSupport final void setNewTopic(String param_name, String value) throws JspException { - if (m_newtopic_name!=null) + if (m_render_data==null) + throw new JspTagException("setNewTopic() called at wrong time!"); + if (m_render_data.containsKey("newtopic_name")) throw new JspTagException(" may only appear once inside a !"); if (param_name==null) throw new JspTagException(" parameter name not specified!"); - m_newtopic_name = param_name; - m_newtopic_value = value; + m_render_data.put("newtopic_name",param_name); + m_render_data.put("newtopic_value",value); } // end setNewTopic final void setPseud(String param_name, String value) throws JspException { + if (m_render_data==null) + throw new JspTagException("setPseud() called at wrong time!"); if (logger.isDebugEnabled()) logger.debug("PostBoxTag.setPseud(\"" + param_name + "\",\"" + value + "\") entry"); - if (m_pseud_name!=null) + if (m_render_data.containsKey("pseud_name")) throw new JspTagException(" may only appear once inside a !"); if (param_name==null) throw new JspTagException(" parameter name not specified!"); - m_pseud_name = param_name; - m_pseud_value = value; + m_render_data.put("pseud_name",param_name); + m_render_data.put("pseud_value",value); } // end setPseud final void setAttach(String param_name, boolean value) throws JspException { - if (m_attach_name!=null) + if (m_render_data==null) + throw new JspTagException("setAttach() called at wrong time!"); + if (m_render_data.containsKey("attach_name")) throw new JspTagException(" may only appear once inside a !"); if (param_name==null) throw new JspTagException(" parameter name not specified!"); - m_attach_name = param_name; - m_attach_value = value; + m_render_data.put("attach_name",param_name); + m_render_data.put("attach_value",(value ? Boolean.TRUE : Boolean.FALSE)); } // end setAttach final void setText(String param_name, String value) throws JspException { + if (m_render_data==null) + throw new JspTagException("setText() called at wrong time!"); if (logger.isDebugEnabled()) logger.debug("PostBoxTag.setText(\"" + param_name + "\",) entry"); - if (m_text_name!=null) + if (m_render_data.containsKey("text_name")) throw new JspTagException(" may only appear once inside a !"); if (param_name==null) throw new JspTagException(" parameter name not specified!"); - m_text_name = param_name; - m_text_value = value; + m_render_data.put("text_name",param_name); + m_render_data.put("text_value",value); } // end setText - final void addButton(String id) + final void addButton(String id) throws JspException { + if (m_buttons==null) + throw new JspTagException("addButton() called at wrong time!"); m_buttons.add(id); } // end addButton diff --git a/src/com/silverwrist/venice/ui/velocity/ParamMapWrapper.java b/src/com/silverwrist/venice/ui/velocity/ParamMapWrapper.java new file mode 100644 index 0000000..8497455 --- /dev/null +++ b/src/com/silverwrist/venice/ui/velocity/ParamMapWrapper.java @@ -0,0 +1,70 @@ +/* + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at . + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.venice.ui.velocity; + +import java.util.*; + +public final class ParamMapWrapper implements VelocitySupplyParams +{ + /*-------------------------------------------------------------------------------- + * Attributes + *-------------------------------------------------------------------------------- + */ + + private Map m_map; + + /*-------------------------------------------------------------------------------- + * Constructor + *-------------------------------------------------------------------------------- + */ + + public ParamMapWrapper(Map map) + { + m_map = map; + + } // end constructor + + /*-------------------------------------------------------------------------------- + * Implementations from interface VelocitySupplyParams + *-------------------------------------------------------------------------------- + */ + + /** + * Returns the value of a parameter set on the object. + * + * @param key The name of the parameter to look up. + * @return The parameter's value, or null if the parameter was not set. + */ + public Object getParameter(String key) + { + return m_map.get(key); + + } // end getParameter + + /** + * Returns a java.util.Collection of all parameter names currently defined on this object. + * + * @return A collection of all parameter names currently defined. + */ + public Collection getParameterNames() + { + return m_map.keySet(); + + } // end getParameterNames + +} // end class ParamMapWrapper diff --git a/src/com/silverwrist/venice/ui/velocity/StdObject.java b/src/com/silverwrist/venice/ui/velocity/StdObject.java index 3ef72f8..2d122ce 100644 --- a/src/com/silverwrist/venice/ui/velocity/StdObject.java +++ b/src/com/silverwrist/venice/ui/velocity/StdObject.java @@ -54,4 +54,46 @@ public class StdObject } // end formatURL + public String fontTag(String color, String size) + { + if (m_html==null) + m_html = (HTMLRendering)(m_rout.queryService(HTMLRendering.class)); + return m_html.getFontTag(color.trim(),size.trim()); + + } // end fontTag + + public String externalStatic(String path) + { + if (m_html==null) + m_html = (HTMLRendering)(m_rout.queryService(HTMLRendering.class)); + return m_html.getExternalStaticPath(path); + + } // end externalStatic + + public String button(String id, String type) + { + if (m_html==null) + m_html = (HTMLRendering)(m_rout.queryService(HTMLRendering.class)); + id = id.trim().toLowerCase(); + type = type.trim().toLowerCase(); + if (type.equals("visual")) + return m_html.getButtonVisual(id); + else if (type.equals("input")) + return m_html.getButtonInput(id); + else + return ""; + + } // end button + + public String comment(String data) + { + if (m_html==null) + m_html = (HTMLRendering)(m_rout.queryService(HTMLRendering.class)); + if (m_html.useHTMLComments()) + return ""; + else + return ""; + + } // end comment + } // end class StdObject diff --git a/templates/VM_global_library.vm b/templates/VM_global_library.vm index e0db93b..9bcfaf0 100644 --- a/templates/VM_global_library.vm +++ b/templates/VM_global_library.vm @@ -18,3 +18,11 @@ ## Define macros around the "std" object. #macro( formatURL $type $url )$std.formatURL($type,$url)#end + +#macro( font $color $size )$std.fontTag($color,$size)#end + +#macro( externalStatic $p )$std.externalStatic($p)#end + +#macro( button $id $typ )$std.button($id,$typ)#end + +#macro( comment $d )$std.comment($d)#end diff --git a/templates/conf/post_box.vm b/templates/conf/post_box.vm new file mode 100644 index 0000000..27862ff --- /dev/null +++ b/templates/conf/post_box.vm @@ -0,0 +1,65 @@ +#* + The contents of this file are subject to the Mozilla Public License Version 1.1 + (the "License"); you may not use this file except in compliance with the License. + You may obtain a copy of the License at . + + Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + WARRANTY OF ANY KIND, either express or implied. See the License for the specific + language governing rights and limitations under the License. + + The Original Code is the Venice Web Communities System. + + The Initial Developer of the Original Code is Eric J. Bowersox , + for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + + Contributor(s): +*# +## Render the post box. +#set( $html_reference = "html-reference.html" ) +#comment( "BEGIN POST BOX" ) +
+ #foreach( $pn in $params.keySet() ) + + #end + + #if( $newtopic_name ) + + #end + + + + + + + +
+ #font( "content.fg" "content" )New topic name:
+ +
+ #font( "content.fg" "content" )Your name/header:
+ + #font( "content.fg" "content" ) Attach a file +
#font( "content.fg" "content" )Message:#font( "content.fg" "content" ) + HTML Guide +
+ +
+ #set( $first = 1 ) + #foreach( $bn in $buttons ) + #if( $first == 1 ) + #set( $first = 0 ) + #else +   + #end + #button( $bn "input" ) + #end +
+
+#comment( "END POST BOX" ) +## -- EOF --