KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > html > Include


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 JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.FileReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.mortbay.log.LogFactory;
32 import org.mortbay.util.IO;
33
34 /* -------------------------------------------------------------------- */
35 /** Include File, InputStream or Reader Element.
36  * <p>This Element includes another file.
37  * This class expects that the HTTP directory separator '/' will be used.
38  * This will be converted to the local directory separator.
39  * @see Element
40  * @version $Id: Include.java,v 1.6 2005/08/13 00:01:23 gregwilkins Exp $
41  * @author Greg Wilkins
42 */

43 public class Include extends Element
44 {
45     private static Log log = LogFactory.getLog(Include.class);
46
47     Reader JavaDoc reader=null;
48     
49     /* ------------------------------------------------------------ */
50     /** Constructor.
51      * Include file
52      * @param directory Directory name
53      * @param fileName file name
54      * @exception IOException File not found
55      */

56     public Include(String JavaDoc directory,
57                    String JavaDoc fileName)
58          throws IOException JavaDoc
59     {
60         if (directory==null)
61             directory=".";
62  
63         if (File.separatorChar != '/')
64         {
65             directory = directory.replace('/',File.separatorChar);
66             fileName = fileName .replace('/',File.separatorChar);
67         }
68
69         if(log.isDebugEnabled())log.debug("IncludeTag("+directory+","+fileName+")");
70         includeFile(new File JavaDoc(directory,fileName));
71     }
72     
73     /* ------------------------------------------------------------ */
74     /** Constructor.
75      * Include file.
76      * @param fileName Filename
77      * @exception IOException File not found
78      */

79     public Include(String JavaDoc fileName)
80         throws IOException JavaDoc
81     {
82         if (File.separatorChar != '/')
83             fileName = fileName .replace('/',File.separatorChar);
84         if(log.isDebugEnabled())log.debug("IncludeTag("+fileName+")");
85         includeFile(new File JavaDoc(fileName));
86     }
87
88     /* ------------------------------------------------------------ */
89     /** Constructor.
90      * Include file.
91      * @param file file
92      * @exception IOException File not found
93      */

94     public Include(File JavaDoc file)
95         throws IOException JavaDoc
96     {
97         if(log.isDebugEnabled())log.debug("IncludeTag("+file+")");
98         includeFile(file);
99     }
100
101     /* ------------------------------------------------------------ */
102     /** Constructor.
103      * Include InputStream.
104      * Byte to character transformation is done assuming the default
105      * local character set. What this means is that on EBCDIC systems
106      * the included file is assumed to be in EBCDIC.
107      * @param in stream
108      * @exception IOException
109      */

110     public Include(InputStream JavaDoc in)
111         throws IOException JavaDoc
112     {
113         if (in!=null)
114             reader=new InputStreamReader JavaDoc(in);
115     }
116     
117     /* ------------------------------------------------------------ */
118     /** Constructor.
119      * Include contents of a URL.
120      * Byte to character transformation is done assuming the default
121      * local character set. What this means is that on EBCDIC systems
122      * the included file is assumed to be in EBCDIC.
123      * @param url the URL to read from.
124      * @exception IOException
125      */

126     public Include(URL JavaDoc url)
127         throws IOException JavaDoc
128     {
129         if (url!=null)
130             reader=new InputStreamReader JavaDoc(url.openStream());
131     }
132     
133     /* ------------------------------------------------------------ */
134     /** Constructor.
135      * Include Reader.
136      * @param in reader
137      * @exception IOException
138      */

139     public Include(Reader JavaDoc in)
140         throws IOException JavaDoc
141     {
142         reader=in;
143     }
144     
145     /* ------------------------------------------------------------ */
146     private void includeFile(File JavaDoc file)
147         throws IOException JavaDoc
148     {
149         if (!file.exists())
150             throw new FileNotFoundException JavaDoc(file.toString());
151         
152         if (file.isDirectory())
153         {
154             List list = new List(List.Unordered);
155             String JavaDoc[] ls = file.list();
156             for (int i=0 ; i< ls.length ; i++)
157                 list.add(ls[i]);
158             StringWriter JavaDoc sw = new StringWriter JavaDoc();
159             list.write(sw);
160             reader = new StringReader JavaDoc(sw.toString());
161         }
162         else
163         {
164             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
165         }
166     }
167     
168
169     /* ---------------------------------------------------------------- */
170     public void write(Writer JavaDoc out)
171          throws IOException JavaDoc
172     {
173         if (reader==null)
174             return;
175         
176         try{
177             IO.copy(reader,out);
178         }
179         finally
180         {
181             reader.close();
182             reader=null;
183         }
184     }
185 }
186
187
188
189
190
191
192
193
194
195
Popular Tags