KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > util > ReadFile


1 package org.mmbase.bridge.jsp.taglib.util;
2 import java.io.*;
3 import org.w3c.dom.*;
4 import org.mmbase.util.Casting;
5
6 /**
7  * This file is found in the public domain, on http://www.dpawson.co.uk/xsl/sect2/N4760.html
8  * It is used to be able to include the codesamples in the taglib documentation.
9  * XSLT does not provide a way to easily include textfiles into your final document,
10  * so unfortunately we need this class to be able to do that.
11  * @author Johannes Verelst
12  */

13 public class ReadFile {
14     public static Document readExample(String JavaDoc actualFileName) {
15         return Casting.toXML("<example><![CDATA[" + contents(actualFileName) + "]]></example>");
16     }
17
18     public static String JavaDoc contents(String JavaDoc actualFileName) {
19         FileReader in = null;
20         try {
21             in = new FileReader(actualFileName);
22         } catch (FileNotFoundException e) {
23             return "File '" + actualFileName + "' not found.\n";
24         }
25         StringWriter out = new StringWriter();
26
27         char[] buffer = new char[4096];
28         int numchars;
29         try {
30             while((numchars = in.read(buffer)) != -1) {
31                 out.write(buffer, 0, numchars);
32             }
33             out.close();
34         } catch (IOException e) {
35             return "IO Error reading file '" + actualFileName + "'.\n";
36         }
37         String JavaDoc returnContents = out.toString();
38         return returnContents.trim();
39     }
40 }
41
Popular Tags