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 String formatXml(String xml) {

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {/*ww w .  j  a va 2 s. c o  m*/
        readableXformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output));
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to format XML", e);
    }
    return output.toString();

}

From source file:Main.java

public static void setPaneHtmlText(String htmlText, JEditorPane pane) {
    if (htmlText == null) {
        pane.setText("");
        return;/*from  w w w  .j  av a  2s .com*/
    } else if (htmlText.length() == 0) {
        pane.setText("");
        return;
    }

    StringReader htmReader = new StringReader(htmlText);
    HTMLEditorKit kit = (HTMLEditorKit) pane.getEditorKitForContentType("text/html");
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();

    ParserDelegator parser = new ParserDelegator();
    try {
        parser.parse(htmReader, doc.getReader(0), true);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    pane.setDocument(doc);
}

From source file:Main.java

public static Object getBeanFromXml(String xml, Class xmlBeanClass) throws Exception {
    Method method = (Method) UNMARSHAL_METHOD_MAP.get(xmlBeanClass.getName());

    if (method == null) {
        method = xmlBeanClass.getMethod("unmarshal", new Class[] { Reader.class });
        UNMARSHAL_METHOD_MAP.put(xmlBeanClass.getName(), method);
    }/*from w ww  . java2s. c  o  m*/
    StringReader stringReader = new StringReader(xml);

    return method.invoke(null, new Object[] { stringReader });

}

From source file:Main.java

/**
 * /*from w  w  w.  j a va2 s .c o  m*/
 * @param xml
 * @param indent
 * @return pretty formatted xml
 */
public static String prettyFormat(String xml, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(xml));
        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");
        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 xmlToBean(String xmlStr, Class<T> t) {
    try {/* w w  w  .  j  a v a 2s  .  co m*/
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlStr, Class<T> t) {
    try {//from  w w  w .  jav a  2s  .  c om
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        T ts = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
        return ts;
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String transformString(String xmlString, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new StringReader(xmlString));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);//w  w  w .  java 2s . c o m

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

public static Node transform(String xslSource, Node original) {
    StringReader sr = new StringReader(xslSource);
    DOMResult result = new DOMResult();
    doTransform(new StreamSource(sr), new DOMSource(original), result);
    return result.getNode();
}

From source file:Main.java

public static Object fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml) {
    JAXBContext context = null;// w  w w .j  ava2 s  .  co  m
    try {
        context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return unmarshaller.unmarshal(new StreamSource(new StringReader(stringXml)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static Document getDocument(String xml) {
    try {//  w  w w.j  av a 2  s.c o m
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        return null;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}