📄 jacsontask.java
字号:
package de.spieleck.app.jacson.ant;
import de.spieleck.app.jacson.Jacson;
import de.spieleck.app.jacson.JacsonChunkSource;
import de.spieleck.app.jacson.JacsonConfigException;
import de.spieleck.app.jacson.JacsonException;
import de.spieleck.app.jacson.JacsonNames;
import de.spieleck.app.jacson.JacsonPrintReport;
import de.spieleck.app.jacson.JacsonState;
import de.spieleck.app.jacson.Version;
import de.spieleck.app.jacson.source.LineChunkSource;
import de.spieleck.config.ConfigNode;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.util.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
/**
* Jacson processing from within an Ant task.
*
* <p>
* This contains pieces of code taken from Ant's XSLTProcess.
* </p>
* <p>
* In general I though Ant would support the TaskWriter even
* more. But it wasn't too bad.
* </p>
* XXX Make the JacsonSource configurable, currently it is LineChunkSource.
* @author fsn
*
*/
public class JacsonTask
extends MatchingTask
implements JacsonNames
{
/** where to find the source files, default is the project's basedir */
private File baseDir = null;
/** Configuration */
private String configName = null;
/** additional parameters to be passed to the stylesheets */
private List params = new ArrayList();
/** Input file to be used */
private File inFile = null;
/** force output of target files even if they already exist */
private boolean force = false;
/** Utilities used for file operations */
private FileUtils fileUtils;
/** The file to write to */
private File outFile = null;
/** Whether to style all files in the included directories as well. */
// This seems to cause traversal of the whole subtree of the project???
private boolean performDirectoryScan = false;
/**
* Creates a new JacsonTask Task.
*/
public JacsonTask() {
fileUtils = FileUtils.newFileUtils();
} //-- JacsonTask
/**
* Whether to style all files in the included directories as well;
* optional, default is true.
*
* @param b true if files in included directories are processed.
*/
public void setScanIncludedDirectories(boolean b) {
performDirectoryScan = b;
}
/**
* go ant hunt down a configuration.
*/
public ConfigNode obtainConfig(JacsonState js)
throws BuildException
{
if (configName == null)
{
throw new BuildException("no configName specified", location);
}
File configFile = project.resolveFile(configName);
if (!configFile.exists())
throw new BuildException("Cannot find config "+configName,location);
ConfigNode config = null;
try
{
config = Jacson.obtainConfig(configFile.getCanonicalPath(), js);
}
catch ( IOException e)
{
throw new BuildException(e,location);
}
catch ( JacsonConfigException e)
{
throw new BuildException(e,location);
}
return config;
}
/**
* Executes the task.
*
* @exception BuildException if there is a problem.
*/
public void execute()
throws BuildException
{
JacsonState js = new ProjectJacsonState(project, params);
ConfigNode config = obtainConfig(js);
try
{
Jacson stat = new Jacson(config, js);
JacsonPrintReport jr = stat.getReport();
if ( outFile != null )
jr.setOutputStream(new FileOutputStream(outFile));
jr.begin(JS_REP_PROC);
long t1 = System.currentTimeMillis();
if ( inFile != null )
{
process(inFile, stat);
}
else
{
File usedBaseDir = baseDir == null
? project.resolveFile(".")
: baseDir ;
DirectoryScanner scanner = getDirectoryScanner(usedBaseDir);
String[] list = scanner.getIncludedFiles();
for (int i = 0; i < list.length; ++i)
process(usedBaseDir, list[i], stat);
if (performDirectoryScan) {
String[] dirs = scanner.getIncludedDirectories();
for (int j = 0; j < dirs.length; ++j){
list = new File(usedBaseDir, dirs[j]).list();
for (int i = 0; i < list.length; ++i)
process(usedBaseDir, list[i], stat);
}
}
}
long dt = System.currentTimeMillis() - t1;
jr.report(JS_REP_TIME, ""+dt);
jr.end();
stat.summary();
jr.finish();
}
catch (Exception e)
{
throw new BuildException(e);
}
}
/**
* Set whether to check dependencies, or always generate;
* optional, default is false.
*
* @param force true if always generate.
*/
public void setForce(boolean force) {
this.force = force;
}
/**
* Set the base directory;
* optional, default is the project's basedir.
*
* @param dir the base directory
**/
public void setBasedir(File dir) {
baseDir = dir;
}
/**
* Name of the configFile to use - given either relative
* to the project's basedir or as an absolute path; required.
*
* @param configName the configFile to use
*/
public void setConfig(String configName) {
this.configName = configName;
}
/**
* Specifies a single input file.
*
* @param inFile the input file
*/
public void setIn(File inFile){
this.inFile = inFile;
}
/**
* Specifies an files where the reports go to.
*
* @param outFile the output file
*/
public void setOut(File outFile){
this.outFile = outFile;
}
/**
* Processes the given input XML file and stores the result
* in the given resultFile.
*
* @param baseDir the base directory for resolving files.
* @param name the input file
* @param stat the Jacson to use.
* @exception BuildException if the processing fails.
*/
private void process(File baseDir, String name, Jacson stat)
throws JacsonException, IOException
{
process(new File(baseDir, name), stat);
}
private void process(File input, Jacson stat)
throws JacsonException, IOException
{
if ( input.isDirectory() )
return;
try
{
String name = input.getCanonicalPath();
log("processing "+name);
JacsonChunkSource ch = new LineChunkSource(name, true);
stat.run(ch);
}
catch ( IOException e )
{
e.printStackTrace(System.out);
}
}
/**
* Create an instance of an parameter for configuration.
*
* @return an instance of the Param class to be configured.
*/
public JacsonTaskParam createParam() {
JacsonTaskParam p = new JacsonTaskParam();
params.add(p);
return p;
}
/**
*
*/
public void init()
throws BuildException
{
super.init();
project.log("Jacson Ant Task "+Version.BUILD_STAMP);
}
} //-- JacsonTask
//
// Jacson - Text Filtering with Java.
// Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -