001    /*
002    * Licensed to the Apache Software Foundation (ASF) under one or more
003    * contributor license agreements.  See the NOTICE file distributed with
004    * this work for additional information regarding copyright ownership.
005    * The ASF licenses this file to You under the Apache License, Version 2.0
006    * (the "License"); you may not use this file except in compliance with
007    * the License.  You may obtain a copy of the License at
008    *
009    *     http://www.apache.org/licenses/LICENSE-2.0
010    *
011    * Unless required by applicable law or agreed to in writing, software
012    * distributed under the License is distributed on an "AS IS" BASIS,
013    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014    * See the License for the specific language governing permissions and
015    * limitations under the License.
016    */
017    
018    package org.apache.geronimo.samples.buildutil;
019    
020    import java.io.BufferedReader;
021    import java.io.File;
022    import java.io.FileReader;
023    import java.io.FileWriter;
024    import java.io.IOException;
025    import java.io.PrintWriter;
026    import java.util.Iterator;
027    import java.util.LinkedList;
028    import java.util.List;
029    import org.apache.tools.ant.BuildException;
030    import org.apache.tools.ant.DirectoryScanner;
031    import org.apache.tools.ant.Project;
032    import org.apache.tools.ant.Task;
033    import org.apache.tools.ant.types.FileSet;
034    
035    /**
036     * Ant task to convert a given set of files from Text to HTML.
037     * Inserts an HTML header including pre tags and replaces special characters
038     * with their HTML escaped equivalents.
039     *
040     * <p>This task is currently used by the ant script to build our examples</p>
041     *
042     * @author Mark Roth
043     */
044    public class Txt2Html 
045        extends Task 
046    {
047        
048        /** The directory to contain the resulting files */
049        private File todir;
050        
051        /** The file to be converted into HTML */
052        private List<FileSet> filesets = new LinkedList<FileSet>();
053        
054        /**
055         * Sets the directory to contain the resulting files
056         *
057         * @param todir The directory
058         */
059        public void setTodir( File todir ) {
060            this.todir = todir;
061        }
062        
063        /**
064         * Sets the files to be converted into HTML
065         *
066         * @param fileset The fileset to be converted.
067         */
068        public void addFileset( FileSet fs ) {
069            filesets.add( fs );
070        }
071        
072        /**
073         * Perform the conversion
074         *
075         * @param BuildException Thrown if an error occurs during execution of
076         *    this task.
077         */
078        public void execute() 
079            throws BuildException 
080        {
081            int count = 0;
082            
083            // Step through each file and convert.
084            Iterator iter = filesets.iterator();
085            while( iter.hasNext() ) {
086                FileSet fs = (FileSet)iter.next();
087                DirectoryScanner ds = fs.getDirectoryScanner( project );
088                File basedir = ds.getBasedir();
089                String[] files = ds.getIncludedFiles();
090                for( int i = 0; i < files.length; i++ ) {
091                    File from = new File( basedir, files[i] );
092                    File to = new File( todir, files[i] + ".html" );
093                    if( !to.exists() || 
094                        (from.lastModified() > to.lastModified()) ) 
095                    {
096                        log( "Converting file '" + from.getAbsolutePath() + 
097                            "' to '" + to.getAbsolutePath(), Project.MSG_VERBOSE );
098                        try {
099                            convert( from, to );
100                        }
101                        catch( IOException e ) {
102                            throw new BuildException( "Could not convert '" + 
103                                from.getAbsolutePath() + "' to '" + 
104                                to.getAbsolutePath() + "'", e );
105                        }
106                        count++;
107                    }
108                }
109                if( count > 0 ) {
110                    log( "Converted " + count + " file" + (count > 1 ? "s" : "") + 
111                        " to " + todir.getAbsolutePath() );
112                }
113            }
114        }
115        
116        /**
117         * Perform the actual copy and conversion
118         *
119         * @param from The input file
120         * @param to The output file
121         * @throws IOException Thrown if an error occurs during the conversion
122         */
123        private void convert( File from, File to )
124            throws IOException
125        {
126            // Open files:
127            BufferedReader in = new BufferedReader( new FileReader( from ) );
128            PrintWriter out = new PrintWriter( new FileWriter( to ) );
129            
130            // Output header:
131            out.println( "<html><body><pre>" );
132            
133            // Convert, line-by-line:
134            String line;
135            while( (line = in.readLine()) != null ) {
136                StringBuffer result = new StringBuffer();
137                int len = line.length();
138                for( int i = 0; i < len; i++ ) {
139                    char c = line.charAt( i );
140                    switch( c ) {
141                        case '&':
142                            result.append( "&amp;" );
143                            break;
144                        case '<':
145                            result.append( "&lt;" );
146                            break;
147                        default:
148                            result.append( c );
149                    }
150                }
151                out.println( result.toString() );
152            }
153            
154            // Output footer:
155            out.println( "</pre></body></html>" );
156            
157            // Close streams:
158            out.close();
159            in.close();
160        }
161        
162    }
163    
164