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:com.sdl.odata.renderer.util.PrettyPrinter.java

/**
 * Pretty-print a given XML./*w  w  w  .j  a  v  a2 s .co m*/
 *
 * @param xml The not-formatted XML.
 * @return The pretty-printed XML.
 */
public static String prettyPrintXml(String xml) throws TransformerException, IOException {

    Source xmlInput = new StreamSource(new StringReader(xml));
    try (StringWriter stringWriter = new StringWriter()) {
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", DEFAULT_INDENT);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    }
}

From source file:Main.java

/**
 * @param parent//from   w ww. j  av  a  2 s .  c om
 *          node to add fragment to
 * @param fragment
 *          a well formed XML fragment
 * @throws ParserConfigurationException 
 */
public static void appendXmlFragment(Node parent, String fragment)
        throws IOException, SAXException, ParserConfigurationException {

    DocumentBuilder docBuilder = getBuilder();
    Document doc = parent.getOwnerDocument();

    Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement();

    fragmentNode = doc.importNode(fragmentNode, true);

    parent.appendChild(fragmentNode);
}

From source file:Main.java

/**
 * Converts an XML string into an equivalent XML document. Applies the
 * reverse operation of {@link #xmlDocumentToString(org.w3c.dom.Node)}.
 *
 * @param xml the XML string to convert/*from  w w w . j ava 2 s  .  c o m*/
 * @return a {@link Document} equivalent to the specified XML string
 * @throws SAXException if an error occurs building the XML document
 * @throws IOException if a general IO error occurs
 * @throws ParserConfigurationException if an error occurs configuring the
 * XML parser
 */
static Document xmlStringToDocument(String xml) throws SAXException, IOException, ParserConfigurationException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

public static Document parseXmlData(String xmlData, boolean validating) {

    try {//from  w w  w  . j ava2 s .  c o  m
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validating);
        // Create the builder and parse the file
        Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlData)));
        return doc;
    } catch (SAXException e) {
        // A parsing error occurred; the xml input is not valid
        System.err.println("SAXException in parsing XML data, will return null doc");
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        System.err.println("ParserConfigurationException in parsing XML data, will return null doc");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException in parsing XML data, will return null doc");
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            InputStream source = !systemId.startsWith("file:") ? null
                    : getClass().getResourceAsStream(
                            "/net/sf/logsupport/" + new File(URI.create(systemId)).getName());

            return source == null ? new InputSource(new StringReader("")) : new InputSource(source);
        }/*from  w  ww .jav a 2s .co  m*/
    });
    return builder;
}

From source file:Main.java

/**
 * Parses a document from the given string
 * /*from  w w  w  . ja v a2s .co m*/
 * @param template The string to parse
 * @return The parsed {@link Document}
 */
public static Document getTemplateDocument(String template) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    dbf.setValidating(false);
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
                return new InputSource(new StringReader(""));
            }
        });
        Document doc = db.parse(new ByteArrayInputStream(template.getBytes("utf8")));
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

private static PrivateKey getRSAPrivateKeyFrom(String content)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    PemReader reader = new PemReader(new StringReader(content));
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
    return KeyFactory.getInstance("RSA").generatePrivate(spec);
}

From source file:Main.java

/**
 * This method will apply an XSLT transformation
 * @param source the source reader/*from w w  w .j av a 2  s  .c o m*/
 * @param result the target writter
 * @param style the stylesheet to be applied
 * @throws TransformerException
 */
static void transform(Reader source, Writer result, String style) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(style)));
    transformer.transform(new StreamSource(source), new StreamResult(result));
}

From source file:com.haulmont.bali.util.Dom4j.java

public static Document readDocument(String xmlString) {
    return readDocument(new StringReader(xmlString));
}

From source file:io.hakbot.util.JsonUtil.java

/**
 * Creates a JsonObject (a Map implementation) from a json-formatted byte[] array
 *//*from w w w.j ava 2 s  . c o  m*/
public static JsonObject toJsonObject(byte[] jsonBytes) {
    JsonReader jsonReader = Json.createReader(new StringReader(new String(jsonBytes)));
    return jsonReader.readObject();
}