View Javadoc

    1   // ========================================================================
    2   // $Id: Include.java,v 1.6 2005/08/13 00:01:23 gregwilkins Exp $
    3   // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.html;
    17  import java.io.BufferedReader;
    18  import java.io.File;
    19  import java.io.FileNotFoundException;
    20  import java.io.FileReader;
    21  import java.io.IOException;
    22  import java.io.InputStream;
    23  import java.io.InputStreamReader;
    24  import java.io.Reader;
    25  import java.io.StringReader;
    26  import java.io.StringWriter;
    27  import java.io.Writer;
    28  import java.net.URL;
    29
    30  import org.mortbay.util.IO;
    31
    32
    33  /* -------------------------------------------------------------------- */
    34  /** Include File, InputStream or Reader Element.
    35   * <p>This Element includes another file.
    36   * This class expects that the HTTP directory separator '/' will be used.
    37   * This will be converted to the local directory separator.
    38   * @see Element
    39   * @version $Id: Include.java,v 1.6 2005/08/13 00:01:23 gregwilkins Exp $
    40   * @author Greg Wilkins
    41  */
    42  public class Include extends Element
    43  {
    44
    45      Reader reader=null;
    46
    47      /* ------------------------------------------------------------ */
    48      /** Constructor.
    49       * Include file
    50       * @param directory Directory name
    51       * @param fileName file name
    52       * @exception IOException File not found
    53       */
    54      public Include(String directory,
    55                     String fileName)
    56           throws IOException
    57      {
    58          if (directory==null)
    59              directory=".";
    60
    61          if (File.separatorChar != '/')
    62          {
    63              directory = directory.replace('/',File.separatorChar);
    64              fileName  = fileName .replace('/',File.separatorChar);
    65          }
    66
    67          includeFile(new File(directory,fileName));
    68      }
    69
    70      /* ------------------------------------------------------------ */
    71      /** Constructor.
    72       * Include file.
    73       * @param fileName Filename
    74       * @exception IOException File not found
    75       */
    76      public Include(String fileName)
    77          throws IOException
    78      {
    79          if (File.separatorChar != '/')
    80              fileName  = fileName .replace('/',File.separatorChar);
    81          includeFile(new File(fileName));
    82      }
    83
    84      /* ------------------------------------------------------------ */
    85      /** Constructor.
    86       * Include file.
    87       * @param file file
    88       * @exception IOException File not found
    89       */
    90      public Include(File file)
    91          throws IOException
    92      {
    93          includeFile(file);
    94      }
    95
    96      /* ------------------------------------------------------------ */
    97      /** Constructor.
    98       * Include InputStream.
    99       * Byte to character transformation is done assuming the default
    100      * local character set.  What this means is that on EBCDIC systems
    101      * the included file is assumed to be in EBCDIC.
    102      * @param in stream
    103      * @exception IOException
    104      */
    105     public Include(InputStream in)
    106         throws IOException
    107     {
    108         if (in!=null)
    109             reader=new InputStreamReader(in);
    110     }
    111
    112     /* ------------------------------------------------------------ */
    113     /** Constructor.
    114      * Include contents of a URL.
    115      * Byte to character transformation is done assuming the default
    116      * local character set.  What this means is that on EBCDIC systems
    117      * the included file is assumed to be in EBCDIC.
    118      * @param url the URL to read from.
    119      * @exception IOException
    120      */
    121     public Include(URL url)
    122         throws IOException
    123     {
    124         if (url!=null)
    125             reader=new InputStreamReader(url.openStream());
    126     }
    127
    128     /* ------------------------------------------------------------ */
    129     /** Constructor.
    130      * Include Reader.
    131      * @param in reader
    132      * @exception IOException
    133      */
    134     public Include(Reader in)
    135         throws IOException
    136     {
    137         reader=in;
    138     }
    139
    140     /* ------------------------------------------------------------ */
    141     private void includeFile(File file)
    142         throws IOException
    143     {
    144         if (!file.exists())
    145             throw new FileNotFoundException(file.toString());
    146
    147         if (file.isDirectory())
    148         {
    149             List list = new List(List.Unordered);
    150             String[] ls = file.list();
    151             for (int i=0 ; i< ls.length ; i++)
    152                 list.add(ls[i]);
    153             StringWriter sw = new StringWriter();
    154             list.write(sw);
    155             reader = new StringReader(sw.toString());
    156         }
    157         else
    158         {
    159             reader = new BufferedReader(new FileReader(file));
    160         }
    161     }
    162
    163
    164     /* ---------------------------------------------------------------- */
    165     public void write(Writer out)
    166          throws IOException
    167     {
    168         if (reader==null)
    169             return;
    170
    171         try{
    172             IO.copy(reader,out);
    173         }
    174         finally
    175         {
    176             reader.close();
    177             reader=null;
    178         }
    179     }
    180 }
    181
    182
    183
    184
    185
    186
    187
    188
    189