Example usage for org.xml.sax InputSource setCharacterStream

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

Introduction

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

Prototype

public void setCharacterStream(Reader characterStream) 

Source Link

Document

Set the character stream for this input source.

Usage

From source file:uk.co.anthonycampbell.java.mp4reader.util.Util.java

/**
 * Utility method to parse iTunes meta XML.
 * /*from w w w.ja  va  2s .  c  o m*/
 * @param xml - the XML to parse.
 */
public static Map<String, List<String>> parseITunesMeta(final String xml) {
    // Initialise result
    final Map<String, List<String>> iTunesMap = new HashMap<>();

    // Validate
    if (StringUtils.isNotEmpty(xml)) {
        try {
            final InputSource inputSource = new InputSource();
            inputSource.setCharacterStream(new StringReader(xml));

            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            final DocumentBuilder builder = factory.newDocumentBuilder();
            final Document document = builder.parse(inputSource);

            final NodeList plists = document.getElementsByTagName("plist");
            for (int i = 0; i < plists.getLength(); ++i) {
                final Element plist = (Element) plists.item(i);

                if (plist != null && plist.hasChildNodes()) {
                    final NodeList nodes = plist.getChildNodes();

                    if (nodes != null) {
                        for (int j = 0; j < nodes.getLength(); ++j) {
                            final Node node = nodes.item(j);

                            if (node != null && node.getNodeType() == Node.ELEMENT_NODE
                                    && "dict".equals(node.getNodeName())) {
                                iTunesMap.putAll(parseDictionary(node));
                            }
                        }
                    }
                }
            }
        } catch (ParserConfigurationException pce) {
        } catch (SAXException saxe) {
        } catch (IOException ioe) {
        }

    } else {
        throw new IllegalArgumentException("Provided XML is invalid! (xml=" + xml + ")");
    }

    return iTunesMap;
}

From source file:uk.me.viv.logmyride.KMLFile.java

/**
 * @todo push this down into a MotionX specific KML class
 * @return/*from  w w  w.  j  ava2 s.  c om*/
 */
public Map<String, String> getDescription() {

    String[] descriptionProperties = { "Date", "Distance", "Elapsed Time", "Avg Speed", "Max Speed", "Avg Pace",
            "Min Altitude", "Max Altitude", "Start Time", "Finish Time", };

    Map<String, String> description = new HashMap<>();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(this.kml));
        try {
            Document doc = builder.parse(is);
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();

            description.put("Name", xpath.evaluate("//Document/name/text()", doc));

            for (String property : descriptionProperties) {
                description.put(property,
                        xpath.evaluate("//td[text()=\"" + property + ":\"]/following::td[1]/text()", doc));
            }

            description.put("Filename", this.getFilename());
            description.put("ID", DigestUtils.shaHex(Integer.toString(description.hashCode())));

        } catch (SAXException ex) {
            Logger.getLogger(KMLFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(KMLFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (XPathExpressionException ex) {
            Logger.getLogger(KMLFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(KMLFile.class.getName()).log(Level.SEVERE, null, ex);
    }

    return description;
}

From source file:webrtc.server.HTTP.HTTPRequest.java

private static Document xmlFromString(String xmlString) {
    try {//  w ww  .  j  a va  2 s  . co  m
        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();

        is.setCharacterStream(new StringReader(xmlString));
        return dBuilder.parse(is);

    } catch (ParserConfigurationException | SAXException | IOException ex) {
        LOG.log(Level.SEVERE, null, ex);
    }

    return null;
}