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 Document parseXML(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilder builder;//  ww w .j  a va 2 s . c  o m
    synchronized (factory) {
        builder = factory.newDocumentBuilder();
    }
    return builder.parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

public static Node getRoot(String documentAsString) {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {//from w w w  . ja  v a  2s  .com
        final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        final Document document = documentBuilder.parse(new InputSource(new StringReader(documentAsString)));
        return document.getFirstChild();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    } catch (SAXException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

/**
 * /*from w  w  w. j  a  v  a 2s . c  o  m*/
 * Checks indentation (over a single line - multipline text nodes is not supported)
 * 
 * @param out
 * @param indentSize
 * @return
 * @throws Exception
 */

public static boolean isIndented(String out, int indentSize) throws Exception {
    BufferedReader reader = new BufferedReader(new StringReader(out));

    boolean indentated = false;

    int level = 0;
    int line = 0;

    String string = reader.readLine();
    while (string != null) {
        int newLevel = 0;
        while (newLevel < string.length()) {
            if (!Character.isWhitespace(string.charAt(newLevel))) {
                break;
            }
            newLevel++;
        }
        if ((newLevel % indentSize) != 0) {
            throw new IllegalArgumentException("Unexpected " + newLevel + " whitespace chars at line " + line);
        }
        if (Math.abs(level - newLevel) > indentSize) {
            throw new IllegalArgumentException("Unexpected jump from " + level + " to " + newLevel
                    + " whitespace chars at line " + line + " for indenting with " + indentSize + " chars");
        }
        level = newLevel;

        string = reader.readLine();
        line++;

        if (level > 0) {
            indentated = true;
        }
    }

    if (!indentated) {
        // see if a simple xml piece
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(out));

        int elementMaxLevel = -1;
        int elementLevel = 0;
        do {
            int event = parser.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                elementLevel++;

                if (elementMaxLevel < elementLevel) {
                    elementMaxLevel = elementLevel;
                }
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                elementLevel--;
            }
        } while (parser.hasNext());

        if (elementMaxLevel > 1) { // should be indentated
            return false;
        }
        return true;
    }

    return indentated;
}

From source file:utils.FreeMarkerUtils.java

public static String renderString(String templateString, Map<String, ?> model) {
    try {//  w  w  w .  java  2s  .  c  om
        StringWriter result = new StringWriter();
        Template t = new Template("name", new StringReader(templateString), new Configuration());
        t.process(model, result);
        return result.toString();
    } catch (Exception e) {
        // throw e;
    }
    return "";
}

From source file:Main.java

public static boolean parseXMLMessage(String msg, HashMap<String, String> strMap) {
    boolean bRet = true;

    // parse xml document.
    XmlPullParserFactory factory;//from  w ww.  j a  v  a 2s . co  m
    XmlPullParser parser;

    try {
        factory = XmlPullParserFactory.newInstance();
        parser = factory.newPullParser();
        Stack eleStack = new Stack();
        int parserEvent;

        parser.setInput(new StringReader(msg));
        parserEvent = parser.getEventType();

        while (parserEvent != XmlPullParser.END_DOCUMENT) {
            switch (parserEvent) {
            case XmlPullParser.START_TAG:
                String newtag = parser.getName();
                if (newtag.compareTo("xml") != 0) {
                    eleStack.push(newtag);
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getName().compareTo("xml") != 0) {
                    eleStack.pop();
                }
                break;
            case XmlPullParser.TEXT:
                String tagkey = "";
                for (int i = 0; i < eleStack.size(); i++) {
                    tagkey += eleStack.elementAt(i);
                    if (i < eleStack.size() - 1)
                        tagkey += "_";
                }
                strMap.put(tagkey, parser.getText());
                break;
            default:
                break;
            }

            parserEvent = parser.next();
        }

        eleStack = null;
        parser = null;
        factory = null;
    } catch (Exception e) {
        e.printStackTrace();
        bRet = false;
    }

    return bRet;
}

From source file:Main.java

/**
 * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string
 * @param xmlStr//from   w  w  w. j a v a2s  . co  m
 * @return
 */
public static Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document toDocument(String content)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    return content == null ? null : db.parse(new InputSource(new StringReader(content)));
}

From source file:Main.java

public static Document createDocumentFromXml(String input)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    StringReader stringReader = new StringReader(input);
    InputSource inputSource = new InputSource(stringReader);
    return documentBuilder.parse(inputSource);
}

From source file:Main.java

/**
 * Parses an XML string into a DOM.//from   ww  w.  jav a2  s  .c om
 * @param xml the XML string
 * @return the parsed DOM
 * @throws SAXException if the string is not valid XML
 */
public static Document toDocument(String xml) throws SAXException {
    try {
        return toDocument(new StringReader(xml));
    } catch (IOException e) {
        //reading from string
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Convert and xml String to an org.w3c.dom.Element
 * @param xml - the XML//  w w  w  .  j  av  a 2s.co  m
 * @return - the Element
 */
public static Element stringToElement(String xml) {
    Element element = null;
    try {
        Document document = getDocumentBuilder().parse(new InputSource(new StringReader(xml)));
        element = (Element) document.getDocumentElement().cloneNode(true);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return element;
}