FileDocCategorySizeDatePackage
Processor.javaAPI DocGlassfish v2 API29126Thu Mar 02 11:51:20 GMT 2006oracle.toplink.libraries.asm.xml

Processor

public 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
author
Eugene Kuleshov

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 voidcopyEntry(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$EntryElementgetEntryElement(java.util.zip.ZipOutputStream zos)

    if( outRepresentation==SINGLE_XML) {
      return new SingleDocElement( zos);
    } else {
      return new ZipEntryElement( zos);
    }
  
private java.lang.StringgetName(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 intgetRepresentation(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 booleanisClassEntry(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 voidmain(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 intprocess()

    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 voidprocessEntry(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 voidshowUsage()

    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 voidupdate(java.lang.Object arg)

    if( arg instanceof Throwable) {
      (( Throwable) arg).printStackTrace();
    } else {
      if(( n % 100)==0) {
        System.err.println( n+" "+arg);
      }
    }
    n++;