HTMLLikeMarkupLanguage.java

  1. /*
  2.  * $Source$
  3.  * $Revision$
  4.  *
  5.  * Copyright (C) 2000 William Chesters
  6.  *
  7.  * Part of Melati (http://melati.org), a framework for the rapid
  8.  * development of clean, maintainable web applications.
  9.  *
  10.  * Melati is free software; Permission is granted to copy, distribute
  11.  * and/or modify this software under the terms either:
  12.  *
  13.  * a) the GNU General Public License as published by the Free Software
  14.  *    Foundation; either version 2 of the License, or (at your option)
  15.  *    any later version,
  16.  *
  17.  *    or
  18.  *
  19.  * b) any version of the Melati Software License, as published
  20.  *    at http://melati.org
  21.  *
  22.  * You should have received a copy of the GNU General Public License and
  23.  * the Melati Software License along with this program;
  24.  * if not, write to the Free Software Foundation, Inc.,
  25.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
  26.  * GNU General Public License and visit http://melati.org to obtain the
  27.  * Melati Software License.
  28.  *
  29.  * Feel free to contact the Developers of Melati (http://melati.org),
  30.  * if you would like to work out a different arrangement than the options
  31.  * outlined here.  It is our intention to allow Melati to be used by as
  32.  * wide an audience as possible.
  33.  *
  34.  * This program is distributed in the hope that it will be useful,
  35.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37.  * GNU General Public License for more details.
  38.  *
  39.  * Contact details for copyright holder:
  40.  *
  41.  *     William Chesters <williamc@paneris.org>
  42.  *     http://paneris.org/~williamc
  43.  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
  44.  */

  45. package org.melati.template;

  46. import java.io.IOException;
  47. import java.text.DateFormat;

  48. import org.melati.Melati;
  49. import org.melati.poem.Persistent;
  50. import org.melati.util.MelatiWriter;
  51. import org.melati.poem.PoemLocale;
  52. import org.melati.util.HTMLUtils;
  53. import org.melati.util.UTF8URLEncoder;

  54. /**
  55.  * A Markup language with syntax similar to HTML.
  56.  */
  57. public abstract class HTMLLikeMarkupLanguage extends AbstractMarkupLanguage
  58.     implements MarkupLanguage {

  59.   /**
  60.    * Constructor.
  61.    */
  62.   public HTMLLikeMarkupLanguage(String name, Melati melati,
  63.                                 TempletLoader templetLoader,
  64.                                 PoemLocale locale) {
  65.     super(name, melati, templetLoader, locale);
  66.   }
  67.  
  68.   protected HTMLLikeMarkupLanguage(String name, HTMLLikeMarkupLanguage other) {
  69.     super(name, other);
  70.   }

  71.  /**
  72.   * Render, directly to the output stream, substituting entities for
  73.   * high value ASCII characters and new line characters.
  74.   * To avoid the unnecessary overhead of writing to a string and then writing
  75.   * that string to the output stream we render directly to the output stream.
  76.   */
  77.   public void render(String s, MelatiWriter writer) {
  78.     try {
  79.       writer.write(HTMLUtils.entitied(s, true, melati.getEncoding(), false));
  80.     } catch (IOException e) {
  81.       throw new TemplateIOException("Problem writing " + s, e);
  82.     }
  83.      
  84.   }
  85.  
  86.   /**
  87.    * {@inheritDoc}
  88.    * @see org.melati.template.AbstractMarkupLanguage#renderMarkup(java.lang.String, org.melati.util.MelatiWriter)
  89.    */
  90.   public void renderMarkup(String s, MelatiWriter writer) {
  91.     try {
  92.       writer.write(HTMLUtils.entitied(s, false, melati.getEncoding(), true));
  93.     } catch (IOException e) {
  94.       throw new TemplateIOException("Problem writing " + s, e);
  95.     }    
  96.   }

  97.   /**
  98.    * Escape a String.
  99.    *
  100.    * @param s the String to escape
  101.    * @return the escaped String
  102.    */
  103.   public String escaped(String s) {
  104.     return HTMLUtils.jsEscaped(s);
  105.   }

  106.   /**
  107.    * Get the DisplayString of a <code>Persistent</code> and
  108.    * escape that using the current locale and a MEDIUM DateFormat.
  109.    *
  110.    * See org/melati/admin/SelectionWindowSelection.wm
  111.    * See org/melati/admin/Update.wm
  112.    * @param o
  113.    * @return the escaped DisplayString
  114.    */
  115.   public String escaped(Persistent o) {
  116.     return escaped(o.displayString(locale, DateFormat.MEDIUM));
  117.   }

  118.   /**
  119.    * {@inheritDoc}
  120.    * @see org.melati.template.MarkupLanguage#encoded(java.lang.String)
  121.    */
  122.   public String encoded(String s) {
  123.     return UTF8URLEncoder.encode(s, melati.getEncoding());
  124.   }
  125.   /**
  126.    * {@inheritDoc}
  127.    * @see org.melati.template.MarkupLanguage#decoded(java.lang.String)
  128.    */
  129.   public String decoded(String s) {
  130.     return UTF8URLEncoder.decode(s, melati.getEncoding());
  131.   }
  132. }