jvanzyl     01/07/09 17:38:27

  Added:       src/adapter/org/apache/turbine/util/webmacro
                        WebMacroEmail.java WebMacroFormatter.java
                        WebMacroHtmlEmail.java
  Log:
  - adapter code
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine/src/adapter/org/apache/turbine/util/webmacro/WebMacroEmail.java
  
  Index: WebMacroEmail.java
  ===================================================================
  package org.apache.turbine.util.webmacro;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.turbine.services.webmacro.TurbineWebMacro;
  import org.apache.turbine.util.Log;
  import org.apache.turbine.util.mail.Email;
  import org.apache.turbine.util.mail.SimpleEmail;
  
  import org.webmacro.Context;
  import org.webmacro.servlet.WebContext;
  
  /**
   * This is a simple class for sending email from within WebMacro.
   * Essentially, the body of the email is a WebMacro Context object.
   * The beauty of this is that you can send email from within your
   * WebMacro template or from your business logic in your Java code.
   * The body of the email is just a WebMacro template so you can use
   * all the template functionality of WebMacro within your emails!
   *
   * <p>Example Usage (This all needs to be on one line in your
   * template):
   *
   * <p>Setup your context:
   *
   * <p>context.put ("WebMacroEmail", new WebMacroEmail() );
   *
   * <p>Then, in your template:
   *
   * <pre>
   * $WebMacroEmail.setTo("Jon Stevens", "[EMAIL PROTECTED]")
   *     .setFrom("Mom", "[EMAIL PROTECTED]").setSubject("Eat dinner")
   *     .setTemplate("email/momEmail.wm")
   *     .setContext($context)
   * </pre>
   *
   * The email/momEmail.wm template will then be parsed with the current
   * Context that is stored in the
   * RunData.getUser().getTemp(WebMacroService.WEBMACRO_CONTEXT)
   * location.  If the context does not already exist there, it will be
   * created and then that will be used.
   *
   * <p>If you want to use this class from within your Java code all you
   * have to do is something like this:
   *
   * <pre>
   * WebMacroEmail wme = new WebMacroEmail();
   * wme.setTo("Jon Stevens", "[EMAIL PROTECTED]");
   * wme.setFrom("Mom", "[EMAIL PROTECTED]").setSubject("Eat dinner");
   * wme.setContext(context);
   * wme.setTemplate("email/momEmail.wm")
   * wme.send();
   * </pre>
   *
   * (Note that when used within a WebMacro template, the send method
   * will be called for you when WebMacro tries to convert the
   * WebMacroEmail to a string by calling toString).
   *
   * <p>This class is just a wrapper around the SimpleEmail class.
   * Thus, it uses the JavaMail API and also depends on having the
   * mail.server property set in the TurbineResources.properties file.
   * If you want to use this class outside of Turbine for general
   * processing that is also possible by making sure to set the path to
   * the TurbineResources.properties.  See the
   * TurbineResourceService.setPropertiesFileName() method for more
   * information.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
   * @version $Id: WebMacroEmail.java,v 1.1 2001/07/10 00:38:27 jvanzyl Exp $
   */
  public class WebMacroEmail
  {
      /** The to name field. */
      private String toName = null;
  
      /** The to email field. */
      private String toEmail = null;
  
      /** The from name field. */
      private String fromName = null;
  
      /** The from email field. */
      private String fromEmail = null;
  
      /** The subject of the message. */
      private String subject = null;
  
      /**
       * The template to process, relative to WM's template
       * directory.
       */
      private String template = null;
  
      /**
       * A WebContext
       */
      private WebContext context = null;
  
      /**
       * Constructor
       */
      public WebMacroEmail ()
      {
      }
  
      /**
       * Constructor
       */
      public WebMacroEmail (WebContext context)
      {
          this.context = context;
      }
  
      /**
       * To: name, email
       *
       * @param to A String with the TO name.
       * @param email A String with the TO email.
       * @return A WebMacroEmail (self).
       */
      public WebMacroEmail setTo(String to,
                                 String email)
      {
          this.toName = to;
          this.toEmail = email;
          return (this);
      }
  
      /**
       * From: name, email.
       *
       * @param from A String with the FROM name.
       * @param email A String with the FROM email.
       * @return A WebMacroEmail (self).
       */
      public WebMacroEmail setFrom(String from,
                                   String email)
      {
          this.fromName = from;
          this.fromEmail = email;
          return (this);
      }
  
      /**
       * Subject.
       *
       * @param subject A String with the subject.
       * @return A WebMacroEmail (self).
       */
      public WebMacroEmail setSubject(String subject)
      {
          this.subject = subject;
          return (this);
      }
  
      /**
       * Webmacro template to execute. Path is relative to the WM
       * templates directory.
       *
       * @param template A String with the template.
       * @return A WebMacroEmail (self).
       */
      public WebMacroEmail setTemplate(String template)
      {
          this.template = template;
          return (this);
      }
  
      /**
       * Set the context object that will be merged with the 
       * template.
       *
       * @param context A WebMacro context object.
       * @return A WebMacroEmail (self).
       */
      public WebMacroEmail setContext(WebContext context)
      {
          this.context = context;
          return (this);
      }
  
      /**
       * Get the context object that will be merged with the 
       * template.
       *
       * @return A WebContext (self).
       */
      public WebContext getContext()
      {
          return this.context;
      }
  
      /**
       * This method sends the email.
       */
      public void send()
      {
          context.put("mail",this);
          try
          {
              // Process the template.
              String body = TurbineWebMacro.handleRequest(context,template);
  
              SimpleEmail se = new SimpleEmail();
              se.setFrom(fromEmail, fromName);
              se.addTo(toEmail, toName);
              se.setSubject(subject);
              se.setMsg(body);
              se.send();
          }
          catch (Exception e)
          {
              // Log the error.
              Log.error ("WebMacroEmail error: ", e);
          }
      }
  
      /**
       * The method toString() calls send() for ease of use within a
       * WebMacro template (see example usage above).
       *
       * @return An empty ("") String.
       */
      public String toString()
      {
          send();
          return "";
      }
  }
  
  
  
  1.1                  
jakarta-turbine/src/adapter/org/apache/turbine/util/webmacro/WebMacroFormatter.java
  
  Index: WebMacroFormatter.java
  ===================================================================
  package org.apache.turbine.util.webmacro;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.lang.reflect.Array;
  
  import java.text.DateFormat;
  import java.text.NumberFormat;
  
  import java.util.Date;
  import java.util.Vector;
  
  import org.apache.turbine.util.ObjectUtils;
  
  import org.webmacro.Context;
  
  /**
   * Formatting tool for inserting into the WebMacro WebContext.  Can
   * format dates or lists of objects.
   *
   * <p>Here's an example of some uses:
   *
   * <code><pre>
   * $formatter.formatShortDate($object.Date)
   * $formatter.formatLongDate($db.getRecord(232).getDate())
   * $formatter.formatArray($array)
   * $formatter.limitLen(30, $object.Description)
   * </pre></code>
   *
   * @author <a href="[EMAIL PROTECTED]">Sean Legassick</a>
   * @version $Id: WebMacroFormatter.java,v 1.1 2001/07/10 00:38:27 jvanzyl Exp $
   */
  public class WebMacroFormatter
  {
      Context context = null;
      NumberFormat nf = NumberFormat.getInstance();
  
      /**
       * Constructor needs a backpointer to the context.
       *
       * @param context A Context.
       */
      public WebMacroFormatter(Context context)
      {
          this.context = context;
      }
  
      /**
       * Formats a date in 'short' style.
       *
       * @param date A Date.
       * @return A String.
       */
      public String formatShortDate(Date date)
      {
          return DateFormat
              .getDateInstance(DateFormat.SHORT).format(date);
      }
  
      /**
       * Formats a date in 'long' style.
       *
       * @param date A Date.
       * @return A String.
       */
      public String formatLongDate(Date date)
      {
          return DateFormat
              .getDateInstance(DateFormat.LONG).format(date);
      }
  
      /**
       * Formats a date/time in 'short' style.
       *
       * @param date A Date.
       * @return A String.
       */
      public String formatShortDateTime(Date date)
      {
          return DateFormat
              .getDateTimeInstance(DateFormat.SHORT,
                                   DateFormat.SHORT).format(date);
      }
  
      /**
       * Formats a date/time in 'long' style.
       *
       * @param date A Date.
       * @return A String.
       */
      public String formatLongDateTime(Date date)
      {
          return DateFormat.getDateTimeInstance(
                  DateFormat.LONG, DateFormat.LONG).format(date);
      }
  
      /**
       * Formats an array into the form "A, B and C".
       *
       * @param array An Object.
       * @return A String.
       */
      public String formatArray(Object array)
      {
          return formatArray(array, ", ", " and ");
      }
  
      /**
       * Formats an array into the form
       * "A&lt;delim&gt;B&lt;delim&gt;C".
       *
       * @param array An Object.
       * @param delim A String.
       * @return A String.
       */
      public String formatArray(Object array,
                                String delim)
      {
          return formatArray(array, delim, delim);
      }
  
      /**
       * Formats an array into the form
       * "A&lt;delim&gt;B&lt;finaldelim&gt;C".
       *
       * @param array An Object.
       * @param delim A String.
       * @param finalDelim A String.
       * @return A String.
       */
      public String formatArray(Object array,
                                String delim,
                                String finaldelim)
      {
          StringBuffer sb = new StringBuffer();
          int arrayLen = Array.getLength(array);
          for (int i = 0; i < arrayLen; i++)
          {
              // Use the Array.get method as this will automatically
              // wrap primitive types in a suitable Object-derived
              // wrapper if necessary.
              sb.append(Array.get(array, i).toString());
              if (i  < arrayLen - 2)
              {
                  sb.append(delim);
              }
              else if (i < arrayLen - 1)
              {
                  sb.append(finaldelim);
              }
          }
          return sb.toString();
      }
  
      /**
       * Formats a vector into the form "A, B and C".
       *
       * @param vector A Vector.
       * @return A String.
       */
      public String formatVector(Vector vector)
      {
          return formatVector(vector, ", ", " and ");
      }
  
      /**
       * Formats a vector into the form "A&lt;delim&gt;B&lt;delim&gt;C".
       *
       * @param vector A Vector.
       * @param delim A String.
       * @return A String.
       */
      public String formatVector(Vector vector,
                                 String delim)
      {
          return formatVector(vector, delim, delim);
      }
  
      /**
       * Formats a vector into the form
       * "Adelim&gt;B&lt;finaldelim&gt;C".
       *
       * @param vector A Vector.
       * @param delim A String.
       * @param finalDelim A String.
       * @return A String.
       */
      public String formatVector(Vector vector,
                                 String delim,
                                 String finaldelim)
      {
          StringBuffer sb = new StringBuffer();
          for (int i = 0; i < vector.size(); i++)
          {
              sb.append(vector.elementAt(i).toString());
              if (i < vector.size() - 2)
              {
                  sb.append(delim);
              }
              else if (i < vector.size() - 1)
              {
                  sb.append(finaldelim);
              }
          }
          return sb.toString();
      }
  
      /**
       * Limits 'string' to 'maxlen' characters.  If the string gets
       * curtailed, "..." is appended to it.
       *
       * @param maxlen An int with the maximum length.
       * @param string A String.
       * @return A String.
       */
      public String limitLen(int maxlen,
                             String string)
      {
          return limitLen(maxlen, string, "...");
      }
  
      /**
       * Limits 'string' to 'maxlen' character.  If the string gets
       * curtailed, 'suffix' is appended to it.
       *
       * @param maxlen An int with the maximum length.
       * @param string A String.
       * @param suffix A String.
       * @return A String.
       */
      public String limitLen(int maxlen,
                             String string,
                             String suffix)
      {
          String ret = string;
          if (string.length() > maxlen)
          {
              ret = string.substring(0, maxlen - suffix.length()) + suffix;
          }
          return ret;
      }
  
      /**
       * Class that returns alternating values in a template.  It stores
       * a list of alternate Strings, whenever alternate() is called it
       * switches to the next in the list.  The current alternate is
       * retrieved through toString() - i.e. just by referencing the
       * object in a webmacro template.  For an example of usage see the
       * makeAlternator() method below.
       */
      public class WebMacroAlternator
      {
          String[] alternates = null;
          int current = 0;
  
          /**
           * Constructor takes an array of Strings.
           *
           * @param alternates A String[].
           */
          public WebMacroAlternator(String[] alternates)
          {
              this.alternates = alternates;
          }
  
          /**
           * Alternates to the next in the list.
           *
           * @return A String.
           */
          public String alternate()
          {
              current++;
              current %= alternates.length;
              return "";
          }
  
          /**
           * Returns the current alternate.
           *
           * @return A String.
           */
          public String toString()
          {
              return alternates[current];
          }
      }
  
      /**
       * Makes an alternator object that alternates between two values.
       *
       * <p>Example usage in a WebMacro template:
       *
       * <code><pre>
       * &lt;table&gt;
       * $formatter.makeAlternator(rowcolor, "#c0c0c0", "#e0e0e0")
       * #foreach $item in $items
       * #begin
       * &lt;tr&gt;&lt;td bgcolor="$rowcolor"&gt;$item.Name&lt;/td&gt;&lt;/tr&gt;
       * $rowcolor.alternate()
       * #end
       * &lt;/table&gt;
       * </pre></code>
       *
       * @param name A String.
       * @param alt1 A String.
       * @param alt2 A String.
       * @return A String.
       */
      public String makeAlternator(String name,
                                   String alt1,
                                   String alt2)
      {
          String[] alternates = { alt1, alt2 };
          context.put(name, new WebMacroAlternator(alternates));
          return "";
      }
  
      /**
       * Makes an alternator object that alternates between three
       * values.
       *
       * @param name A String.
       * @param alt1 A String.
       * @param alt2 A String.
       * @param alt3 A String.
       * @return A String.
       */
      public String makeAlternator(String name,
                                   String alt1,
                                   String alt2,
                                   String alt3)
      {
          String[] alternates = { alt1, alt2, alt3 };
          context.put(name, new WebMacroAlternator(alternates));
          return "";
      }
  
      /**
       * Makes an alternator object that alternates between four values.
       *
       * @param name A String.
       * @param alt1 A String.
       * @param alt2 A String.
       * @param alt3 A String.
       * @param alt4 A String.
       * @return A String.
       */
      String makeAlternator(String name,
                            String alt1,
                            String alt2,
                            String alt3,
                            String alt4)
      {
          String[] alternates = { alt1, alt2, alt3, alt4 };
          context.put(name, new WebMacroAlternator(alternates));
          return "";
      }
  
      /**
       * Returns a default value if the object passed is null.
       */
      public Object isNull(Object o, Object dflt)
      {
         return ObjectUtils.isNull(o, dflt);
      }
  }
  
  
  
  1.1                  
jakarta-turbine/src/adapter/org/apache/turbine/util/webmacro/WebMacroHtmlEmail.java
  
  Index: WebMacroHtmlEmail.java
  ===================================================================
  package org.apache.turbine.util.webmacro;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.net.URL;
  
  import java.util.Hashtable;
  
  import javax.mail.MessagingException;
  
  import org.apache.turbine.services.webmacro.TurbineWebMacro;
  import org.apache.turbine.util.Log;
  import org.apache.turbine.util.mail.HtmlEmail;
  import org.apache.turbine.util.mail.MultiPartEmail;
  
  import org.webmacro.Context;
  import org.webmacro.servlet.WebContext;
  
  /**
   * This is a simple class for sending html email from within WebMacro.
   * Essentially, the bodies (text and html) of the email are a WebMacro
   * Context objects.  The beauty of this is that you can send email
   * from within your WebMacro template or from your business logic in
   * your Java code.  The body of the email is just a WebMacro template
   * so you can use all the template functionality of WebMacro within
   * your emails!
   *
   * <p>This class allows you to send HTML email with embedded content
   * and/or with attachments.  You can access the WebMacroHtmlEmail
   * instance within your templates trough the <code>$mail</code>
   * WebMacro variable.
   *
   * <p>The templates should be located under your Template turbine
   * directory.
   *
   * <p>This class extends the HtmlEmail class.  Thus, it uses the
   * JavaMail API and also depends on having the mail.server property
   * set in the TurbineResources.properties file.  If you want to use
   * this class outside of Turbine for general processing that is also
   * possible by making sure to set the path to the
   * TurbineResources.properties.  See the
   * TurbineConfig class for more information.
   *
   * @author <a href="mailto:unknown";>Regis Koenig</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
   * @version $Id: WebMacroHtmlEmail.java,v 1.1 2001/07/10 00:38:27 jvanzyl Exp $
   */
  public class WebMacroHtmlEmail
      extends HtmlEmail
  {
      /**
       * The html template to process, relative to WM's template
       * directory.
       */
      private String htmlTemplate = null;
  
      /**
       * The text template to process, relative to WM's template
       * directory.
       */
      private String textTemplate = null;
  
      /** The map of embedded files. */
      private Hashtable embmap = null;
  
      /**
       * A WebContext
       */
      private WebContext context = null;
  
      /**
       * Constructor, sets the RunData object.
       *
       * @param data A Turbine RunData object.
       * @exception MessagingException.
       */
      public WebMacroHtmlEmail()
          throws MessagingException
      {
          super.init();
          embmap = new Hashtable();
      }
  
      /**
       * Set the HTML template for the mail.  This is the Webmacro
       * template to execute for the HTML part.  Path is relative to the
       * WM templates directory.
       *
       * @param template A String.
       * @return A WebMacroHtmlEmail (self).
       */
      public WebMacroHtmlEmail setHtmlTemplate(String template)
      {
          this.htmlTemplate = template;
          return this;
      }
  
      /**
       * Set the text template for the mail.  This is the Webmacro
       * template to execute for the text part.  Path is relative to the
       * WM templates directory
       *
       * @param template A String.
       * @return A WebMacroHtmlEmail (self).
       */
      public WebMacroHtmlEmail setTextTemplate(String template)
      {
          this.textTemplate = template;
          return this;
      }
  
      /**
       * Set the context object that will be merged with the 
       * template.
       *
       * @param context A WebMacro context object.
       * @return A WebMacroEmail (self).
       */
      public WebMacroHtmlEmail setContext(WebContext context)
      {
          this.context = context;
          return (this);
      }
  
      /**
       * Get the context object that will be merged with the 
       * template.
       *
       * @return A WebContext (self).
       */
      public WebContext getContext()
      {
          return this.context;
      }
  
      /**
       * Actually send the mail.
       *
       * @exception MessagingException.
       */
      public void send()
          throws MessagingException
      {
          context.put("mail",this);
  
          String htmlbody = "";
          String textbody = "";
  
          // Process the templates.
          try
          {
              if( htmlTemplate != null )
                  htmlbody = TurbineWebMacro.handleRequest(context,
                                                           htmlTemplate);
              if( textTemplate != null )
                  textbody = TurbineWebMacro.handleRequest(context,
                                                           textTemplate);
          }
          catch( Exception e)
          {
              throw new MessagingException("Cannot parse template", e);
          }
  
          setHtmlMsg(htmlbody);
          setTextMsg(textbody);
  
          super.send();
      }
  
      /**
       * Embed a file in the mail.  The file can be referenced through
       * its Content-ID.  This function also registers the CID in an
       * internal map, so the embedded file can be referenced more than
       * once by using the getCid() function.  This may be useful in a
       * template.
       *
       * <p>Example of template:
       *
       * <code><pre width="80">
       * &lt;html&gt;
       * &lt;!-- $mail.embed("http://server/border.gif","border.gif";); --&gt;
       * &lt;img src=$mail.getCid("border.gif")&gt;
       * &lt;p&gt;This is your content
       * &lt;img src=$mail.getCid("border.gif")&gt;
       * &lt;/html&gt;
       * </pre></code>
       *
       * @param surl A String.
       * @param name A String.
       * @return A String with the cid of the embedded file.
       * @exception MessagingException.
       * @see HtmlEmail#embed(URL surl, String name) embed.
       */
      public String embed(String surl,
                          String name)
          throws MessagingException
      {
          String cid ="";
          try
          {
              URL url = new URL(surl);
              cid = super.embed(url, name);
              embmap.put(name,cid);
          }
          catch( Exception e )
          {
              Log.error("cannot embed "+surl+": ", e);
          }
          return cid;
      }
  
      /**
       * Get the cid of an embedded file.
       *
       * @param filename A String.
       * @return A String with the cid of the embedded file.
       * @see #embed(String surl, String name) embed.
       */
      public String getCid(String filename)
      {
          String cid = (String)embmap.get(filename);
          return "cid:"+cid;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to