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

private static InputSource toInputSource(String xml) {
    InputSource iso = new InputSource(new StringReader(xml));
    return iso;/*from  w w  w . ja v a2 s .  c  o  m*/
}

From source file:Main.java

public final static void applyXSLTTransform(final String xslt, final Reader input, final Writer output)
        throws TransformerException {
    applyXSLTTransform(new StringReader(xslt), input, output);
}

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

private static PublicKey getRSAPublicKeyFrom(String content)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    PemReader reader = new PemReader(new StringReader(content));
    EncodedKeySpec spec = new X509EncodedKeySpec(reader.readPemObject().getContent());
    return KeyFactory.getInstance("RSA").generatePublic(spec);
}

From source file:Main.java

/**
 * Reads an xml string into XML Document.
 * //from   w ww . java2 s .c  om
 * @param xmlStr String containing xml
 * @return xml Document
 * @throws Exception
 */
public static Document readXmlString(String xmlStr) throws Exception {

    Document doc = null;

    try {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);

        DocumentBuilder builder = domFactory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlStr));
        doc = builder.parse(inStream);
    } catch (ParserConfigurationException e) {
        throw new Exception(e.getMessage(), e);
    } catch (SAXException e) {
        throw new Exception(e.getMessage(), e);
    } catch (IOException e) {
        throw new Exception(e.getMessage(), e);
    } catch (Exception e) {
        throw new Exception(e.getMessage(), e);
    }

    return doc;

}

From source file:Main.java

/**
 * This method validate XML by input XML as String and XSD File.
 *
 * @param xml input XML as String//w  w w.java2 s .co m
 * @param xsd input XSD File
 *
 * @return true or false, valid or not
 */
public static boolean validateXMLByXSD(String xml, File xsd) {
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd).newValidator()
                .validate(new StreamSource(new StringReader(xml)));
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static Document getXMLDocumentFromString(String xml)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;// w  w  w  . ja va  2  s . c  o  m
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xml)));
    return doc;
}

From source file:Main.java

public static Document getXMLDocumentFromScratch(String rootElementName)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;/*from www.j  av a  2 s .c  o m*/
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader("<" + rootElementName + "></" + rootElementName + ">")));
    return doc;
}

From source file:Main.java

public static Element getElementFromXml(String xmltext)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    StringReader sr = new StringReader(xmltext);
    InputSource is = new InputSource(sr);
    Document document = db.parse(is);
    return document.getDocumentElement();
}

From source file:Main.java

/**
 * This method makes the conversion XML -> JAVA with JAXB.
 * //from   www  .j  ava 2s . c  o  m
 * @param xml
 *            The XML to convert.
 * @param classType
 *            The target class to convert.
 * @param <E>
 *            Class type that resolves to the given XML's NameSpace.
 * @return The java object representation of the passed XML.
 * @throws JAXBException
 *             If some problem occur while making the conversion.
 */
@SuppressWarnings("unchecked")
public static <E> E getObjectByXML(String xml, Class<E> classType) throws JAXBException {
    E retObj = null;
    if (xml != null) {
        Unmarshaller unmarshaller = getJaxbContext(classType).createUnmarshaller();
        retObj = (E) unmarshaller.unmarshal(new StringReader(stripNonValidXMLCharacters(xml)));
    }
    return retObj;
}

From source file:de.micromata.genome.util.text.StandardHeaderSplitter.java

/**
 * Split./*from  w w w . ja va2s . c  om*/
 *
 * @param text the text
 * @return the pair
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Pair<String, Map<String, String>> split(String text) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new StringReader(text));
    Map<String, String> headers = new HashMap<String, String>();
    String line = null;
    boolean leedingNewlines = true;
    while ((line = lnr.readLine()) != null) {
        if (StringUtils.isBlank(line)) {
            if (leedingNewlines == true) {// es kann sein, dass am Anfang die Newlines sind(wegen Code, etc)
                continue;
            } else {
                break;
            }
        }
        String key = StringUtils.substringBefore(line, ":");
        String value = StringUtils.substringAfter(line, ":");
        headers.put(StringUtils.trim(key), StringUtils.trim(value));
        leedingNewlines = false;
    }
    if (headers.size() == 0) {
        return new Pair<String, Map<String, String>>(text, headers);
    }
    return new Pair<String, Map<String, String>>(slurp(lnr), headers);
}