⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processor.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final byte[] buff = new byte[4096];
        int i;
        while ( (i = zis.read( buff )) != -1 ) {
            bos.write( buff,
                       0,
                       i );
        }
        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;
        }

        final int inRepresentation = getRepresentation( args[0] );
        final 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;
        }

        final Processor m = new Processor( inRepresentation,
                                           outRepresentation,
                                           is,
                                           os,
                                           xslt );

        final long l1 = System.currentTimeMillis();
        final int n = m.process();
        final 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 Processor.BYTECODE;
        } else if ( "xml".equals( s ) ) {
            return Processor.MULTI_XML;
        } else if ( "singlexml".equals( s ) ) {
            return Processor.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 file>]" );
        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 this.is.read();
        }

        public final int read(final byte[] b,
                              final int off,
                              final int len) throws IOException {
            return this.is.read( b,
                                 off,
                                 len );
        }

        public final int available() throws IOException {
            return this.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( this.w,
                                  this.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( this.os,
                                          this.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 {
                final TransformerHandler handler = this.saxtf.newTransformerHandler( this.templates );
                handler.setResult( new SAXResult( this.outputHandler ) );
                return handler;
            } catch ( final 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 this.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();
                this.w.write( "<".concat( qName ) );
                if ( atts != null && atts.getLength() > 0 ) {
                    writeAttributes( atts );
                }

                if ( !this.optimizeEmptyElements ) {
                    this.w.write( ">\n" );
                } else {
                    this.openElement = true;
                }
                this.ident += 2;

            } catch ( final IOException ex ) {
                throw new SAXException( ex );

            }
        }

        public final void endElement(final String ns,
                                     final String localName,
                                     final String qName) throws SAXException {
            this.ident -= 2;
            try {
                if ( this.openElement ) {
                    this.w.write( "/>\n" );
                    this.openElement = false;
                } else {
                    writeIdent();
                    this.w.write( "</" + qName + ">\n" );
                }

            } catch ( final IOException ex ) {
                throw new SAXException( ex );

            }
        }

        public final void endDocument() throws SAXException {
            try {
                this.w.flush();

            } catch ( final IOException ex ) {
                throw new SAXException( ex );

            }
        }

        public final void comment(final char[] ch,
                                  final int off,
                                  final int len) throws SAXException {
            try {
                closeElement();

                writeIdent();
                this.w.write( "<!-- " );
                this.w.write( ch,
                              off,
                              len );
                this.w.write( " -->\n" );

            } catch ( final 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 {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -