001: // ========================================================================
002: // $Id: Include.java,v 1.6 2005/08/13 00:01:23 gregwilkins Exp $
003: // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.html;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.FileNotFoundException;
021: import java.io.FileReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.Reader;
026: import java.io.StringReader;
027: import java.io.StringWriter;
028: import java.io.Writer;
029: import java.net.URL;
030:
031: import org.mortbay.util.IO;
032:
033: /* -------------------------------------------------------------------- */
034: /** Include File, InputStream or Reader Element.
035: * <p>This Element includes another file.
036: * This class expects that the HTTP directory separator '/' will be used.
037: * This will be converted to the local directory separator.
038: * @see Element
039: * @version $Id: Include.java,v 1.6 2005/08/13 00:01:23 gregwilkins Exp $
040: * @author Greg Wilkins
041: */
042: public class Include extends Element {
043:
044: Reader reader = null;
045:
046: /* ------------------------------------------------------------ */
047: /** Constructor.
048: * Include file
049: * @param directory Directory name
050: * @param fileName file name
051: * @exception IOException File not found
052: */
053: public Include(String directory, String fileName)
054: throws IOException {
055: if (directory == null)
056: directory = ".";
057:
058: if (File.separatorChar != '/') {
059: directory = directory.replace('/', File.separatorChar);
060: fileName = fileName.replace('/', File.separatorChar);
061: }
062:
063: includeFile(new File(directory, fileName));
064: }
065:
066: /* ------------------------------------------------------------ */
067: /** Constructor.
068: * Include file.
069: * @param fileName Filename
070: * @exception IOException File not found
071: */
072: public Include(String fileName) throws IOException {
073: if (File.separatorChar != '/')
074: fileName = fileName.replace('/', File.separatorChar);
075: includeFile(new File(fileName));
076: }
077:
078: /* ------------------------------------------------------------ */
079: /** Constructor.
080: * Include file.
081: * @param file file
082: * @exception IOException File not found
083: */
084: public Include(File file) throws IOException {
085: includeFile(file);
086: }
087:
088: /* ------------------------------------------------------------ */
089: /** Constructor.
090: * Include InputStream.
091: * Byte to character transformation is done assuming the default
092: * local character set. What this means is that on EBCDIC systems
093: * the included file is assumed to be in EBCDIC.
094: * @param in stream
095: * @exception IOException
096: */
097: public Include(InputStream in) throws IOException {
098: if (in != null)
099: reader = new InputStreamReader(in);
100: }
101:
102: /* ------------------------------------------------------------ */
103: /** Constructor.
104: * Include contents of a URL.
105: * Byte to character transformation is done assuming the default
106: * local character set. What this means is that on EBCDIC systems
107: * the included file is assumed to be in EBCDIC.
108: * @param url the URL to read from.
109: * @exception IOException
110: */
111: public Include(URL url) throws IOException {
112: if (url != null)
113: reader = new InputStreamReader(url.openStream());
114: }
115:
116: /* ------------------------------------------------------------ */
117: /** Constructor.
118: * Include Reader.
119: * @param in reader
120: * @exception IOException
121: */
122: public Include(Reader in) throws IOException {
123: reader = in;
124: }
125:
126: /* ------------------------------------------------------------ */
127: private void includeFile(File file) throws IOException {
128: if (!file.exists())
129: throw new FileNotFoundException(file.toString());
130:
131: if (file.isDirectory()) {
132: List list = new List(List.Unordered);
133: String[] ls = file.list();
134: for (int i = 0; i < ls.length; i++)
135: list.add(ls[i]);
136: StringWriter sw = new StringWriter();
137: list.write(sw);
138: reader = new StringReader(sw.toString());
139: } else {
140: reader = new BufferedReader(new FileReader(file));
141: }
142: }
143:
144: /* ---------------------------------------------------------------- */
145: public void write(Writer out) throws IOException {
146: if (reader == null)
147: return;
148:
149: try {
150: IO.copy(reader, out);
151: } finally {
152: reader.close();
153: reader = null;
154: }
155: }
156: }
|