FileDocCategorySizeDatePackage
Txt2Html.javaAPI DocApache Tomcat 6.0.145444Fri Jul 20 04:20:36 BST 2007org.apache.tomcat.buildutil

Txt2Html

public class Txt2Html extends org.apache.tools.ant.Task
Ant task to convert a given set of files from Text to HTML. Inserts an HTML header including pre tags and replaces special characters with their HTML escaped equivalents.

This task is currently used by the ant script to build our examples

author
Mark Roth

Fields Summary
private File
todir
The directory to contain the resulting files
private List
filesets
The file to be converted into HTML
Constructors Summary
Methods Summary
public voidaddFileset(org.apache.tools.ant.types.FileSet fs)
Sets the files to be converted into HTML

param
fileset The fileset to be converted.

        filesets.add( fs );
    
private voidconvert(java.io.File from, java.io.File to)
Perform the actual copy and conversion

param
from The input file
param
to The output file
throws
IOException Thrown if an error occurs during the conversion

        // Open files:
        BufferedReader in = new BufferedReader( new FileReader( from ) );
        PrintWriter out = new PrintWriter( new FileWriter( to ) );
        
        // Output header:
        out.println( "<html><body><pre>" );
        
        // Convert, line-by-line:
        String line;
        while( (line = in.readLine()) != null ) {
            StringBuffer result = new StringBuffer();
            int len = line.length();
            for( int i = 0; i < len; i++ ) {
                char c = line.charAt( i );
                switch( c ) {
                    case '&":
                        result.append( "&" );
                        break;
                    case '<":
                        result.append( "<" );
                        break;
                    default:
                        result.append( c );
                }
            }
            out.println( result.toString() );
        }
        
        // Output footer:
        out.println( "</pre></body></html>" );
        
        // Close streams:
        out.close();
        in.close();
    
public voidexecute()
Perform the conversion

param
BuildException Thrown if an error occurs during execution of this task.

        int count = 0;
        
        // Step through each file and convert.
        Iterator iter = filesets.iterator();
        while( iter.hasNext() ) {
            FileSet fs = (FileSet)iter.next();
            DirectoryScanner ds = fs.getDirectoryScanner( project );
            File basedir = ds.getBasedir();
            String[] files = ds.getIncludedFiles();
            for( int i = 0; i < files.length; i++ ) {
                File from = new File( basedir, files[i] );
                File to = new File( todir, files[i] + ".html" );
                if( !to.exists() || 
                    (from.lastModified() > to.lastModified()) ) 
                {
                    log( "Converting file '" + from.getAbsolutePath() + 
                        "' to '" + to.getAbsolutePath(), Project.MSG_VERBOSE );
                    try {
                        convert( from, to );
                    }
                    catch( IOException e ) {
                        throw new BuildException( "Could not convert '" + 
                            from.getAbsolutePath() + "' to '" + 
                            to.getAbsolutePath() + "'", e );
                    }
                    count++;
                }
            }
            if( count > 0 ) {
                log( "Converted " + count + " file" + (count > 1 ? "s" : "") + 
                    " to " + todir.getAbsolutePath() );
            }
        }
    
public voidsetTodir(java.io.File todir)
Sets the directory to contain the resulting files

param
todir The directory

    
                     
          
        this.todir = todir;