Update of /cvsroot/xdoclet/xdoclet/modules/apache/src/xdoclet/modules/apache/velocity
In directory sc8-pr-cvs1:/tmp/cvs-serv553/velocity

Modified Files:
        VelocityEngineTagHandler.java 
Log Message:
add BSF, refactoring common pieces with Velocity tag handler

Index: VelocityEngineTagHandler.java
===================================================================
RCS file: 
/cvsroot/xdoclet/xdoclet/modules/apache/src/xdoclet/modules/apache/velocity/VelocityEngineTagHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** VelocityEngineTagHandler.java       17 Jul 2003 23:13:55 -0000      1.1
--- VelocityEngineTagHandler.java       17 Jul 2003 23:49:10 -0000      1.2
***************
*** 9,12 ****
--- 9,13 ----
  
  import xdoclet.XDocletException;
+ import xdoclet.modules.apache.ScriptEngineTagHandler;
  import xdoclet.modules.apache.SubTemplateEngine;
  import xdoclet.tagshandler.AbstractProgramElementTagsHandler;
***************
*** 22,26 ****
   * @xdoclet.taghandler   namespace="Velocity"
   */
! public class VelocityEngineTagHandler extends AbstractProgramElementTagsHandler
  {
      /*
--- 23,27 ----
   * @xdoclet.taghandler   namespace="Velocity"
   */
! public class VelocityEngineTagHandler extends ScriptEngineTagHandler
  {
      /*
***************
*** 41,60 ****
       * </XDtTemplateEngines:generator>
       */
-     // XDOCLET subtemplate section start tag inside the other template
-     private final String XDTSectionStart = "<XDt>";
-     // subtemplate end tag
-     private final String XDTSectionEnd = "</XDt>";
- 
-     //// Velocity engine
- 
      private VelocitySubTemplateEngine velocityEngine;
  
-     private VelocitySubTemplateEngine getVelocityEngine() {
-         if (velocityEngine == null) {
-             velocityEngine = new VelocitySubTemplateEngine();
-         }
-         return velocityEngine;
-     }
- 
  
      /**
--- 42,47 ----
***************
*** 99,228 ****
      }
  
!     /**
!      * Get a subtemplate variable
!      *
!      * @param subengine          The subtemplate engine
!      * @param attributes         The attributes from XDOCLET tag
!      * @return                   The value
!      * @throws XDocletException
!      */
!     private String getSubTemplateVariable(SubTemplateEngine subengine, Properties 
attributes) throws XDocletException
      {
!         String vname = attributes.getProperty("name");
! 
!         if (vname == null) {
!             throw new XDocletException("Missing name property the name of the 
Velocity variable!");
!         }
! 
!         Object value = subengine.getVariable(vname);
! 
!         if (value == null) {
!             String defaultValue = attributes.getProperty("default", "");
! 
!             return defaultValue;
!         }
!         return value.toString();
!     }
! 
!     /// Common methods for all engines
! 
!     /**
!      * @param subengine           The SubTemplateEngine used to generate
!      * @param template            The body of the block tag
!      * @param attributes          The attributes of the template tag
!      * @throws TemplateException
!      */
!     private void generate(SubTemplateEngine subengine, String template, Properties 
attributes) throws TemplateException
!     {
!         if ("yes".equalsIgnoreCase(attributes.getProperty("disable"))) {
!             return;
!         }
! 
!         fillVariables(subengine);
! 
!         String result = subengine.generate(template, attributes);
!         Log log = LogUtil.getLog(VelocityEngineTagHandler.class, "generate");
! 
!         log.debug("Subengine generated results:" + result);
! 
!         StringBuffer output = new StringBuffer(result);
! 
!         // send back the XDOCLET engine the velocity output, but only when 
silent="true" is not set
!         if (!"yes".equalsIgnoreCase(attributes.getProperty("silent"))) {
!             TemplateEngine engine = getEngine();
! 
!             escapeResults(engine, output);
!             engine.print(output.toString());
!         }
!     }
! 
!     /**
!      * Fill the variables passed to the engines
!      *
!      * @param templateEngine
!      * @exception XDocletException
!      */
!     private void fillVariables(SubTemplateEngine templateEngine) throws 
XDocletException
!     {
!         Log log = LogUtil.getLog(VelocityEngineTagHandler.class, "fillVariables");
! 
!         log.debug("fillVariables() called");
!         try {
!             templateEngine.setVariable("tagHandler", this);
!             templateEngine.setVariable("currentPackage", getCurrentPackage());
!             templateEngine.setVariable("currentClass", getCurrentClass());
!             templateEngine.setVariable("currentMethod", getCurrentMethod());
!             templateEngine.setVariable("currentConstructor", 
getCurrentConstructor());
!             templateEngine.setVariable("currentField", getCurrentField());
!             templateEngine.setVariable("currentClassTag", getCurrentClassTag());
!             templateEngine.setVariable("currentFieldTag", getCurrentFieldTag());
!             templateEngine.setVariable("currentMethodTag", getCurrentMethodTag());
!         }
!         catch (Exception ex) {
!             log.error("Exception when setting variables", ex);
!             throw new XDocletException(ex, "Exception when setting variables");
!         }
!     }
! 
!     /**
!      * Escape and evaluate the <XDt></XDt> sections with XDOCLET template engine. 
This allows embedding XDOCLET sections
!      * into Velocity sections
!      *
!      * @param engine                 The XDOCLET template engine
!      * @param results                The results
!      * @exception TemplateException
!      */
!     private void escapeResults(TemplateEngine engine, StringBuffer results) throws 
TemplateException
!     {
!         Log log = LogUtil.getLog(VelocityEngineTagHandler.class, "escapeResults");
! 
!         // will return when no more subtemplate found
!         while (true) {
!             // find the <XDt> sections
!             int startidx = results.indexOf(XDTSectionStart);
!             int endidx = results.indexOf(XDTSectionEnd);
! 
!             boolean templatefound = (startidx >= 0 && endidx >= 0);
! 
!             if (!templatefound)
!                 return;
! 
!             // extract the content of the <XDt> section
!             String subXDtTemplate = results.substring(startidx + 
XDTSectionStart.length(), endidx);
! 
!             log.debug("subTemplate found:<" + subXDtTemplate + ">");
! 
!             // remove the <XDt> section
!             results.delete(startidx, endidx + XDTSectionEnd.length());
!             log.debug("Results after XDt section removed:<" + results.toString() + 
">");
! 
!             // generate the subtemplate by calling the XDOCLET engine
!             String subTemplateOutput = engine.outputOf(subXDtTemplate);
! 
!             log.debug("Generated subTempalte output:" + subTemplateOutput);
! 
!             // write back the result
!             results.insert(startidx, subTemplateOutput);
          }
      }
  }
--- 86,95 ----
      }
  
!     private VelocitySubTemplateEngine getVelocityEngine()
      {
!         if (velocityEngine == null) {
!             velocityEngine = new VelocitySubTemplateEngine();
          }
+         return velocityEngine;
      }
  }



-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to