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

/**
 * @param xml//from   w w w . j  av a  2 s . c o m
 * @return pretty xml
 */
public static String prettyXml(String xml) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    try {
        Source source = new StreamSource(new StringReader(xml));
        StringWriter writer = new StringWriter();

        serializer = tfactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (Exception e) {
        return xml;
    }
}

From source file:Main.java

/**
 * Formats a xml string/* ww  w .  ja  va 2s  .com*/
 * @param input
 * @param indent
 * @return
 */
public static String prettyFormatXml(String input, int indent) {
    try {
        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");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return input;
}

From source file:Main.java

public static Document parseXmlString(String resp)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(resp)));
}

From source file:Main.java

public static Document string2doc(String xml) throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilder builder = builderFac.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

/**
 * get multiple lines from a string with \n
 * /*www .  java 2s .com*/
 * @param strText the script to divide into single lines
 * @param strEndInstruction the character that marks the end of an instruction
 * @return
 */
public static ArrayList<String> getScriptLines(String strText, String strEndInstruction) throws Exception {

    // inits the list and the reader
    ArrayList<String> alLines = new ArrayList<String>();
    BufferedReader br = new BufferedReader(new StringReader(strText));
    String strLine = null;
    StringBuffer sbLine = new StringBuffer();

    // reads the script through the reader
    while ((strLine = br.readLine()) != null) {

        // if is a comment skips the line
        if (strLine.length() == 0 || strLine.startsWith("\n") || strLine.startsWith("--")
                || strLine.startsWith("#"))
            continue;

        // adds the script line to the buffer
        sbLine.append(strLine);

        // if it's the end of the command, adds it to the AL
        if (strLine.trim().endsWith(strEndInstruction) || strEndInstruction.equals("")) {
            alLines.add(sbLine.toString());
            sbLine = new StringBuffer("");
        }
    }

    // and return the AL
    return alLines;
}

From source file:Main.java

public static Node parseXML(String text) throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = dbf.newDocumentBuilder();
    InputSource source = new InputSource(new StringReader(text));
    Document doc = parser.parse(source);
    if (doc == null) {
        throw new NullPointerException();
    }// w ww  .  ja v  a2s .com

    return doc.getFirstChild();
}

From source file:Main.java

public static Document convertStringToDocument(String xmlStr)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(new InputSource(new StringReader(xmlStr)));
}

From source file:Main.java

public static Document stringToDocument(String xml) {
    Document doc = null;/*from   w w w. j  a  v  a 2  s .  c  om*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // Turn on validation, and turn off namespaces
    factory.setValidating(false);
    factory.setNamespaceAware(false);

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return doc;
}

From source file:Main.java

public static Document parse(String xml) throws Exception {
    return newBuilder().parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

public static Document parseTheXml(String thePage)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(thePage));

    return db.parse(is);
    //build dat DOM document!

}