exportusagetask.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 191 行
JAVA
191 行
/*
* $Id: ExportUsageTask.java,v 1.1 2003/11/25 11:54:02 epr Exp $
*/
package org.jnode.shell.help.ant;
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Path;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import org.jnode.build.AbstractPluginsTask;
import org.jnode.plugin.ConfigurationElement;
import org.jnode.plugin.Extension;
import org.jnode.plugin.ExtensionPoint;
import org.jnode.plugin.PluginRegistry;
import org.jnode.shell.alias.AliasManager;
import org.jnode.shell.help.Argument;
import org.jnode.shell.help.Help;
import org.jnode.shell.help.Parameter;
import org.jnode.shell.help.Syntax;
/**
* Task used to export usage information from a Main class.
* @author epr
*/
public class ExportUsageTask extends AbstractPluginsTask {
private File destFile;
private Path classPath;
/**
* Execute this task
* @throws BuildException
*/
public void execute() throws BuildException {
final Document doc = DocumentHelper.createDocument();
final Element root = doc.addElement("s:shell");
root.addNamespace("s", "http://jnode.org/xsl/shell");
final ClassLoader loader = new AntClassLoader(getClass().getClassLoader(), getProject(), classPath, true);
try {
final PluginRegistry piRegistry = getPluginRegistry();
final ExtensionPoint ep = piRegistry.getExtensionPoint(AliasManager.ALIASES_EP_NAME);
final Extension[] extensions = ep.getExtensions();
for (int i = 0; i < extensions.length; i++) {
final Extension ext = extensions[i];
final ConfigurationElement[] elements = ext.getConfigurationElements();
for (int j = 0; j < elements.length; j++) {
String className = elements[j].getAttribute("class");
try {
final Class cls = loader.loadClass(className);
final Field infoField = cls.getField(Help.INFO_FIELD_NAME);
final Help.Info info = (Help.Info)infoField.get(null);
new XmlHelp(info).bind(root);
} catch (ClassCastException ex) {
ex.printStackTrace();
throw new BuildException(ex);
} catch (ClassNotFoundException ex) {
throw new BuildException(ex);
} catch (NoSuchFieldException ex) {
log("No INFO field in class " + className, Project.MSG_INFO);
} catch (IllegalAccessException ex) {
throw new BuildException(ex);
}
}
}
final XMLWriter xmlOut = new XMLWriter(new FileWriter(destFile));
xmlOut.write(doc);
xmlOut.flush();
xmlOut.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new BuildException(ex);
}
}
/**
* Gets the destination file
*/
public File getDestFile() {
return destFile;
}
/**
* Sets the destination file
* @param file
*/
public void setDestFile(File file) {
destFile = file;
}
public Path createClasspath() {
return classPath.createPath();
}
private class XmlHelp extends Help {
Element doc = null;
Element s = null; // syntax element
Info info;
XmlHelp(Info info) {
this.info = info;
}
void bind(Element root) {
doc = root.addElement("s:command");
doc.addAttribute("name", info.getName());
help(info);
}
/**
* Shows the complete help for a command.
*/
public void help(Info info) {
final Syntax[] syntaxes = info.getSyntaxes();
for (int i = 0; i < syntaxes.length; i++)
help(info.getName(), syntaxes[i]);
}
/**
* Shows the help for a command syntax.
*/
public void help(String name, Syntax syntax) {
usage(name, syntax);
final Parameter[] params = syntax.getParams();
for (int i = 0; i < params.length; i++)
params[i].describe(this);
}
/**
* Shows the usage information of a command.
*/
public void usage(Info info) {
final Syntax[] syntaxes = info.getSyntaxes();
for (int i = 0; i < syntaxes.length; i++)
usage(info.getName(), syntaxes[i]);
}
/**
* Shows the usage information of a command.
*/
public synchronized void usage(String name, Syntax syntax) {
s = doc.addElement("s:syntax");
String line = name;
final Parameter[] params = syntax.getParams();
for (int i = 0; i < params.length; i++)
line += " " + params[i].format();
s.addElement("s:usage").addText(line);
s.addElement("s:description").addText(syntax.getDescription());
}
public synchronized void describeParameter(Parameter param) {
final Element p = s.addElement("s:parameter");
p.addElement("s:name").addText(param.getName());
p.addElement("s:description").addText(param.getDescription());
}
public synchronized void describeArgument(Argument arg) {
final Element a = s.addElement("s:argument");
a.addElement("s:name").addText(arg.getName());
a.addElement("s:description").addText(arg.getDescription());
}
}
/**
* @see org.apache.tools.ant.Task#init()
*/
public void init() throws BuildException {
super.init();
classPath = new Path(getProject());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?