Example usage for org.xml.sax InputSource getCharacterStream

List of usage examples for org.xml.sax InputSource getCharacterStream

Introduction

In this page you can find the example usage for org.xml.sax InputSource getCharacterStream.

Prototype

public Reader getCharacterStream() 

Source Link

Document

Get the character stream for this input source.

Usage

From source file:org.exist.collections.Collection.java

/** Stores an XML document in the database. {@link #validateXMLResourceInternal(org.exist.storage.txn.Txn,
 * org.exist.storage.DBBroker, org.exist.xmldb.XmldbURI, CollectionConfiguration, org.exist.collections.Collection.ValidateBlock)} 
 * should have been called previously in order to acquire a write lock for the document. Launches the finish trigger.
 * /*from ww  w.  j  av  a2s.c o m*/
 * @param transaction
 * @param broker
 * @param info
 * @param source
 * @param privileged
 * 
 * @throws EXistException
 * @throws PermissionDeniedException
 * @throws TriggerException
 * @throws SAXException
 * @throws LockException
 */
public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final InputSource source,
        boolean privileged)
        throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {

    storeXMLInternal(transaction, broker, info, privileged, new StoreBlock() {
        @Override
        public void run() throws EXistException, SAXException {
            try {
                final InputStream is = source.getByteStream();
                if (is != null && is.markSupported()) {
                    is.reset();
                } else {
                    final Reader cs = source.getCharacterStream();
                    if (cs != null && cs.markSupported()) {
                        cs.reset();
                    }
                }
            } catch (final IOException e) {
                // mark is not supported: exception is expected, do nothing
                LOG.debug(
                        "InputStream or CharacterStream underlying the InputSource does not support marking and therefore cannot be re-read.");
            }
            final XMLReader reader = getReader(broker, false, info.getCollectionConfig());
            info.setReader(reader, null);
            try {
                reader.parse(source);
            } catch (final IOException e) {
                throw new EXistException(e);
            } finally {
                releaseReader(broker, info, reader);
            }
        }
    });
}

From source file:org.exist.collections.Collection.java

private InputSource closeShieldInputSource(final InputSource source) {

    final InputSource protectedInputSource = new InputSource();
    protectedInputSource.setEncoding(source.getEncoding());
    protectedInputSource.setSystemId(source.getSystemId());
    protectedInputSource.setPublicId(source.getPublicId());

    if (source.getByteStream() != null) {
        //TODO consider AutoCloseInputStream
        final InputStream closeShieldByteStream = new CloseShieldInputStream(source.getByteStream());
        protectedInputSource.setByteStream(closeShieldByteStream);
    }/*from ww  w.j  a  v a2  s  .  co  m*/

    if (source.getCharacterStream() != null) {
        //TODO consider AutoCloseReader
        final Reader closeShieldReader = new CloseShieldReader(source.getCharacterStream());
        protectedInputSource.setCharacterStream(closeShieldReader);
    }

    return protectedInputSource;
}

From source file:org.exist.collections.MutableCollection.java

@Override
public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final InputSource source)
        throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {
    storeXMLInternal(transaction, broker, info, storeInfo -> {
        try {/* w w w . j av a  2 s  .  c o m*/
            final InputStream is = source.getByteStream();
            if (is != null && is.markSupported()) {
                is.reset();
            } else {
                final Reader cs = source.getCharacterStream();
                if (cs != null && cs.markSupported()) {
                    cs.reset();
                }
            }
        } catch (final IOException e) {
            // mark is not supported: exception is expected, do nothing
            LOG.debug(
                    "InputStream or CharacterStream underlying the InputSource does not support marking and therefore cannot be re-read.");
        }
        final XMLReader reader = getReader(broker, false, storeInfo.getCollectionConfig());
        storeInfo.setReader(reader, null);
        try {
            reader.parse(source);
        } catch (final IOException e) {
            throw new EXistException(e);
        } finally {
            releaseReader(broker, storeInfo, reader);
        }
    });
}

From source file:org.exist.collections.MutableCollection.java

private InputSource closeShieldInputSource(final InputSource source) {
    final InputSource protectedInputSource = new InputSource();
    protectedInputSource.setEncoding(source.getEncoding());
    protectedInputSource.setSystemId(source.getSystemId());
    protectedInputSource.setPublicId(source.getPublicId());

    if (source.getByteStream() != null) {
        //TODO consider AutoCloseInputStream
        final InputStream closeShieldByteStream = new CloseShieldInputStream(source.getByteStream());
        protectedInputSource.setByteStream(closeShieldByteStream);
    }// ww  w. j a  va 2  s .  c  om

    if (source.getCharacterStream() != null) {
        //TODO consider AutoCloseReader
        final Reader closeShieldReader = new CloseShieldReader(source.getCharacterStream());
        protectedInputSource.setCharacterStream(closeShieldReader);
    }

    return protectedInputSource;
}

From source file:org.jboss.bpm.console.server.util.DOMUtils.java

/** Parse the given input source and return the root Element
 *//*from   w  w w . j  a  va  2 s.co  m*/
public static Element parse(InputSource source) throws IOException {
    try {
        return getDocumentBuilder().parse(source).getDocumentElement();
    } catch (SAXException se) {
        throw new IOException(se.toString());
    } finally {
        InputStream is = source.getByteStream();
        if (is != null) {
            is.close();
        }
        Reader r = source.getCharacterStream();
        if (r != null) {
            r.close();
        }
    }
}

From source file:org.milyn.csv.CSVRecordParser.java

/**
 * {@inheritDoc}//from   w  w w . j  a v  a2  s .c  o m
 */
public void setDataSource(InputSource source) {
    Reader reader = source.getCharacterStream();

    if (reader == null) {
        throw new IllegalStateException(
                "Invalid InputSource type supplied to CSVRecordParser.  Must contain a Reader instance.");
    }

    // Create the CSV line reader...
    T factory = getFactory();
    csvLineReader = new au.com.bytecode.opencsv.CSVReader(reader, factory.getSeparator(),
            factory.getQuoteChar(), factory.getSkipLines());
}

From source file:org.regenstrief.util.XMLUtil.java

public final static Reader toReader(final InputSource source) {
    try {//from   ww w.ja  va2s  .co m
        final InputStream in = source.getByteStream();
        if (in != null) {
            return Util.getReader(in);
        } else {
            final Reader r = source.getCharacterStream();
            if (r != null) {
                return r;
            } else {
                final String systemId = source.getSystemId();
                if (systemId != null) {
                    return Util.getReader(systemId);
                }
            }
        }
    } catch (final Exception e) {
        throw Util.toRuntimeException(e);
    }

    throw new RuntimeException("Unsupported InputSource: " + source);
}

From source file:org.xchain.framework.servlet.XChainManager.java

private static void close(InputSource inputSource) {
    try {//from   ww w.j  av  a2 s .  co  m
        if (inputSource != null && inputSource.getByteStream() != null) {
            close(inputSource.getByteStream());
        } else if (inputSource != null && inputSource.getCharacterStream() != null) {
            close(inputSource.getCharacterStream());
        }
    } catch (Throwable t) {
        if (log.isWarnEnabled()) {
            log.warn("Exception thrown while closing input source.");
        }
    }
}

From source file:org.zanata.adapter.glossary.GlossaryPoReader.java

static MessageStreamParser createParser(InputSource inputSource) {
    MessageStreamParser messageParser;/* w  ww  .  j  av a2 s. c  o  m*/
    if (inputSource.getCharacterStream() != null)
        messageParser = new MessageStreamParser(inputSource.getCharacterStream());
    else if (inputSource.getByteStream() != null) {
        if (inputSource.getEncoding() != null)
            messageParser = new MessageStreamParser(inputSource.getByteStream(),
                    Charset.forName(inputSource.getEncoding()));
        else
            messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName("UTF-8"));
    } else if (inputSource.getSystemId() != null) {
        try {
            URL url = new URL(inputSource.getSystemId());

            if (inputSource.getEncoding() != null)
                messageParser = new MessageStreamParser(url.openStream(),
                        Charset.forName(inputSource.getEncoding()));
            else
                messageParser = new MessageStreamParser(url.openStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException("failed to get input from url in inputSource", e);
        }
    } else
        throw new RuntimeException("not a valid inputSource");

    return messageParser;
}

From source file:org.zanata.adapter.po.PoReader2.java

static MessageStreamParser createParser(InputSource inputSource) {
    MessageStreamParser messageParser;//  w ww.  java  2s .  c o m
    if (inputSource.getCharacterStream() != null)
        messageParser = new MessageStreamParser(inputSource.getCharacterStream());
    else if (inputSource.getByteStream() != null) {
        if (inputSource.getEncoding() != null)
            messageParser = new MessageStreamParser(inputSource.getByteStream(),
                    Charset.forName(inputSource.getEncoding()));
        else
            messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName("UTF-8"));
    } else if (inputSource.getSystemId() != null) {
        try {
            URL url = new URL(inputSource.getSystemId());

            if (inputSource.getEncoding() != null)
                messageParser = new MessageStreamParser(url.openStream(),
                        Charset.forName(inputSource.getEncoding()));
            else
                messageParser = new MessageStreamParser(url.openStream(), Charset.forName("UTF-8"));
        } catch (IOException e) {
            // TODO throw stronger typed exception
            throw new RuntimeException("failed to get input from url in inputSource", e);
        }
    } else
        // TODO throw stronger typed exception
        throw new RuntimeException("not a valid inputSource");

    return messageParser;
}