jstrachan    2002/10/02 09:07:27

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/util
                        UtilTagLibrary.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/util
                        LoadTextTag.java
  Log:
  Added a simple utility tag that allows a text file to be loaded into a variable as a 
String.
  
  Revision  Changes    Path
  1.3       +7 -6      
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java
  
  Index: UtilTagLibrary.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- UtilTagLibrary.java       2 Oct 2002 11:03:38 -0000       1.2
  +++ UtilTagLibrary.java       2 Oct 2002 16:07:27 -0000       1.3
  @@ -71,7 +71,8 @@
   public class UtilTagLibrary extends TagLibrary
   {
       public UtilTagLibrary() {
  -        registerTag("tokenize", TokenizeTag.class);
  +        registerTag("loadText", LoadTextTag.class);
           registerTag("properties", PropertiesTag.class);
  +        registerTag("tokenize", TokenizeTag.class);
       }
   }
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/LoadTextTag.java
  
  Index: LoadTextTag.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/LoadTextTag.java,v
 1.5 2002/05/17 15:18:08 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:08 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" 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"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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/>.
   * 
   * $Id: LoadTextTag.java,v 1.5 2002/05/17 15:18:08 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.util;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Reader;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /** 
   * A tag which catches exceptions thrown by its body.
   * This allows conditional logic to be performed based on if exceptions
   * are thrown or to do some kind of custom exception logging logic.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
   * @version $Revision: 1.5 $
   */
  public class LoadTextTag extends TagSupport {
  
      private String var;
      private File file;
      private String uri;
  
      public LoadTextTag() {
      }
  
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(XMLOutput output) throws Exception {
          if (var == null) {
              throw new MissingAttributeException("var");
          }
          if (file == null && uri == null) {
              throw new JellyException( "This tag must have a 'file' or 'uri' 
specified" );
          }
          Reader reader = null;
          if (file != null) {
              if (! file.exists()) {
                  throw new JellyException( "The file: " + file + " does not exist" );
              }
              reader = new FileReader(file);
          }   
          else {
              InputStream in = context.getResourceAsStream(uri);
              if (in == null) {
                  throw new JellyException( "Could not find uri: " + uri );
              }
              // @todo should we allow an encoding to be specified?
              reader = new InputStreamReader(in);
          }
          String text = loadText(reader);
          context.setVariable(var, text);
      }
  
      // Properties
      //------------------------------------------------------------------------- 
      
      /**
       * Sets the name of the variable which will be exported with the text value of 
the
       * given file.
       */
      public void setVar(String var) {
          this.var = var;
      }
      /**
       * Returns the file.
       * @return File
       */
      public File getFile() {
          return file;
      }
  
      /**
       * Returns the uri.
       * @return String
       */
      public String getUri() {
          return uri;
      }
  
      /**
       * Returns the var.
       * @return String
       */
      public String getVar() {
          return var;
      }
  
      /**
       * Sets the file to be parsed as text
       */
      public void setFile(File file) {
          this.file = file;
      }
  
      /**
       * Sets the uri to be parsed as text. 
       * This can be an absolute URL or a relative or absolute URI 
       * from this Jelly script or the root context.
       */
      public void setUri(String uri) {
          this.uri = uri;
      }
  
  
      // Implementation methods
      //------------------------------------------------------------------------- 
  
      /**
       * Loads all the text from the given Reader
       */
      protected String loadText(Reader reader) throws Exception {
          StringBuffer buffer = new StringBuffer();
          
          // @todo its probably more efficient to use a fixed char[] buffer instead
          BufferedReader bufferedReader = new BufferedReader(reader);
          while (true) {
              String line = bufferedReader.readLine();
              if (line == null) {
                  break;
              }
              else {
                  buffer.append(line);
                  buffer.append('\n');
              }
          }
          return buffer.toString();
      }
  }
  
  
  

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

Reply via email to