/*
* This file is part of a syntax highlighting package
* Copyright (C) 2003 Stephen Ostermiller
* http://ostermiller.org/contact.pl?regarding=Syntax+Highlighting
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See COPYING.TXT for details.
*/
package com.Ostermiller.bte;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.util.*;
import org.apache.tools.ant.types.*;
import java_cup.runtime.Symbol;
import java.io.*;
import java.net.*;
public class BTEAntTask extends MatchingTask {
public BTEAntTask(){
}
private File srcDir;
/**
* Set the source dir to find the source files.
*/
public void setSrcdir(File srcDir) {
this.srcDir = srcDir;
}
private File destDir = null;
/**
* Set the destination where the fixed files should be placed.
* Default is to replace the original file.
*/
public void setDestdir(File destDir) {
this.destDir = destDir;
}
/**
* Executes the task.
*/
public void execute() throws BuildException {
if (srcDir == null) {
srcDir = getProject().getBaseDir();
}
if (!srcDir.exists()) {
throw new BuildException("srcdir does not exist!");
}
if (!srcDir.isDirectory()) {
throw new BuildException("srcdir is not a directory!");
}
if (srcDir == null) {
destDir = srcDir;
}
if (!destDir.exists()) {
throw new BuildException("destdir does not exist!");
}
if (!destDir.isDirectory()) {
throw new BuildException("destdir is not a directory!");
}
DirectoryScanner ds = super.getDirectoryScanner(srcDir);
String[] files = ds.getIncludedFiles();
SourceFileScanner sfs = new SourceFileScanner(this);
String[] newFiles = sfs.restrict(
files,
srcDir,
destDir,
new FileNameMapper(){
public void setFrom(String from){}
public void setTo(String to){}
public String[] mapFileName(String fileName){
try {
String extension = getExt(fileName);
if (extension == null) return null;
return new String[]{
Compiler.nameFile(fileName, extension)
};
} catch (Exception x){
System.err.println(x.getMessage());
return null;
}
}
}
);
try {
if (newFiles.length > 0){
System.out.println("Compiling " + newFiles.length + " BTE template files.");
}
for (int i = 0; i < newFiles.length; i++){
String extension = getExt(newFiles[i]);
if (extension != null){
File destFile = new File(destDir, Compiler.nameFile(newFiles[i], extension));
File destParent = destFile.getParentFile();
if (destParent != null && !destParent.exists()){
if (!destParent.mkdirs()){
throw new BuildException("could not create directory: " + destParent);
}
}
Compiler compiler = new Compiler();
compiler.compileURL(new File(newFiles[i]).toURL());
}
}
} catch (Exception x){
throw new BuildException(x);
}
}
private static String getExt(String fileName) throws Exception {
String extension = "html";
File f = new File(fileName);
Lexer lex = new Lexer(new FileReader(f));
boolean foundDoc = false;
Symbol s;
while (!foundDoc && (s = lex.next_token()).sym != sym.EOF){
Token t = (Token)(s.value);
switch (t.getID()){
case sym.WHITESPACE:{
} break;
case sym.START_DOCUMENT:{
BTEFile bteFile = new BTEFile(t);
if (!bteFile.output) return null;
if (bteFile.fileExt != null){
extension = bteFile.fileExt;
}
foundDoc = true;
} break;
default:{
foundDoc = true;
} break;
}
}
return extension;
}
}