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

public static final Object xml2obj(final Class<?> cls, final String xml) throws JAXBException {
    StringReader reader = new StringReader(xml);
    JAXBContext contextIn = JAXBContext.newInstance(cls);
    Unmarshaller marshallerIn = contextIn.createUnmarshaller();

    return marshallerIn.unmarshal(reader);
}

From source file:Main.java

public static Document read(String filename) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);//ww w . j a v  a  2s .  c  om
    DocumentBuilder builder = null;
    Document document = null;
    try {
        builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        document = builder.parse(new File(filename));
        document.getDocumentElement().normalize();
    } catch (Exception e) {
    }
    return document;
}

From source file:Main.java

public static List<String> splitLines(String string) {
    try {//from w w  w  . j  a v  a2 s.  c om
        ArrayList<String> list = new ArrayList<String>();

        LineNumberReader reader = new LineNumberReader(new StringReader(string));
        String s;
        while ((s = reader.readLine()) != null) {
            list.add(s);
        }
        return list;
    } catch (IOException e) {
        // I don't think this can really happen with a StringReader.
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Map handleXMLResponse(HttpResponse response) {
    Map<String, String> oauthResponse = new HashMap<String, String>();
    try {/*from  w w  w  . java  2 s .c  om*/

        String xmlString = EntityUtils.toString(response.getEntity());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlString));
        Document doc = db.parse(inStream);

        System.out.println("********** XML Response Received **********");
        parseXMLDoc(null, doc, oauthResponse);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Exception occurred while parsing XML response");
    }
    return oauthResponse;
}

From source file:Main.java

public static <T> T converyToJavaBean(JAXBContext context, String xmlStr, Class<T> c) {
    JAXBElement<T> t = null;
    try {//from w  ww  . j ava2s.  c o m
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (JAXBElement<T>) unmarshaller.unmarshal((new StreamSource(new StringReader(xmlStr))), c);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t.getValue();
}

From source file:Main.java

/**
 * @param <T>/*from w w  w.j a va  2 s  . c  om*/
 *           the type we want to convert the XML into
 * @param c
 *           the class of the parameterized type
 * @param xml
 *           the instance XML description
 * @return a deserialization of the XML into an object of type T of class
 *         class <T>
 * @throws javax.xml.bind.JAXBException
 */
@SuppressWarnings("unchecked")
public static <T> T unmarshal(Class<T> c, String xml) throws JAXBException {
    T res;
    if (c == xml.getClass()) {
        res = (T) xml;
    } else {
        JAXBContext ctx = JAXBContext.newInstance(c);
        Unmarshaller marshaller = ctx.createUnmarshaller();
        res = (T) marshaller.unmarshal(new StringReader(xml));
    }
    return res;
}

From source file:Main.java

/**
 * This method will Read the XML and act accordingly
 *
 * @param xmlString - the XML String/*  w  w  w  . ja v a 2  s. co  m*/
 * @return the list of elements within the XML
 */
public static Document readXML(String xmlString)
        throws SAXParseException, SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    InputSource xmlStream = new InputSource();
    xmlStream.setCharacterStream(new StringReader(xmlString));
    return dBuilder.parse(xmlStream);
}

From source file:Main.java

/**
 * Converts properties from file storage format
 *//*from w ww . java2 s.co  m*/
public static Properties fromFileContent(String content) {
    try {
        StringReader reader = new StringReader(content);
        Properties props = new Properties();
        props.load(reader);
        return props;
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe); // shouldn't happen
    }
}

From source file:edu.uci.ics.hyracks.yarn.am.manifest.ManifestParser.java

public static HyracksCluster parse(String mXML) throws Exception {
    Digester d = createDigester();//from w ww.  jav a 2 s .  c om
    return (HyracksCluster) d.parse(new StringReader(mXML));
}

From source file:Main.java

/**
 * Marshal a object of/*www .  j a  va 2  s  .c  o m*/
 * <code>classItem</code> from the xmlResponse
 * <code>String</code>.
 *
 * @param xmlResponse <code>String</code> that represents the object to be
 *                    marshal.
 * @param classItem   <code>class</code> of the returns object.
 * @return a object of <code>classItem</code>.
 * @throws JAXBException throw trying to marshal.
 */
public static Object unmarshalFromString(String xmlResponse, Class classItem) throws JAXBException {
    final JAXBContext jaxbContext = JAXBContext.newInstance(classItem);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = null;
    try {
        reader = new StringReader(xmlResponse);
        return jaxbUnmarshaller.unmarshal(reader);
    } finally {
        if (reader != null)
            reader.close();
    }
}