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 string2Document(String xml) throws Exception {
    InputSource src = new InputSource(new StringReader(xml));
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);//from w w w.  j  a va  2s  . com
    return dbFactory.newDocumentBuilder().parse(src);
}

From source file:Main.java

private static Document getDocument(String xml) throws ParserConfigurationException, SAXException, IOException {
    // Create a builder factory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from ww  w  .ja v  a2s  .c  o m

    return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

private static Document parseXmlFile(String in) {
    try {/*ww  w .  j av  a 2 s.  co m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * Creates a JsonObject (a Map implementation) from a json-formatted string
 *///from  w  ww.j  a v a 2s  .c om
public static JsonObject toJsonObject(String jsonString) {
    JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
    return jsonReader.readObject();
}

From source file:jfix.zk.Medias.java

public static Reader asReader(Media media) {
    return new BufferedReader(
            media.inMemory() ? new StringReader(media.getStringData()) : media.getReaderData());
}

From source file:me.smoe.adar.utils.cam.o.common.SentenceAnalyzer.java

public static Set<String> analyzer(String sentence) throws Exception {
    if (StringUtils.isEmpty(sentence)) {
        return Collections.emptySet();
    }/* w  ww.  jav a  2  s . c  o  m*/

    Analyzer analyzer = new StandardAnalyzer();
    try {
        TokenStream tokenStream = analyzer.tokenStream(StringUtils.EMPTY, new StringReader(sentence));
        tokenStream.addAttribute(CharTermAttribute.class);
        tokenStream.reset();

        Set<String> words = new LinkedHashSet<>();
        while (tokenStream.incrementToken()) {
            String word = ((CharTermAttribute) tokenStream.getAttribute(CharTermAttribute.class)).toString();

            if (word.length() <= 1) {
                continue;
            }

            words.add(word);
        }

        return words;
    } finally {
        analyzer.close();
    }
}

From source file:Main.java

/** Read an XML file into a DOM tree
 * /*w  w  w.  jav a2  s. co  m*/
 * @param file the file to read
 * @return a new XML document
 * @throws SAXException if an XML parse error occurs
 * @throws IOException if a file I/O error occurs
 */
public static Document read(File file) throws SAXException, IOException {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        // Workaround for missing external DTD.  When the parser looks up
        // an external reference, we provide an empty document back.  While
        // not correct, we don't expect SVG files loaded by this program
        // to depend on externally defined entities; nor do we require
        // validation.
        //
        // http://stackoverflow.com/questions/2640825/how-to-parse-a-xhtml-ignoring-the-doctype-declaration-using-dom-parser
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });

        return builder.parse(file);
    } catch (ParserConfigurationException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());
    String text = "<html>one<br>two<br><a name =\"three\"></a>three<br>four<br>five<br>six<br>seven<br>eight<br>nine<br>ten</html>";
    StringReader reader = new StringReader(text);

    html = new JEditorPane();
    html.setContentType("text/html");

    try {//from ww  w . jav  a 2 s  .  com
        html.read(reader, null);
    } catch (Exception e) {
        System.out.println(e);
    }

    JScrollPane scrollPane = new JScrollPane(html);
    scrollPane.setPreferredSize(new Dimension(400, 100));
    add(scrollPane);
    html.scrollToReference("three");

}

From source file:Main.java

/**
 * @param xmlsource/*w  ww  .ja  v a2s .co m*/
 * @param xsltfile
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, File xsltfile) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltfile);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

/**
 * Format the provided XML input.//from ww  w .  j a v a2  s. com
 * 
 * @param input
 *            XML input to format.
 * @param indent
 *            Indentation to use on formatted XML.
 * @return Formatted XML.
 * @throws TransformerException
 *             if some problem occur while processing the xml
 * @see #prettyFormat(String)
 */
public static String prettyFormat(String input, Integer indent) throws TransformerException {
    if (input != null) {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
                String.valueOf(indent == null ? 2 : indent));
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    }
    return input;
}