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.tomcat.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 extends Task {
045:
046: /** The directory to contain the resulting files */
047: private File todir;
048:
049: /** The file to be converted into HTML */
050: private List<FileSet> filesets = new LinkedList<FileSet>();
051:
052: /**
053: * Sets the directory to contain the resulting files
054: *
055: * @param todir The directory
056: */
057: public void setTodir(File todir) {
058: this .todir = todir;
059: }
060:
061: /**
062: * Sets the files to be converted into HTML
063: *
064: * @param fileset The fileset to be converted.
065: */
066: public void addFileset(FileSet fs) {
067: filesets.add(fs);
068: }
069:
070: /**
071: * Perform the conversion
072: *
073: * @param BuildException Thrown if an error occurs during execution of
074: * this task.
075: */
076: public void execute() throws BuildException {
077: int count = 0;
078:
079: // Step through each file and convert.
080: Iterator iter = filesets.iterator();
081: while (iter.hasNext()) {
082: FileSet fs = (FileSet) iter.next();
083: DirectoryScanner ds = fs.getDirectoryScanner(project);
084: File basedir = ds.getBasedir();
085: String[] files = ds.getIncludedFiles();
086: for (int i = 0; i < files.length; i++) {
087: File from = new File(basedir, files[i]);
088: File to = new File(todir, files[i] + ".html");
089: if (!to.exists()
090: || (from.lastModified() > to.lastModified())) {
091: log("Converting file '" + from.getAbsolutePath()
092: + "' to '" + to.getAbsolutePath(),
093: Project.MSG_VERBOSE);
094: try {
095: convert(from, to);
096: } catch (IOException e) {
097: throw new BuildException("Could not convert '"
098: + from.getAbsolutePath() + "' to '"
099: + to.getAbsolutePath() + "'", e);
100: }
101: count++;
102: }
103: }
104: if (count > 0) {
105: log("Converted " + count + " file"
106: + (count > 1 ? "s" : "") + " to "
107: + todir.getAbsolutePath());
108: }
109: }
110: }
111:
112: /**
113: * Perform the actual copy and conversion
114: *
115: * @param from The input file
116: * @param to The output file
117: * @throws IOException Thrown if an error occurs during the conversion
118: */
119: private void convert(File from, File to) throws IOException {
120: // Open files:
121: BufferedReader in = new BufferedReader(new FileReader(from));
122: PrintWriter out = new PrintWriter(new FileWriter(to));
123:
124: // Output header:
125: out.println("<html><body><pre>");
126:
127: // Convert, line-by-line:
128: String line;
129: while ((line = in.readLine()) != null) {
130: StringBuffer result = new StringBuffer();
131: int len = line.length();
132: for (int i = 0; i < len; i++) {
133: char c = line.charAt(i);
134: switch (c) {
135: case '&':
136: result.append("&");
137: break;
138: case '<':
139: result.append("<");
140: break;
141: default:
142: result.append(c);
143: }
144: }
145: out.println(result.toString());
146: }
147:
148: // Output footer:
149: out.println("</pre></body></html>");
150:
151: // Close streams:
152: out.close();
153: in.close();
154: }
155:
156: }
|