KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > task > Txt2Html


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25
26 package task;
27
28 import java.io.BufferedReader JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.FileWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.DirectoryScanner;
39 import org.apache.tools.ant.Project;
40 import org.apache.tools.ant.Task;
41 import org.apache.tools.ant.types.FileSet;
42
43 /**
44  * Ant task to convert a given set of files from Text to HTML.
45  * Inserts an HTML header including pre tags and replaces special characters
46  * with their HTML escaped equivalents.
47  *
48  * <p>This task is currently used by the ant script to build our examples</p>
49  *
50  * @author Mark Roth
51  */

52 public class Txt2Html
53     extends Task
54 {
55     
56     /** The directory to contain the resulting files */
57     private File JavaDoc todir;
58     
59     /** The file to be converted into HTML */
60     private List JavaDoc filesets = new LinkedList JavaDoc();
61     
62     /**
63      * Sets the directory to contain the resulting files
64      *
65      * @param todir The directory
66      */

67     public void setTodir( File JavaDoc todir ) {
68         this.todir = todir;
69     }
70     
71     /**
72      * Sets the files to be converted into HTML
73      *
74      * @param fileset The fileset to be converted.
75      */

76     public void addFileset( FileSet fs ) {
77         filesets.add( fs );
78     }
79     
80     /**
81      * Perform the conversion
82      *
83      * @param BuildException Thrown if an error occurs during execution of
84      * this task.
85      */

86     public void execute()
87         throws BuildException
88     {
89         int count = 0;
90         
91         // Step through each file and convert.
92
Iterator JavaDoc iter = filesets.iterator();
93         while( iter.hasNext() ) {
94             FileSet fs = (FileSet)iter.next();
95             DirectoryScanner ds = fs.getDirectoryScanner( project );
96             File JavaDoc basedir = ds.getBasedir();
97             String JavaDoc[] files = ds.getIncludedFiles();
98             for( int i = 0; i < files.length; i++ ) {
99                 File JavaDoc from = new File JavaDoc( basedir, files[i] );
100                 File JavaDoc to = new File JavaDoc( todir, files[i] + ".html" );
101                 if( !to.exists() ||
102                     (from.lastModified() > to.lastModified()) )
103                 {
104                     log( "Converting file '" + from.getAbsolutePath() +
105                         "' to '" + to.getAbsolutePath(), Project.MSG_VERBOSE );
106                     try {
107                         convert( from, to );
108                     }
109                     catch( IOException JavaDoc e ) {
110                         throw new BuildException( "Could not convert '" +
111                             from.getAbsolutePath() + "' to '" +
112                             to.getAbsolutePath() + "'", e );
113                     }
114                     count++;
115                 }
116             }
117             if( count > 0 ) {
118                 log( "Converted " + count + " file" + (count > 1 ? "s" : "") +
119                     " to " + todir.getAbsolutePath() );
120             }
121         }
122     }
123     
124     /**
125      * Perform the actual copy and conversion
126      *
127      * @param from The input file
128      * @param to The output file
129      * @throws IOException Thrown if an error occurs during the conversion
130      */

131     private void convert( File JavaDoc from, File JavaDoc to )
132         throws IOException JavaDoc
133     {
134         // Open files:
135
BufferedReader JavaDoc in = new BufferedReader JavaDoc( new FileReader JavaDoc( from ) );
136         PrintWriter JavaDoc out = new PrintWriter JavaDoc( new FileWriter JavaDoc( to ) );
137         
138         // Output header:
139
out.println( "<html><body><pre>" );
140         
141         // Convert, line-by-line:
142
String JavaDoc line;
143         while( (line = in.readLine()) != null ) {
144             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
145             int len = line.length();
146             for( int i = 0; i < len; i++ ) {
147                 char c = line.charAt( i );
148                 switch( c ) {
149                     case '&':
150                         result.append( "&amp;" );
151                         break;
152                     case '<':
153                         result.append( "&lt;" );
154                         break;
155                     default:
156                         result.append( c );
157                 }
158             }
159             out.println( result.toString() );
160         }
161         
162         // Output footer:
163
out.println( "</pre></body></html>" );
164         
165         // Close streams:
166
out.close();
167         in.close();
168     }
169     
170 }
171
172
173
Popular Tags