Example usage for org.xml.sax InputSource getEncoding

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

Introduction

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

Prototype

public String getEncoding() 

Source Link

Document

Get the character encoding for a byte stream or URI.

Usage

From source file:com.adamrosenfield.wordswithcrosses.net.derstandard.SolutionParser.java

private Reader getReader(InputSource s) throws SAXException, IOException {
    Reader r = s.getCharacterStream();
    InputStream i = s.getByteStream();
    String encoding = s.getEncoding();
    String publicid = s.getPublicId();
    String systemid = s.getSystemId();
    if (r == null) {
        if (i == null)
            i = getInputStream(publicid, systemid);
        // i = new BufferedInputStream(i);
        if (encoding == null) {
            r = theAutoDetector.autoDetectingReader(i);
        } else {//from  ww w . j  a  v  a  2 s .c o m
            try {
                r = new InputStreamReader(i, encoding);
            } catch (UnsupportedEncodingException e) {
                r = new InputStreamReader(i);
            }
        }
    }
    // r = new BufferedReader(r);
    return r;
}

From source file:org.cobra_grendel.html.parser.DocumentBuilderImpl.java

/**
 * Creates a document without parsing it so it can be used for incremental rendering.
 * /* w  w w  .j  av a 2 s .c o m*/
 * @param is
 *            The input source, which may be an instance of {@link org.cobra_grendel.html.parser.InputSourceImpl}.
 */
public Document createDocument(final InputSource is, final int transactionId) throws SAXException, IOException {
    String charset = is.getEncoding();
    if (charset == null) {
        charset = "US-ASCII";
    }
    String uri = is.getSystemId();
    if (uri == null) {
        LOGGER.warn("parse(): InputSource has no SystemId (URI); document item URLs will not be resolvable.");
    }
    InputStream in = is.getByteStream();
    WritableLineReader wis;
    if (in != null) {
        wis = new WritableLineReader(new InputStreamReader(in, charset));
    } else {
        Reader reader = is.getCharacterStream();
        if (reader != null) {
            wis = new WritableLineReader(reader);
        } else {
            throw new IllegalArgumentException("InputSource has neither a byte stream nor a character stream");
        }
    }
    HTMLDocumentImpl document = new HTMLDocumentImpl(bcontext, rcontext, wis, uri, transactionId);
    return document;
}

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);
    }/*w  w  w. j  a  v  a 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.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  va2s  . 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.dom.XMLUtil.java

public static String readFile(InputSource is) throws IOException {
    // read the file into a string
    return readFile(is.getByteStream(), is.getEncoding());
}

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

static MessageStreamParser createParser(InputSource inputSource) {
    MessageStreamParser messageParser;//ww w . j a v  a2s  . co 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 .  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) {
            // 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;
}