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