📄 anakiatask.java
字号:
Writer writer = null;
try
{
// the current input file relative to the baseDir
inFile = new File(baseDir,xmlFile);
// the output file relative to basedir
outFile = new File(destDir,
xmlFile.substring(0,
xmlFile.lastIndexOf('.')) + extension);
// only process files that have changed
if (lastModifiedCheck == false ||
(inFile.lastModified() > outFile.lastModified() ||
styleSheetLastModified > outFile.lastModified() ||
projectFileLastModified > outFile.lastModified() ||
userContextsModifed(outFile.lastModified())))
{
ensureDirectoryFor( outFile );
//-- command line status
log("Input: " + xmlFile, Project.MSG_INFO );
// Build the JDOM Document
Document root = builder.build(inFile);
// Shove things into the Context
VelocityContext context = new VelocityContext();
/*
* get the property TEMPLATE_ENCODING
* we know it's a string...
*/
String encoding = (String) ve.getProperty( RuntimeConstants.OUTPUT_ENCODING );
if (encoding == null || encoding.length() == 0
|| encoding.equals("8859-1") || encoding.equals("8859_1"))
{
encoding = "ISO-8859-1";
}
Format f = Format.getRawFormat();
f.setEncoding(encoding);
OutputWrapper ow = new OutputWrapper(f);
context.put ("root", root.getRootElement());
context.put ("xmlout", ow );
context.put ("relativePath", getRelativePath(xmlFile));
context.put ("treeWalk", new TreeWalker());
context.put ("xpath", new XPathTool() );
context.put ("escape", new Escape() );
context.put ("date", new java.util.Date() );
/**
* only put this into the context if it exists.
*/
if (projectDocument != null)
{
context.put ("project", projectDocument.getRootElement());
}
/**
* Add the user subcontexts to the to context
*/
for (Iterator iter = contexts.iterator(); iter.hasNext();)
{
Context subContext = (Context) iter.next();
if (subContext == null)
{
throw new BuildException("Found an undefined SubContext!");
}
if (subContext.getContextDocument() == null)
{
throw new BuildException("Could not build a subContext for " + subContext.getName());
}
context.put(subContext.getName(), subContext
.getContextDocument().getRootElement());
}
/**
* Process the VSL template with the context and write out
* the result as the outFile.
*/
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile),
encoding));
/**
* get the template to process
*/
Template template = ve.getTemplate(style);
template.merge(context, writer);
log("Output: " + outFile, Project.MSG_INFO );
}
}
catch (JDOMException e)
{
outFile.delete();
if (e.getCause() != null)
{
Throwable rootCause = e.getCause();
if (rootCause instanceof SAXParseException)
{
System.out.println("");
System.out.println("Error: " + rootCause.getMessage());
System.out.println(
" Line: " +
((SAXParseException)rootCause).getLineNumber() +
" Column: " +
((SAXParseException)rootCause).getColumnNumber());
System.out.println("");
}
else
{
rootCause.printStackTrace();
}
}
else
{
e.printStackTrace();
}
}
catch (Throwable e)
{
if (outFile != null)
{
outFile.delete();
}
e.printStackTrace();
}
finally
{
if (writer != null)
{
try
{
writer.flush();
}
catch (IOException e)
{
// Do nothing
}
try
{
writer.close();
}
catch (IOException e)
{
// Do nothing
}
}
}
}
/**
* Hacky method to figure out the relative path
* that we are currently in. This is good for getting
* the relative path for images and anchor's.
*/
private String getRelativePath(String file)
{
if (file == null || file.length()==0)
return "";
StringTokenizer st = new StringTokenizer(file, "/\\");
// needs to be -1 cause ST returns 1 even if there are no matches. huh?
int slashCount = st.countTokens() - 1;
StringBuffer sb = new StringBuffer();
for (int i=0;i<slashCount ;i++ )
{
sb.append ("../");
}
if (sb.toString().length() > 0)
{
return StringUtils.chop(sb.toString(), 1);
}
return ".";
}
/**
* create directories as needed
*/
private void ensureDirectoryFor( File targetFile ) throws BuildException
{
File directory = new File( targetFile.getParent() );
if (!directory.exists())
{
if (!directory.mkdirs())
{
throw new BuildException("Unable to create directory: "
+ directory.getAbsolutePath() );
}
}
}
/**
* Check to see if user context is modified.
*/
private boolean userContextsModifed(long lastModified)
{
for (Iterator iter = contexts.iterator(); iter.hasNext();)
{
AnakiaTask.Context ctx = (AnakiaTask.Context) iter.next();
if(ctx.getLastModified() > lastModified)
{
return true;
}
}
return false;
}
/**
* Create a new context.
* @return A new context.
*/
public Context createContext()
{
Context context = new Context();
contexts.add(context);
return context;
}
/**
* A context implementation that loads all values from an XML file.
*/
public class Context
{
private String name;
private Document contextDoc = null;
private String file;
/**
* Public constructor.
*/
public Context()
{
}
/**
* Get the name of the context.
* @return The name of the context.
*/
public String getName()
{
return name;
}
/**
* Set the name of the context.
* @param name
*
* @throws IllegalArgumentException if a reserved word is used as a
* name, specifically any of "relativePath", "treeWalk", "xpath",
* "escape", "date", or "project"
*/
public void setName(String name)
{
if (name.equals("relativePath") ||
name.equals("treeWalk") ||
name.equals("xpath") ||
name.equals("escape") ||
name.equals("date") ||
name.equals("project"))
{
throw new IllegalArgumentException("Context name '" + name
+ "' is reserved by Anakia");
}
this.name = name;
}
/**
* Build the context based on a file path.
* @param file
*/
public void setFile(String file)
{
this.file = file;
}
/**
* Retrieve the time the source file was last modified.
* @return The time the source file was last modified.
*/
public long getLastModified()
{
return new File(baseDir, file).lastModified();
}
/**
* Retrieve the context document object.
* @return The context document object.
*/
public Document getContextDocument()
{
if (contextDoc == null)
{
File contextFile = new File(baseDir, file);
try
{
contextDoc = builder.build(contextFile);
}
catch (Exception e)
{
throw new BuildException(e);
}
}
return contextDoc;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -