📄 processor.java
字号:
return bos.toByteArray();
}
/*
* (non-Javadoc)
*
* @see java.util.Observer#update(java.util.Observable, java.lang.Object)
*/
protected void update(final Object arg, final int n) {
if (arg instanceof Throwable) {
((Throwable) arg).printStackTrace();
} else {
if (n % 100 == 0) {
System.err.println(n + " " + arg);
}
}
}
public static void main(final String[] args) throws Exception {
if (args.length < 2) {
showUsage();
return;
}
int inRepresentation = getRepresentation(args[0]);
int outRepresentation = getRepresentation(args[1]);
InputStream is = System.in;
OutputStream os = new BufferedOutputStream(System.out);
Source xslt = null;
// boolean computeMax = true;
for (int i = 2; i < args.length; i++) {
if ("-in".equals(args[i])) {
is = new FileInputStream(args[++i]);
} else if ("-out".equals(args[i])) {
os = new BufferedOutputStream(new FileOutputStream(args[++i]));
} else if ("-xslt".equals(args[i])) {
xslt = new StreamSource(new FileInputStream(args[++i]));
// } else if( "-computemax".equals( args[ i].toLowerCase())) {
// computeMax = true;
} else {
showUsage();
return;
}
}
if (inRepresentation == 0 || outRepresentation == 0) {
showUsage();
return;
}
Processor m = new Processor(inRepresentation,
outRepresentation,
is,
os,
xslt);
long l1 = System.currentTimeMillis();
int n = m.process();
long l2 = System.currentTimeMillis();
System.err.println(n);
System.err.println("" + (l2 - l1) + "ms " + 1000f * n / (l2 - l1)
+ " resources/sec");
}
private static int getRepresentation(final String s) {
if ("code".equals(s)) {
return BYTECODE;
} else if ("xml".equals(s)) {
return MULTI_XML;
} else if ("singlexml".equals(s)) {
return SINGLE_XML;
}
return 0;
}
private static void showUsage() {
System.err.println("Usage: Main <in format> <out format> [-in <input jar>] [-out <output jar>] [-xslt <xslt fiel>]");
System.err.println(" when -in or -out is omitted sysin and sysout would be used");
System.err.println(" <in format> and <out format> - code | xml | singlexml");
}
/**
* IputStream wrapper class used to protect input streams from being closed
* by some stupid XML parsers.
*/
private static final class ProtectedInputStream extends InputStream {
private final InputStream is;
private ProtectedInputStream(final InputStream is) {
super();
this.is = is;
}
public final void close() throws IOException {
}
public final int read() throws IOException {
return is.read();
}
public final int read(final byte[] b, final int off, final int len)
throws IOException
{
return is.read(b, off, len);
}
public final int available() throws IOException {
return is.available();
}
}
/**
* A {@link ContentHandlerFactory ContentHandlerFactory} is used to create
* {@link org.xml.sax.ContentHandler ContentHandler} instances for concrete
* context.
*/
private static interface ContentHandlerFactory {
/**
* Creates an instance of the content handler.
*
* @return content handler
*/
ContentHandler createContentHandler();
}
/**
* SAXWriterFactory
*/
private static final class SAXWriterFactory implements
ContentHandlerFactory
{
private Writer w;
private boolean optimizeEmptyElements;
public SAXWriterFactory(
final Writer w,
final boolean optimizeEmptyElements)
{
this.w = w;
this.optimizeEmptyElements = optimizeEmptyElements;
}
public final ContentHandler createContentHandler() {
return new SAXWriter(w, optimizeEmptyElements);
}
}
/**
* ASMContentHandlerFactory
*/
private static final class ASMContentHandlerFactory implements
ContentHandlerFactory
{
private OutputStream os;
private boolean computeMax;
public ASMContentHandlerFactory(
final OutputStream os,
final boolean computeMax)
{
this.os = os;
this.computeMax = computeMax;
}
public final ContentHandler createContentHandler() {
return new ASMContentHandler(os, computeMax);
}
}
/**
* TransformerHandlerFactory
*/
private static final class TransformerHandlerFactory implements
ContentHandlerFactory
{
private SAXTransformerFactory saxtf;
private Templates templates;
private ContentHandler outputHandler;
public TransformerHandlerFactory(
final SAXTransformerFactory saxtf,
final Templates templates,
final ContentHandler outputHandler)
{
this.saxtf = saxtf;
this.templates = templates;
this.outputHandler = outputHandler;
}
public final ContentHandler createContentHandler() {
try {
TransformerHandler handler = saxtf.newTransformerHandler(templates);
handler.setResult(new SAXResult(outputHandler));
return handler;
} catch (TransformerConfigurationException ex) {
throw new RuntimeException(ex.toString());
}
}
}
/**
* SubdocumentHandlerFactory
*/
private final static class SubdocumentHandlerFactory implements
ContentHandlerFactory
{
private ContentHandler subdocumentHandler;
public SubdocumentHandlerFactory(final ContentHandler subdocumentHandler)
{
this.subdocumentHandler = subdocumentHandler;
}
public final ContentHandler createContentHandler() {
return subdocumentHandler;
}
}
/**
* A {@link org.xml.sax.ContentHandler ContentHandler} and
* {@link org.xml.sax.ext.LexicalHandler LexicalHandler} that serializes XML
* from SAX 2.0 events into {@link java.io.Writer Writer}.
*
* <i><blockquote> This implementation does not support namespaces, entity
* definitions (uncluding DTD), CDATA and text elements. </blockquote></i>
*/
private final static class SAXWriter extends DefaultHandler implements
LexicalHandler
{
private static final char[] OFF = " ".toCharArray();
private Writer w;
private boolean optimizeEmptyElements;
private boolean openElement = false;
private int ident = 0;
/**
* Creates <code>SAXWriter</code>.
*
* @param w writer
* @param optimizeEmptyElements if set to <code>true</code>, short
* XML syntax will be used for empty elements
*/
public SAXWriter(final Writer w, final boolean optimizeEmptyElements) {
this.w = w;
this.optimizeEmptyElements = optimizeEmptyElements;
}
public final void startElement(
final String ns,
final String localName,
final String qName,
final Attributes atts) throws SAXException
{
try {
closeElement();
writeIdent();
w.write("<".concat(qName));
if (atts != null && atts.getLength() > 0) {
writeAttributes(atts);
}
if (!optimizeEmptyElements) {
w.write(">\n");
} else {
openElement = true;
}
ident += 2;
} catch (IOException ex) {
throw new SAXException(ex);
}
}
public final void endElement(
final String ns,
final String localName,
final String qName) throws SAXException
{
ident -= 2;
try {
if (openElement) {
w.write("/>\n");
openElement = false;
} else {
writeIdent();
w.write("</" + qName + ">\n");
}
} catch (IOException ex) {
throw new SAXException(ex);
}
}
public final void endDocument() throws SAXException {
try {
w.flush();
} catch (IOException ex) {
throw new SAXException(ex);
}
}
public final void comment(final char[] ch, final int off, final int len)
throws SAXException
{
try {
closeElement();
writeIdent();
w.write("<!-- ");
w.write(ch, off, len);
w.write(" -->\n");
} catch (IOException ex) {
throw new SAXException(ex);
}
}
public final void startDTD(
final String arg0,
final String arg1,
final String arg2) throws SAXException
{
}
public final void endDTD() throws SAXException {
}
public final void startEntity(final String arg0) throws SAXException {
}
public final void endEntity(final String arg0) throws SAXException {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -