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:io.kaif.kmark.KmarkProcessor.java

public static String process(final String input) {
    try {/* w  w w.  j a va2  s .co  m*/
        return new KmarkProcessor().process(new StringReader(input));
    } catch (IOException ignore) {
        //never happen, it's string reader
        return null;
    }
}

From source file:Main.java

private static String flattenXmlRecord(final String xmlRecord) throws XMLStreamException {
    StringBuilder flatXmlRecord = new StringBuilder();
    XMLEventReader eventReader = inFactory.createXMLEventReader(new StringReader(xmlRecord));
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.getEventType() != XMLEvent.START_ELEMENT) {
            continue;
        }/*from  w ww .ja va 2 s .c  o  m*/
        String elementName = event.asStartElement().getName().getLocalPart();
        event = eventReader.nextEvent();
        if (event.getEventType() != XMLEvent.CHARACTERS) {
            continue;
        }
        if (event.asCharacters().getData().trim().isEmpty()) {
            continue;
        }
        flatXmlRecord.append(elementName + ", " + event.asCharacters().getData() + "\n");

    }
    return flatXmlRecord.toString();
}

From source file:Main.java

public static String prettyFormat(String input, int indent) {
    try {/*from  w w  w. j av  a2s.  co  m*/
        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) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

public static Document loadDocument(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    //skip DTD validation
    builder.setEntityResolver(new EntityResolver() {

        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }/*  ww w .ja  v  a  2 s .com*/
    });

    return builder.parse(is);
}

From source file:Main.java

/**
 * Formats an XML string for pretty printing. Requires Java 1.6.
 * //  ww w .  j  ava2 s .c om
 * Taken from http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
 * 
 * @param xml
 * @return pretty-print formatted XML
 */
public static String prettyPrintXml(String xml) {
    try {
        final InputSource src = new InputSource(new StringReader(xml));
        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
                .getDocumentElement();
        final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

        //May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        final LSSerializer writer = impl.createLSSerializer();

        writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
        writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.

        return writer.writeToString(document);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static Transformer getTransformerFromText(String xsltContent) throws TransformerConfigurationException {
    if (xsltContent == null) {
        return getDefaultTransformer();
    } else {//from  w w w.j av a2 s.  co m
        checkTransformFactory();
        return transformerFactory.newTransformer(new StreamSource(new StringReader(xsltContent)));
    }
}

From source file:Main.java

/**
 * parse document from xml String//from w w  w .j  ava2s .  co  m
 *
 * @param xml xml string
 * @throws IOException                  if failed to create XML document
 * @throws ParserConfigurationException e
 * @throws SAXException                 e
 */
public static Document loadDocument(String xml) throws IOException, ParserConfigurationException, SAXException {
    return dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

public static Document documentFromString(final String documentContents) throws Exception {

    return createDocumentBuilder().parse(new InputSource(new StringReader(documentContents)));
}

From source file:Main.java

public static <T> T unmarshal(String s, Class<T> clazz)
        throws JAXBException, XMLStreamException, FactoryConfigurationError {
    StringReader sr = new StringReader(s);
    return unmarshal(sr, clazz);
}

From source file:me.smoe.adar.analyzer.luence.AnalyzerToy.java

public static void analyzerByStop(String sentence) throws Exception {
    Analyzer analyzer = new StopAnalyzer();

    TokenStream tokenStream = analyzer.tokenStream(StringUtils.EMPTY, new StringReader(sentence));
    tokenStream.addAttribute(CharTermAttribute.class);
    tokenStream.reset();//from ww  w .  j a  v a 2 s . co m
    while (tokenStream.incrementToken()) {
        CharTermAttribute charTermAttribute = (CharTermAttribute) tokenStream
                .getAttribute(CharTermAttribute.class);
        System.out.print(charTermAttribute.toString() + " ,");
    }

    analyzer.close();
}