Example usage for java.io StringReader StringReader

List of usage examples for java.io StringReader StringReader

Introduction

In this page you can find the example usage for java.io StringReader StringReader.

Prototype

public StringReader(String s) 

Source Link

Document

Creates a new string reader.

Usage

From source file:ca.liquidlabs.android.speedtestvisualizer.util.CsvDataParser.java

/**
 * Parses CSV data/*from  w ww.  ja v  a 2  s. c o m*/
 * 
 * @param csvHeader Header items for csv records
 * @param csvData
 * @return
 */
public static List<SpeedTestRecord> parseCsvData(String csvHeader, String csvData) {
    Reader in = new StringReader(getCsvData(csvHeader, csvData));
    try {
        CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT.toBuilder().withHeader().build());

        // get the parsed records
        List<CSVRecord> list = parser.getRecords();
        // create a list to convert data into SpeedTestRecord model
        List<SpeedTestRecord> speedTestRecords = new ArrayList<SpeedTestRecord>();
        for (CSVRecord csvRecord : list) {
            speedTestRecords.add(new SpeedTestRecord(csvRecord));
        }

        return speedTestRecords;
    } catch (IOException e) {
        Tracer.error(LOG_TAG, e);
    }
    // when no data, send empty list
    return Collections.emptyList();
}

From source file:Main.java

/**
 * Reads the DOM Document from a XML string
 *//*from w ww  . j av  a2 s.  c  o  m*/
public static Document readDocument(final String xml) {
    return readDocument(new InputSource(new StringReader(xml)));
}

From source file:Main.java

/**
 * Unmarshal XML data from XML string using XSD string and return the resulting JAXB content tree
 *
 * @param dummyCtxObject//  w ww  .j a  va  2 s.co  m
 *            Dummy contect object for creating related JAXB context
 * @param strXML
 *            XML
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshalling(Object dummyCtxObject, String strXML, String strXSD) throws Exception {
    if (dummyCtxObject == null) {
        throw new RuntimeException("No dummy context objekt (null)!");
    }
    if (strXML == null) {
        throw new RuntimeException("No XML document (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD document (null)!");
    }

    Object unmarshalledObject = null;
    StringReader readerXSD = null;
    StringReader readerXML = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        readerXSD = new StringReader(strXSD);
        readerXML = new StringReader(strXML);

        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(readerXSD));
        unmarshaller.setSchema(schema);

        unmarshalledObject = unmarshaller.unmarshal(new StreamSource(readerXML));
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (readerXSD != null) {
            readerXSD.close();
            readerXSD = null;
        }
        if (readerXML != null) {
            readerXML.close();
            readerXML = null;
        }
    }

    return unmarshalledObject;
}

From source file:Main.java

/**
 * Converts String containing XML code to Document
 * /*from  ww  w .j  a va 2s. c  om*/
 * @param xmlString
 * @return <code>Document</code> interface
 */
public static Document stringToDocument(String xmlString) {
    if (xmlString == null)
        return null;

    DocumentBuilder documentBuilder = getDocumentBuilder();
    InputSource inputSource = new InputSource(new StringReader(xmlString));
    try {
        return documentBuilder.parse(inputSource);
    } catch (SAXException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Unmarshal XML data from XML DOM document using XSD string and return the resulting JAXB content tree
 *
 * @param dummyJAXBObject//from  ww w.  j a va2  s. c  o m
 *            Dummy contect object for creating related JAXB context
 * @param doc
 *            XML DOM document
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromDOMDocument(Object dummyJAXBObject, Document doc, String strXSD)
        throws Exception {
    if (dummyJAXBObject == null) {
        throw new RuntimeException("No dummy context objekt (null)!");
    }
    if (doc == null) {
        throw new RuntimeException("No XML DOM document (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD document (null)!");
    }

    Object unmarshalledObject = null;
    StringReader reader = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyJAXBObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        reader = new StringReader(strXSD);

        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(reader));
        unmarshaller.setSchema(schema);

        unmarshalledObject = unmarshaller.unmarshal(doc);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return unmarshalledObject;
}

From source file:com.jaeksoft.searchlib.util.NetworksUtils.java

public final static SubnetInfo[] getSubnetArray(String ips) throws IOException {
    if (ips == null)
        return null;
    StringReader sr = new StringReader(ips);
    try {//from   w w w. j a  v  a2s. com
        List<String> lines = IOUtils.readLines(sr);
        if (lines == null)
            return null;
        SubnetInfo[] subnetArray = new SubnetInfo[lines.size()];
        int i = 0;
        for (String line : lines) {
            line = line.trim();
            if (line.isEmpty())
                continue;
            if (line.indexOf('/') == -1)
                line = line + "/32";
            SubnetUtils subnetUtils = new SubnetUtils(line);
            subnetUtils.setInclusiveHostCount(true);
            subnetArray[i++] = subnetUtils.getInfo();
        }
        return subnetArray;
    } finally {
        IOUtils.closeQuietly(sr);
    }
}

From source file:Main.java

/**
 * Parses a XML document./*from w  ww  .  j  av a2s .  c o m*/
 * 
 * @param xml the XML document
 * @return DOM
 */
public static Document parse(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    removeWhitespaces((Element) document.getChildNodes().item(0));
    return document;
}

From source file:com.ms.commons.test.classloader.util.VelocityTemplateUtil.java

public static String mergeContent(final Map<Object, Object> context, String text) {
    StringReader sr = new StringReader(text);
    StringWriter sw = new StringWriter();
    Context c = new Context() {

        public boolean containsKey(Object key) {
            return context.containsKey(key);
        }/*  w w w.j av a 2  s  . c  o  m*/

        public Object get(String key) {
            return context.get(key);
        }

        public Object[] getKeys() {
            return context.keySet().toArray();
        }

        public Object put(String key, Object value) {
            return context.put(key, value);
        }

        public Object remove(Object key) {
            return context.remove(key);
        }
    };

    try {
        VELOCITY_ENGINE.evaluate(c, sw, VelocityTemplateUtil.class.getSimpleName(), sr);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return sw.toString();
}

From source file:Main.java

/**
 * Evaluates the XPath expression against the <code>xml</code> and returns the selected value.
 * //from  ww w  .  j  a  va  2 s  .  c o m
 * @param expression Expression to evaluate.
 * @param xml The xml to query.
 * @return The selected value.
 * @throws XPathExpressionException If an error occurs evaluating the expression.
 */
public static String getValue(final String expression, final String xml) throws XPathExpressionException {
    final InputSource source = new InputSource(new StringReader(xml));
    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();

    return xpath.evaluate(expression, source);
}

From source file:com.github.dbourdette.glass.job.util.JobDataMapUtils.java

public static JobDataMap fromProperties(String dataMap) {
    if (StringUtils.isEmpty(dataMap)) {
        return new JobDataMap();
    }/*from   w  ww  .  j av a  2  s . com*/

    Properties props = new Properties();

    try {
        props.load(new StringReader(dataMap));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    JobDataMap map = new JobDataMap();

    for (Object key : props.keySet()) {
        map.put(key, props.getProperty((String) key));
    }

    return map;
}