Processorpublic class Processor extends Object Processor is a command line tool that can be used for
bytecode waving directed by XSL transformation.
In order to use a concrete XSLT engine, system property
javax.xml.transform.TransformerFactory must be set to
one of the following values.
jd.xslt |
jd.xml.xslt.trax.TransformerFactoryImpl |
Saxon |
net.sf.saxon.TransformerFactoryImpl |
Caucho |
com.caucho.xsl.Xsl |
Xalan interpeter |
org.apache.xalan.processor.TransformerFactory |
Xalan xsltc |
org.apache.xalan.xsltc.trax.TransformerFactoryImpl |
|
Fields Summary |
---|
public static final int | BYTECODE | public static final int | MULTI_XML | public static final int | SINGLE_XML | private static final String | SINGLE_XML_NAME | private int | inRepresentation | private int | outRepresentation | private InputStream | input | private OutputStream | output | private Source | xslt | private boolean | computeMax | private int | n |
Constructors Summary |
---|
public Processor(int inRepresenation, int outRepresentation, InputStream input, OutputStream output, Source xslt)
this.inRepresentation = inRepresenation;
this.outRepresentation = outRepresentation;
this.input = input;
this.output = output;
this.xslt = xslt;
this.computeMax = true;
|
Methods Summary |
---|
private void | copyEntry(java.io.InputStream is, java.io.OutputStream os)
if( outRepresentation==SINGLE_XML) return;
byte[] buff = new byte[2048];
int n;
while ((n = is.read(buff)) != -1) {
os.write(buff, 0, n);
}
| private oracle.toplink.libraries.asm.xml.Processor$EntryElement | getEntryElement(java.util.zip.ZipOutputStream zos)
if( outRepresentation==SINGLE_XML) {
return new SingleDocElement( zos);
} else {
return new ZipEntryElement( zos);
}
| private java.lang.String | getName(java.util.zip.ZipEntry ze)
String name = ze.getName();
if( isClassEntry( ze)) {
if( inRepresentation!=BYTECODE && outRepresentation==BYTECODE) {
name = name.substring( 0, name.length()-4); // .class.xml to .class
} else if( inRepresentation==BYTECODE && outRepresentation!=BYTECODE) {
name = name.concat( ".xml"); // .class to .class.xml
}
// } else if( CODE2ASM.equals( command)) {
// name = name.substring( 0, name.length()-6).concat( ".asm");
}
return name;
| private static int | getRepresentation(java.lang.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 boolean | isClassEntry(java.util.zip.ZipEntry ze)
String name = ze.getName();
return inRepresentation==SINGLE_XML && name.equals( SINGLE_XML_NAME) ||
name.endsWith( ".class") || name.endsWith( ".class.xml");
| public static void | main(java.lang.String[] args)
if( args.length<2) {
showUsage();
return;
}
int inRepresentation = getRepresentation( args[ 0]);
int outRepresentation = getRepresentation( args[ 1]);
InputStream is = null;
OutputStream os = null;
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( is==null || os==null || 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))+" files/sec");
| public int | process()
ZipInputStream zis = new ZipInputStream( input);
final ZipOutputStream zos = new ZipOutputStream( output);
final OutputStreamWriter osw = new OutputStreamWriter( zos);
Thread.currentThread().setContextClassLoader( getClass().getClassLoader());
TransformerFactory tf = TransformerFactory.newInstance();
if( !tf.getFeature( SAXSource.FEATURE) || !tf.getFeature( SAXResult.FEATURE)) return 0;
SAXTransformerFactory saxtf = ( SAXTransformerFactory) tf;
Templates templates = null;
if( xslt!=null) {
templates = saxtf.newTemplates( xslt);
}
// configuring outHandlerFactory ///////////////////////////////////////////////////////
EntryElement entryElement = getEntryElement( zos);
ContentHandler outDocHandler = null;
switch( outRepresentation) {
case BYTECODE:
outDocHandler = new OutputSlicingHandler( new ASMContentHandlerFactory( zos, computeMax), entryElement, false);
break;
case MULTI_XML:
outDocHandler = new OutputSlicingHandler( new SAXWriterFactory( osw, true), entryElement, true);
break;
case SINGLE_XML:
ZipEntry outputEntry = new ZipEntry( SINGLE_XML_NAME);
zos.putNextEntry( outputEntry);
outDocHandler = new SAXWriter( osw, false);
break;
}
// configuring inputDocHandlerFactory /////////////////////////////////////////////////
ContentHandler inDocHandler = null;
if( templates==null) {
inDocHandler = outDocHandler;
} else {
inDocHandler = new InputSlicingHandler( "class", outDocHandler,
new TransformerHandlerFactory( saxtf, templates, outDocHandler));
}
ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory( inDocHandler);
if( inDocHandler!=null && inRepresentation!=SINGLE_XML) {
inDocHandler.startDocument();
inDocHandler.startElement( "", "classes", "classes", new AttributesImpl());
}
int n = 0;
ZipEntry ze = null;
while(( ze = zis.getNextEntry())!=null) {
if( isClassEntry( ze)) {
processEntry( zis, ze, inDocHandlerFactory);
} else {
OutputStream os = entryElement.openEntry( getName( ze));
copyEntry( zis, os);
entryElement.closeEntry();
}
n++;
update( ze.getName());
}
if( inDocHandler!=null && inRepresentation!=SINGLE_XML) {
inDocHandler.endElement( "", "classes", "classes");
inDocHandler.endDocument();
}
if( outRepresentation==SINGLE_XML) {
zos.closeEntry();
}
zos.flush();
zos.close();
return n;
| private void | processEntry(java.util.zip.ZipInputStream zis, java.util.zip.ZipEntry ze, oracle.toplink.libraries.asm.xml.Processor$ContentHandlerFactory handlerFactory)
ContentHandler handler = handlerFactory.createContentHandler();
try {
/*
if( CODE2ASM.equals( command)) {
// read bytecode and process it with TraceClassVisitor
ClassReader cr = new ClassReader( readEntry( zis, ze));
cr.accept( new TraceClassVisitor( null, new PrintWriter( os)), false);
*/
boolean singleInputDocument = inRepresentation==SINGLE_XML;
if( inRepresentation==BYTECODE) { // read bytecode and process it with handler
ClassReader cr = new ClassReader( readEntry( zis, ze));
cr.accept( new SAXClassAdapter( handler, singleInputDocument), false);
} else { // read XML and process it with handler
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler( handler);
reader.parse( new InputSource( singleInputDocument ?
( InputStream) new ProtectedInputStream( zis) : new ByteArrayInputStream( readEntry( zis, ze))));
}
} catch( Exception ex) {
update( ze.getName());
update( ex);
}
| private byte[] | readEntry(java.util.zip.ZipInputStream zis, java.util.zip.ZipEntry ze)
long size = ze.getSize();
if (size > -1) {
byte[] buff = new byte[(int) size];
// int k = 0;
// while(( n = zis.read(buff, k, buff.length-k)) > 0) {
// k += n;
// }
zis.read(buff);
return buff;
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buff = new byte[4096];
int n;
while(( n = zis.read(buff)) != -1) {
bos.write(buff, 0, n);
}
return bos.toByteArray();
}
| 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( " <in format> and <out format> - code | xml | singlexml");
| public void | update(java.lang.Object arg)
if( arg instanceof Throwable) {
(( Throwable) arg).printStackTrace();
} else {
if(( n % 100)==0) {
System.err.println( n+" "+arg);
}
}
n++;
|
|