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 synchronized Document riskyParse(String text) throws Exception {
    InputSource is = new InputSource(new StringReader(text));
    return parser.parse(is);
}

From source file:Main.java

public static String prettyFormat(String input, int indent, boolean isOmitXmlDeclaration) {
    try {/*  ww  w  . java 2s . co m*/
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (isOmitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToObject(String str, Class<T> clszz) {
    JAXBContext context = null;//from   w  w  w  . j  a v a2s . co  m
    T obj = null;

    try {
        context = JAXBContext.newInstance(clszz);
        StringReader reader = new StringReader(str);
        Unmarshaller unmar = context.createUnmarshaller();
        obj = (T) unmar.unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T ToJavaBean(String xml, Class<T> c) {
    T t = null;/*from w w  w  . j  av a  2s  .  c o m*/
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return t;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;/*from  w w w.ja  va2 s.c  om*/
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        System.out.println(e);
    }
    return t;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;//from ww w.  ja  va  2s  . c  o m
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return t;
}

From source file:Main.java

public static <T> ArrayList<T> toObjectList(String json, Class<T> type) {
    if (json == null) {
        return null;
    }//  w w  w  .  ja  va 2  s.  co  m
    JsonReader reader = new JsonReader(new StringReader(json));
    reader.setLenient(true);
    Type gsonType = new TypeToken<ArrayList<T>>() {
    }.getType();
    return (ArrayList<T>) gson.fromJson(reader, gsonType);
}

From source file:Main.java

public static String formatXml(String xml) {

    try {/*from   ww  w.  j av  a  2 s . co  m*/
        final InputSource src = new InputSource(new StringReader(xml));
        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
                .getDocumentElement();
        final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

        //May need this: System.setProperty(DOMImplementationRegistry.
        //PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        final LSSerializer writer = impl.createLSSerializer();

        // Set this to true if the output needs to be beautified.
        writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

        // Set this to true if the declaration is needed to be outputted.
        writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);

        return writer.writeToString(document);
    } catch (Exception e) {

        System.err.println("Exception thrown");
        throw new RuntimeException(e);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T fromXml(String xmlStr, Class<T> klass) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(klass);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    return t;// w ww . ja v  a 2s.  com
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unserializer(Class<T> clazz, String xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T obj = (T) unmarshaller.unmarshal(new StringReader(xml));
    return obj;//from   w  w  w.jav a2s.  c  om
}