Example usage for java.io StringReader close

List of usage examples for java.io StringReader close

Introduction

In this page you can find the example usage for java.io StringReader close.

Prototype

public void close() 

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:org.sakaiproject.sitestats.impl.parser.DigesterUtil.java

public static List<ReportDef> convertXmlToReportDefs(String inputString) throws Exception {
    BeanReader beanReader = getBeanReader();
    StringReader reader = null;
    List<ReportDef> reportDefs = null;
    try {/*www .j a  va 2  s.  c  om*/
        reader = new StringReader(inputString);
        reportDefs = (List<ReportDef>) beanReader.parse(reader);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return reportDefs;
}

From source file:org.orcid.utils.BibtexUtils.java

/**
 * Utility method to obtain the underlying {@link BibTeXDatabase} object
 * // w ww.  j a  va  2 s .  c om
 * @param bibtex
 *            the BinTeX as a string
 * @return the {@link BibTeXDatabase} object
 * @throws ParseException
 *             if the bibtex string is invalid
 */
public static BibTeXDatabase getBibTeXDatabase(String bibtex) throws ParseException {
    StringReader reader = null;
    try {
        reader = new StringReader(bibtex);
        return BIBTEX_PARSER.parse(reader);
    } catch (IOException e) {
        LOGGER.warn("Problem with reading the string.", e);
        throw new IllegalStateException("Problem reading from a StringReader!", e);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static JSONObject convertStringToJSONObject(String json) {
    JSONObject result = null;/*from  ww w  .j av a2  s.  co  m*/
    StringReader r = new StringReader(json);
    JSONParser jp = new JSONParser();
    try {
        result = (JSONObject) jp.parse(r);
    } catch (Throwable t) {
        System.out.println(t.getMessage());
    }
    r.close();
    return result;
}

From source file:nl.ellipsis.webdav.server.util.XMLHelper.java

public static String format(String xml) {
    String retval = xml;//from  w  ww. j a  v  a 2s .  c  o m

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    Document document = null;
    StringReader sr = null;
    try {
        sr = new StringReader(xml);
        InputSource inputSource = new InputSource(sr);
        documentBuilderFactory.setNamespaceAware(true);
        document = documentBuilderFactory.newDocumentBuilder().parse(inputSource);
    } catch (SAXException | IOException | ParserConfigurationException e) {
        e.printStackTrace();
    } finally {
        sr.close();
    }
    if (document != null) {
        retval = format(document);
    }
    return retval;
}

From source file:org.kalypso.ogc.sensor.filter.FilterFactory.java

/**
 * Creates a filtered observation if the specified url contains the specification for creating a filter. Otherwise
 * directly returns the given observation.
 * /*from w  w w  .j  a v  a 2  s  .c o  m*/
 * @return IObservation
 */
public static IObservation createFilterFrom(final String href, final IObservation obs, final URL context)
        throws SensorException {
    final String strFilterXml = getFilterPart(href);
    if (strFilterXml == null)
        return obs;
    final StringReader sr = new StringReader(strFilterXml);
    final IObservation obsFilter;
    try {
        final Unmarshaller unmarshaller = ZmlFactory.JC.createUnmarshaller();
        final JAXBElement<?> value = (JAXBElement<?>) unmarshaller.unmarshal(new InputSource(sr));
        final AbstractFilterType af = (AbstractFilterType) value.getValue();
        sr.close();
        final IFilterCreator creator = getCreatorInstance(af);
        obsFilter = creator.createFilter(af, obs, context);
    } catch (final Exception e) // generic exception caught for simplicity
    {
        throw new SensorException(e);
    } finally {
        IOUtils.closeQuietly(sr);
    }
    return obsFilter;
}

From source file:com.datastax.sparql.ConsoleCompiler.java

private static void printWithHeadline(final String headline, final Object content) throws IOException {
    final StringReader sr = new StringReader(content != null ? content.toString() : "null");
    final BufferedReader br = new BufferedReader(sr);
    String line;//from w  w  w.  j av a 2 s. c  om
    System.out.println();
    System.out.println(headline); //Not sure what this is :-/
    System.out.println();
    boolean skip = true;
    while (null != (line = br.readLine())) {
        skip &= line.isEmpty();
        if (!skip) {
            System.out.println("  " + line);
        }
    }
    System.out.println();
    br.close();
    sr.close();
}

From source file:Main.java

/**
 * Convert XML string to a XML DOM document
 *
 * @param strXML//from   w  ww.j  a  va2s.c  o  m
 *            XML
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document xmlStringToDOMDocument(String strXML) throws Exception {
    if (strXML == null) {
        throw new RuntimeException("No XML input given(null)!");
    }

    StringReader reader = null;
    Document doc = null;
    try {
        reader = new StringReader(strXML);
        InputSource inputSource = new InputSource(reader);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(inputSource);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Parsing of XML input failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return doc;
}

From source file:Pathway2Rdf.java

/**
 * @param args/*from   ww  w  . jav  a2s. c  o m*/
 * @throws XPathExpressionException
 * @throws DOMException
 * @throws ServiceException
 * @throws ConverterException
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 * @throws ParseException
 */

private static boolean isValidXML(String gpml, String wpIdentifier, String revision) {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        StringReader reader = new StringReader(gpml);
        InputSource inputSource = new InputSource(reader);
        Document doc = docBuilder.parse(inputSource);
        reader.close();
        doc.getDocumentElement().normalize();

        return true;

    } catch (SAXParseException spe) {
        System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision);
        return false;

    } catch (SAXException sxe) {
        System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision);
        return false;

    } catch (ParserConfigurationException pce) {
        System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision);
        return false;

    } catch (IOException ioe) {
        System.out.println("Invalid GPML: " + wpIdentifier + "/" + revision);
        return false;
    }

}

From source file:org.opennms.core.test.xml.XmlTest.java

protected static NodeList xpathGetNodesMatching(final String xml, final String expression)
        throws XPathExpressionException {
    final XPath query = XPathFactory.newInstance().newXPath();
    StringReader sr = null;
    InputSource is = null;/*from www .  j  a va  2s  .c om*/
    NodeList nodes = null;
    try {
        sr = new StringReader(xml);
        is = new InputSource(sr);
        nodes = (NodeList) query.evaluate(expression, is, XPathConstants.NODESET);
    } finally {
        sr.close();
        IOUtils.closeQuietly(sr);
    }
    return nodes;
}

From source file:Main.java

/**
 * Unmarshal XML data from XML file path using XSD string and return the resulting JAXB content tree
 * //from   w  w  w .  j av a2 s.c  o m
 * @param dummyCtxObject
 *            Dummy contect object for creating related JAXB context
 * @param strXMLFilePath
 *            XML file path
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromXMLFile(Object dummyCtxObject, String strXMLFilePath, String strXSD)
        throws Exception {
    if (dummyCtxObject == null) {
        throw new RuntimeException("No dummy context object (null)!");
    }
    if (strXMLFilePath == null) {
        throw new RuntimeException("No XML file path (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD (null)!");
    }

    Object unmarshalledObject = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        // unmarshaller.setValidating(true);
        /*
        javax.xml.validation.Schema schema =
        javax.xml.validation.SchemaFactory.newInstance(
        javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
        new java.io.File(m_strXSDFilePath));
         */
        StringReader reader = null;
        FileInputStream fis = null;
        try {
            reader = new StringReader(strXSD);
            javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                    .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)
                    .newSchema(new StreamSource(reader));
            unmarshaller.setSchema(schema);

            fis = new FileInputStream(strXMLFilePath);
            unmarshalledObject = unmarshaller.unmarshal(fis);
        } finally {
            if (fis != null) {
                fis.close();
                fis = null;
            }
            if (reader != null) {
                reader.close();
                reader = null;
            }
        }
        // } catch (JAXBException e) {
        // //m_logger.error(e);
        // throw new OrderException(e);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    }

    return unmarshalledObject;
}