Example usage for javax.xml.parsers SAXParser reset

List of usage examples for javax.xml.parsers SAXParser reset

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser reset.

Prototype

public void reset() 

Source Link

Document

Reset this SAXParser to its original configuration.

SAXParser is reset to the same state as when it was created with SAXParserFactory#newSAXParser() .

Usage

From source file:com.honnix.jaxo.core.internal.pool.SAXParserObjectFactory.java

@Override
public void passivateObject(SAXParser saxParser) throws Exception {
    saxParser.reset();
}

From source file:ch.entwine.weblounge.common.impl.content.AbstractResourceReaderImpl.java

/**
 * Resets this parser instance.// w  ww.  j a va2 s .com
 */
@Override
public void reset() {
    this.resource = null;
    this.parserContext = ParserContext.Document;
    SAXParser parser = parserRef.get();
    if (parser != null)
        parser.reset();
}

From source file:com.amalto.core.schema.validation.SkipAttributeDocumentBuilder.java

@Override
public Document parse(InputSource is) throws SAXException, IOException {
    SAXParser parser;
    try {//  w  w w . j a  v  a 2  s.  c  o m
        parser = getSaxParser();
    } catch (ParserConfigurationException e) {
        throw new SAXException(e);
    }
    try {
        Document document = newDocument();
        parser.parse(is, new SkipAttributeHandler(document, ignoreTalendNamespace));
        return document;
    } finally {
        parser.reset();
    }
}

From source file:ch.entwine.weblounge.common.impl.content.page.PageReader.java

/**
 * Resets this parser instance.//  w ww.j a  v  a 2  s . c  o  m
 */
public void reset() {
    super.reset();
    this.page = null;
    this.composer = null;
    this.parserContext = ParserContext.Document;
    this.position = 0;
    this.pageletReader.reset();
    SAXParser parser = parserRef.get();
    if (parser != null)
        parser.reset();
}

From source file:org.alfresco.repo.transfer.fsr.FileTransferReceiver.java

public void commit(String transferId) throws TransferException {
    if (log.isDebugEnabled()) {
        log.debug("Committing transferId=" + transferId);
    }//from  w  ww.  j a v a2 s. c  o m

    /**
     * A side-effect of checking the lock here is that it ensures that the lock timeout is suspended.
     */
    checkLock(transferId);

    final String fTransferId = transferId;

    try {
        progressMonitor.updateStatus(transferId, TransferProgress.Status.COMMITTING);

        List<TransferManifestProcessor> commitProcessors = manifestProcessorFactory
                .getCommitProcessors(FileTransferReceiver.this, fTransferId);

        try {
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            SAXParser parser = saxParserFactory.newSAXParser();
            File snapshotFile = getSnapshotFile(fTransferId);

            if (snapshotFile.exists()) {
                if (log.isDebugEnabled()) {
                    log.debug("Processing manifest file:" + snapshotFile.getAbsolutePath());
                }
                // We parse the file as many times as we have processors
                for (TransferManifestProcessor processor : commitProcessors) {
                    XMLTransferManifestReader reader = new XMLTransferManifestReader(processor);
                    parser.parse(snapshotFile, reader);
                    parser.reset();
                }
            } else {
                progressMonitor.logException(fTransferId, "Unable to start commit. No snapshot file received",
                        new TransferException(MSG_NO_SNAPSHOT_RECEIVED, new Object[] { fTransferId }));
            }
        } catch (Exception ex) {
            progressMonitor.logException(transferId, "Caught exception while committing the transfer", ex);
        }

        //Was there an error? If so, change the transfer status to "ERROR" and throw the exception
        Throwable error = progressMonitor.getProgress(transferId).getError();
        if (error != null) {
            progressMonitor.updateStatus(transferId, TransferProgress.Status.ERROR);
            if (TransferException.class.isAssignableFrom(error.getClass())) {
                throw (TransferException) error;
            } else {
                throw new TransferException(MSG_ERROR_WHILE_COMMITTING_TRANSFER, new Object[] { transferId },
                        error);
            }
        }

        /**
         * If we get to this point then the commit has taken place without error.
         */
        progressMonitor.updateStatus(transferId, TransferProgress.Status.COMPLETE);
        if (log.isDebugEnabled()) {
            log.debug("Commit success transferId=" + transferId);
        }
    } finally {
        /**
         * Clean up at the end of the transfer
         */
        try {
            end(transferId);
        } catch (Exception ex) {
            log.error("Failed to clean up transfer. Lock may still be in place: " + transferId, ex);
        }

        // let's run postCommit
        if (postCommit != null && postCommit.size() > 0) {
            for (FSRRunnable runnable : postCommit) {
                try {
                    runnable.setTransferId(transferId);
                    runnable.run();
                } catch (Throwable t) {
                    log.error("Error from postCommit event t:" + t.toString(), t);
                }
            }
        }
    }

}

From source file:org.alfresco.repo.transfer.RepoTransferReceiverImpl.java

public void commit(final String transferId) throws TransferException {
    if (log.isDebugEnabled()) {
        log.debug("Committing transferId=" + transferId);
    }/*  www.j  a  v  a  2s  . c  o  m*/

    /**
     * A side-effect of checking the lock here is that it ensures that the lock timeout is suspended.
     */
    checkLock(transferId);

    /**
     * Turn off rules while transfer is being committed.
     */
    boolean rulesEnabled = ruleService.isEnabled();
    ruleService.disableRules();

    try {
        /* lock is going to be released */ checkLock(transferId);
        progressMonitor.updateStatus(transferId, Status.COMMITTING);

        RetryingTransactionHelper.RetryingTransactionCallback<Object> commitWork = new RetryingTransactionCallback<Object>() {
            public Object execute() throws Throwable {
                AlfrescoTransactionSupport.bindListener(
                        new TransferCommitTransactionListener(transferId, RepoTransferReceiverImpl.this));

                List<TransferManifestProcessor> commitProcessors = manifestProcessorFactory
                        .getCommitProcessors(RepoTransferReceiverImpl.this, transferId);

                Set<TransferSummaryReport> summaryReports = new HashSet<TransferSummaryReport>();

                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                SAXParser parser = saxParserFactory.newSAXParser();
                File snapshotFile = getSnapshotFile(transferId);

                if (snapshotFile.exists()) {
                    if (log.isDebugEnabled()) {
                        log.debug("Processing manifest file:" + snapshotFile.getAbsolutePath());
                    }
                    // We parse the file as many times as we have processors
                    for (TransferManifestProcessor processor : commitProcessors) {
                        XMLTransferManifestReader reader = new XMLTransferManifestReader(processor);

                        //behaviourFilter.disableBehaviour(ContentModel.ASPECT_AUDITABLE);
                        behaviourFilter.disableBehaviour();
                        if (processor instanceof TransferSummaryAware) {
                            summaryReports.add(((TransferSummaryAware) processor).getTransferSummaryReport());
                        }
                        try {
                            parser.parse(snapshotFile, reader);
                        } finally {
                            behaviourFilter.enableBehaviour();
                        }
                        parser.reset();
                    }

                    for (TransferSummaryReport transferSummaryReport : summaryReports) {
                        if (transferSummaryReport != null) {
                            transferSummaryReport.finishSummaryReport();
                        }
                    }
                } else {
                    progressMonitor.logException(transferId,
                            "Unable to start commit. No snapshot file received",
                            new TransferException(MSG_NO_SNAPSHOT_RECEIVED, new Object[] { transferId }));
                }
                return null;
            }
        };

        transactionService.getRetryingTransactionHelper().doInTransaction(commitWork, false, true);

        Throwable error = progressMonitor.getProgress(transferId).getError();
        if (error != null) {
            if (TransferException.class.isAssignableFrom(error.getClass())) {
                throw (TransferException) error;
            } else {
                throw new TransferException(MSG_ERROR_WHILE_COMMITTING_TRANSFER, new Object[] { transferId },
                        error);
            }
        }

        /**
         * Successfully committed
         */
        if (log.isDebugEnabled()) {
            log.debug("Commit success transferId=" + transferId);
        }
    } catch (Exception ex) {
        if (TransferException.class.isAssignableFrom(ex.getClass())) {
            throw (TransferException) ex;
        } else {
            throw new TransferException(MSG_ERROR_WHILE_COMMITTING_TRANSFER, ex);
        }
    } finally {
        if (rulesEnabled) {
            /**
             * Turn rules back on if we turned them off earlier.
             */
            ruleService.enableRules();
        }

        /**
         * Clean up at the end of the transfer
         */
        try {
            end(transferId);
        } catch (Exception ex) {
            log.error("Failed to clean up transfer. Lock may still be in place: " + transferId, ex);
        }
    }
}

From source file:org.attoparser.benchmark.AttoParserVSStandardSAXBenchmark.java

public static String standardSaxBenchmark(final String fileName, final int iterations) throws Exception {

    final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    final SAXParser parser = parserFactory.newSAXParser();

    /*//w  w  w. j a  v  a  2 s .c  o  m
     * WARMUP BEGIN
     */
    System.out.println("Warming up phase for SAX STARTED");
    for (int i = 0; i < 10000; i++) {

        InputStream is = null;
        Reader reader = null;

        try {

            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"));

            final InputSource inputSource = new InputSource(reader);

            final BenchmarkStandardSaxContentHandler handler = new BenchmarkStandardSaxContentHandler();
            parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
            parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler);

            parser.parse(inputSource, handler);
            parser.reset();

            handler.getEventCounter();

        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (final Exception ignored) {
                /* ignored */}
            try {
                if (is != null)
                    is.close();
            } catch (final Exception ignored) {
                /* ignored */}
        }

    }
    /*
     * WARMUP END
     */
    System.out.println("Warming up phase for SAX FINISHED");

    final StopWatch sw = new StopWatch();
    boolean started = false;

    int eventCounter = 0;
    for (int i = 0; i < iterations; i++) {

        InputStream is = null;
        Reader reader = null;

        try {

            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"));

            final InputSource inputSource = new InputSource(reader);

            final BenchmarkStandardSaxContentHandler handler = new BenchmarkStandardSaxContentHandler();
            parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
            parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler);

            if (started) {
                sw.resume();
            } else {
                started = true;
                sw.start();
            }

            parser.parse(inputSource, handler);
            parser.reset();

            sw.suspend();

            eventCounter = handler.getEventCounter();

        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (final Exception ignored) {
                /* ignored */}
            try {
                if (is != null)
                    is.close();
            } catch (final Exception ignored) {
                /* ignored */}
        }

    }

    sw.stop();

    return "[" + eventCounter + "] " + sw.toString();

}