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:Main.java

/**
 * Reads an XML document and returns it as a {@link String}.
 * //from  w ww .  j  a  v a 2 s. c o  m
 * @param source the input source for the XML document
 * 
 * @return the XML document, as a {@link String}
 * 
 * @throws IOException
 */
public static String readXmlDocument(InputSource source) throws IOException {
    CharArrayWriter out = new CharArrayWriter();
    FileCopyUtils.copy(source.getCharacterStream(), out);
    return out.toString();
}

From source file:com.zotoh.core.io.StreamUte.java

/**
 * @param iso//from ww  w  . j  av a  2s .c om
 */
public static void resetInputSource(InputSource iso) {

    if (iso != null) {
        Reader rdr = iso.getCharacterStream();
        InputStream ism = iso.getByteStream();
        try {
            if (ism != null)
                ism.reset();
        } catch (Exception e) {
        }
        try {
            if (rdr != null)
                rdr.reset();
        } catch (Exception e) {
        }
    }

}

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  w w  w. j a  v  a  2 s . co m*/
            try {
                r = new InputStreamReader(i, encoding);
            } catch (UnsupportedEncodingException e) {
                r = new InputStreamReader(i);
            }
        }
    }
    // r = new BufferedReader(r);
    return r;
}

From source file:net.javacrumbs.json2xml.JsonXmlReader.java

public void parse(InputSource input) throws IOException, SAXException {
    JsonParser jsonParser = new JsonFactory().createParser(input.getCharacterStream());
    new JsonSaxAdapter(jsonParser, contentHandler, namespaceUri, addTypeAttributes, artificialRootName,
            elementNameConverter).parse();
}

From source file:org.callimachusproject.xml.InputSourceResolver.java

@Override
public Reader resolve(URI absoluteURI, String encoding, Configuration config) throws XPathException {
    try {/*www. j  ava 2 s. c  o  m*/
        InputSource source = resolve(absoluteURI.toASCIIString());
        if (encoding != null && encoding.length() > 0) {
            source.setEncoding(encoding);
        }
        return source.getCharacterStream();
    } catch (IOException e) {
        throw new XPathException(e.toString(), e);
    }
}

From source file:com.mirth.connect.plugins.datatypes.hl7v2.ER7Reader.java

private String getMessageFromSource(InputSource source) throws IOException {
    BufferedReader reader = new BufferedReader(source.getCharacterStream());
    StringBuilder builder = new StringBuilder();

    int c;//from  w  w  w  .ja  va2  s.com

    while ((c = reader.read()) != -1) {
        builder.append((char) c);
    }

    return builder.toString().trim();
}

From source file:org.callimachusproject.xml.XdmNodeFactory.java

public XdmNode parse(String systemId) throws SAXException, IOException {
    InputSource isource = resolveEntity(null, systemId);
    if (isource == null)
        return null;
    try {/*  w  ww.  j a v a 2s .  c  o  m*/
        return parse(isource);
    } finally {
        InputStream in = isource.getByteStream();
        if (in != null) {
            in.close();
        }
        Reader reader = isource.getCharacterStream();
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.mirth.connect.model.converters.NCPDPReader.java

@Override
public void parse(InputSource input) throws SAXException, IOException {
    // convert the InputSource to a String and trim the whitespace
    String message = IOUtils.toString(input.getCharacterStream()).trim();

    ContentHandler contentHandler = getContentHandler();
    contentHandler.startDocument();// w ww.  j ava2 s . c o m

    if ((message == null) || (message.length() < 3)) {
        throw new SAXException("Unable to parse, message is null or too short: " + message);
    }

    // process header
    String header = parseHeader(message, contentHandler);

    // process body
    int groupDelimeterIndex = message.indexOf(groupDelimeter);
    int segmentDelimeterIndex = message.indexOf(segmentDelimeter);
    int bodyIndex = 0;

    if ((groupDelimeterIndex == -1) || (segmentDelimeterIndex < groupDelimeterIndex)) {
        bodyIndex = segmentDelimeterIndex;
    } else {
        bodyIndex = groupDelimeterIndex;
    }

    String body = message.substring(bodyIndex, message.length());

    boolean hasMoreSegments = true;
    boolean inGroup = false;
    boolean firstTransaction = true;
    int groupCounter = 0;

    while (hasMoreSegments) {
        // get next segment or group
        groupDelimeterIndex = body.indexOf(groupDelimeter);
        segmentDelimeterIndex = body.indexOf(segmentDelimeter);

        if ((groupDelimeterIndex > -1) && (groupDelimeterIndex < segmentDelimeterIndex)) { // case: next part is a group
            // process last segment before group
            parseSegment(body.substring(0, groupDelimeterIndex), contentHandler);

            if (inGroup) {
                contentHandler.endElement("", "TRANSACTION", "");
            }

            if (firstTransaction) {
                firstTransaction = false;
                contentHandler.startElement("", "TRANSACTIONS", "", null);
            }

            // process a group
            AttributesImpl attr = new AttributesImpl();
            attr.addAttribute("", "counter", "counter", "", Integer.toString(++groupCounter));
            contentHandler.startElement("", "TRANSACTION", "", attr);
            inGroup = true;
        } else if (groupDelimeterIndex == -1 && segmentDelimeterIndex == -1) { // case: last segment
            parseSegment(body, contentHandler);
            hasMoreSegments = false;
        } else { // case: next part is a segment
            String segment = body.substring(0, segmentDelimeterIndex);
            parseSegment(segment, contentHandler);
        }

        // remove processed segment from message body
        body = body.substring(segmentDelimeterIndex + segmentDelimeter.length());
    }

    // end group if we have started one
    if (inGroup) {
        contentHandler.endElement("", "TRANSACTION", "");
        contentHandler.endElement("", "TRANSACTIONS", "");
    }

    contentHandler.endElement("", header, "");
    contentHandler.endDocument();
}

From source file:org.callimachusproject.xml.InputSourceResolver.java

@Override
public StreamSource resolve(String href, String base) throws TransformerException {
    try {// w  w w  . ja  v  a  2 s  . co m
        InputSource input = resolve(resolveURI(href, base));
        if (input == null)
            return null;
        InputStream in = input.getByteStream();
        if (in != null) {
            if (input.getSystemId() == null)
                return new StreamSource(in);
            return new StreamSource(in, input.getSystemId());
        }
        Reader reader = input.getCharacterStream();
        if (reader != null) {
            if (input.getSystemId() == null)
                return new StreamSource(reader);
            return new StreamSource(reader, input.getSystemId());
        }
        if (input.getSystemId() == null)
            return new StreamSource();
        return new StreamSource(input.getSystemId());
    } catch (IOException e) {
        throw new TransformerException(e);
    }
}

From source file:CSVXMLReader.java

/**
 * Parse a CSV file. SAX events are delivered to the ContentHandler
 * that was registered via <code>setContentHandler</code>.
 *
 * @param input the comma separated values file to parse.
 *//* ww  w .j  ava 2 s . c o  m*/
public void parse(InputSource input) throws IOException, SAXException {
    // if no handler is registered to receive events, don't bother
    // to parse the CSV file
    ContentHandler ch = getContentHandler();
    if (ch == null) {
        return;
    }

    // convert the InputSource into a BufferedReader
    BufferedReader br = null;
    if (input.getCharacterStream() != null) {
        br = new BufferedReader(input.getCharacterStream());
    } else if (input.getByteStream() != null) {
        br = new BufferedReader(new InputStreamReader(input.getByteStream()));
    } else if (input.getSystemId() != null) {
        java.net.URL url = new URL(input.getSystemId());
        br = new BufferedReader(new InputStreamReader(url.openStream()));
    } else {
        throw new SAXException("Invalid InputSource object");
    }

    ch.startDocument();

    // emit <csvFile>
    ch.startElement("", "", "csvFile", EMPTY_ATTR);

    // read each line of the file until EOF is reached
    String curLine = null;
    while ((curLine = br.readLine()) != null) {
        curLine = curLine.trim();
        if (curLine.length() > 0) {
            // create the <line> element
            ch.startElement("", "", "line", EMPTY_ATTR);
            // output data from this line
            parseLine(curLine, ch);
            // close the </line> element
            ch.endElement("", "", "line");
        }
    }

    // emit </csvFile>
    ch.endElement("", "", "csvFile");
    ch.endDocument();
}