📄 flowjavasourcegenerator.java
字号:
/*
* Copyright (c) 2003, Alexander Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Flow4J-Eclipse project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.orthanc.flow4j.tools.ant;
import java.io.File;
import net.orthanc.flow4j.base.PrettyPrinterUtils;
import net.orthanc.flow4j.model.codegen.javasrc.Consts;
import net.orthanc.flow4j.model.codegen.javasrc.JavaSrcModelDigester;
import org.acm.seguin.util.FileSettings;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;
/**
* <h3>Description</h3>
* <p>Genarates the java source code of a flow4j model</p>
*
<p>
The set of files to be transformed can be refined with the
<i>includes</i>, <i>includesfile</i>, <i>excludes</i>,
<i>excludesfile</i> and <i>defaultexcludes</i>
attributes. Patterns provided through the <i>includes</i> or
<i>includesfile</i> attributes specify files to be
included. Patterns provided through the <i>exclude</i> or
<i>excludesfile</i> attribute specify files to be
excluded. Additionally, default exclusions can be specified with
the <i>defaultexcludes</i> attribute. Look at the ant
documentation for details of file inclusion/exclusion patterns
and their usage.
</p>
<p>This task forms an implicit <a href="../CoreTypes/fileset.html">FileSet</a> and
supports all attributes of <code><fileset></code>
(<code>dir</code> becomes <code>srcdir</code>) as well as the nested
<code><include></code>, <code><exclude></code> and
<code><patternset></code> elements.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">srcDir</td>
<td valign="top">Where to find the files to be fixed up.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">destDir</td>
<td valign="top">Where to place the corrected files. Defaults to
srcDir (replacing the original file)</td>
<td valign="top" align="center">No</td>
</tr>
<!--tr>
<td valign="top">mappingFile</td>
<td valign="top">Where to find the castor mapping file</td>
<td valign="top" align="center">Yes</td>
</tr-->
<tr>
<td valign="top">includes</td>
<td valign="top">comma- or space-separated list of patterns of files that must be
included. All files are included when omitted.</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">includesfile</td>
<td valign="top">the name of a file. Each line of this file is
taken to be an include pattern</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">excludes</td>
<td valign="top">comma- or space-separated list of patterns of files that must be
excluded. No files (except default excludes) are excluded when omitted.</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">excludesfile</td>
<td valign="top">the name of a file. Each line of this file is
taken to be an exclude pattern</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">defaultexcludes</td>
<td valign="top">indicates whether default excludes should be used or not
("yes"/"no"). Default excludes are used when omitted.</td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Example</h3>
<pre>
<target name="generate-flow-java-src" depends="compile-plugin">
<taskdef name="flow2java" classname="net.orthanc.flow4j.tools.ant.FlowJavaSourceGenerator">
<classpath>
<pathelement path="${build.dir}"/>
<fileset dir="lib">
<include name="** / *.jar"/>
</fileset>
<fileset dir="${eclipse.plugins}">
<include name="org.apache.xerces* /*.jar"/>
<include name="org.eclipse.jdt.core* /*.jar"/>
<include name="org.eclipse.core.runtime* /*.jar"/>
<include name="org.eclipse.core.resources* /*.jar"/>
</fileset>
</classpath>
</taskdef>
<flow2java destDir="${src.dir}"
srcDir="${src.dir}"
mappingFile="${src.dir}/mapping.xml"
includes="** /*.f4j">
</flow2java>
</target>
</pre>
* @author agreif
*/
public class FlowJavaSourceGenerator extends MatchingTask {
private File destDir = null;
private File srcDir;
// private File mappingFile;
private File prettySettingsFile;
private File scriptRootFolder;
/**
* Set the source dir to find the source text files.
*/
public void setSrcdir(File srcDir) {
this.srcDir = srcDir;
}
/**
* 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;
}
/**
* The location of the castor mapping file
* @param mappingFile
*/
// public void setMappingFile(File mappingFile) {
// this.mappingFile = mappingFile;
// }
/**
* The location of the pretty printer settings file
* @param mappingFile
*/
public void setPrettySettingsFile(File settingsFile) {
this.prettySettingsFile = settingsFile;
}
/**
* @param file
*/
public void setScriptRootFolder(File file) {
scriptRootFolder = file;
}
/**
* Transforms the flow model file into java source code
* @param file the flow model file
* @throws BuildException
*/
private void processFile(String file) throws BuildException {
File srcFile = new File(srcDir, file);
// File destD = destDir == null ? srcDir : destDir;
System.out.println("Generate java source of flow: " + file);
File relFile = new File(file);
String packagePath =
relFile.getParentFile().toString().replace('/', '.').replace('\\', '.');
if (packagePath.startsWith("."))
packagePath = packagePath.substring(1);
if (packagePath.endsWith("."))
packagePath = packagePath.substring(0, packagePath.length());
try {
//castor FlowModelBind.loadMapping(mappingFile);
FileSettings prettySettings = PrettyPrinterUtils.loadPrettySettings(prettySettingsFile);
//JavaSrcModelDigester.setPrettySettings(prettySettings);
//JavaSrcModelDigester.generateCode(srcFile, packagePath, scriptRootFolder, destDir);
File outputFolder = new File(destDir, packagePath.replace('.', File.separatorChar));
String fileName = srcFile.getName();
fileName = fileName.substring(0, fileName.lastIndexOf(".")+1) + Consts.FILE_EXTENSION_JAVA;
File outputFile = new File(outputFolder, fileName);
JavaSrcModelDigester.setPrettySettings(prettySettings);
JavaSrcModelDigester.generateCode(srcFile, packagePath, destDir, scriptRootFolder);
} catch (Exception e) {
throw new BuildException(e);
}
}
/**
* Executes the task.
*/
public void execute() throws BuildException {
// first off, make sure that we've got a srcdir and destdir
if (srcDir == null) {
throw new BuildException("srcdir attribute must be set!");
}
if (!srcDir.exists()) {
throw new BuildException("srcdir does not exist!");
}
if (!srcDir.isDirectory()) {
throw new BuildException("srcdir is not a directory!");
}
if (destDir != null) {
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();
log("destDir: " + destDir, Project.MSG_VERBOSE);
log("srcDir: " + srcDir, Project.MSG_VERBOSE);
//castor log("mappingFile: " + mappingFile, Project.MSG_VERBOSE);
for (int i = 0; i < files.length; i++) {
processFile(files[i]);
}
}
/* static public void main(String[] args) {
FlowJavaSourceGenerator gen = new FlowJavaSourceGenerator();
gen.setDestDir(new File("/Users/agreif/project/flow4j/eclipse/net.orthanc.flow4j/src"));
gen.setFlowFile(new File("/Users/agreif/project/flow4j/eclipse/net.orthanc.flow4j/src/net/orthanc/flow4j/flows/FlowFileUpdate.f4j"));
gen.setMappingFile(new File("/Users/agreif/project/flow4j/eclipse/net.orthanc.flow4j/src/mapping.xml"));
gen.setPackagePath("net.orthanc.flow4j.flows");
gen.execute();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -