List of usage examples for java.io StringReader close
public void close()
From source file:ORG.oclc.os.SRW.SRWDatabase.java
private static ExtraDataType makeExtraDataType(String extraData) { ExtraDataType edt = null;/*from w w w. j a v a 2 s .c o m*/ // extraData is always encoded as "xml" Document domDoc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { DocumentBuilder db = dbf.newDocumentBuilder(); StringReader sr = new StringReader("<bogus>" + extraData + "</bogus>"); domDoc = db.parse(new InputSource(sr)); sr.close(); Element el = domDoc.getDocumentElement(); NodeList nodes = el.getChildNodes(); MessageElement elems[] = new MessageElement[nodes.getLength()]; for (int i = 0; i < elems.length; i++) elems[i] = new MessageElement((Element) nodes.item(i)); edt = new ExtraDataType(); edt.set_any(elems); domDoc = null; } catch (IOException e) { log.error(e, e); } catch (ParserConfigurationException e) { log.error(e, e); } catch (SAXException e) { log.error(e, e); try { log.error("Bad ExtraResponseData:\n" + Utilities.byteArrayToString(extraData.getBytes("UTF8"))); } catch (UnsupportedEncodingException e2) { } // can't happen } return edt; }
From source file:ORG.oclc.os.SRW.SRWDatabase.java
private static de.escidoc.core.domain.sru.ExtraDataType makeSruExtraDataType(String extraData) { de.escidoc.core.domain.sru.ExtraDataType edt = null; // extraData is always encoded as "xml" Document domDoc;/* w ww . jav a 2 s .com*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { DocumentBuilder db = dbf.newDocumentBuilder(); StringReader sr = new StringReader("<bogus>" + extraData + "</bogus>"); domDoc = db.parse(new InputSource(sr)); sr.close(); Element el = domDoc.getDocumentElement(); NodeList nodes = el.getChildNodes(); List<Object> elems = new ArrayList<Object>(); for (int i = 0; i < nodes.getLength(); i++) elems.add((Element) nodes.item(i)); edt = new de.escidoc.core.domain.sru.ExtraDataType(); edt.getAny().addAll(elems); domDoc = null; } catch (IOException e) { log.error(e, e); } catch (ParserConfigurationException e) { log.error(e, e); } catch (SAXException e) { log.error(e, e); try { log.error("Bad ExtraResponseData:\n" + Utilities.byteArrayToString(extraData.getBytes("UTF8"))); } catch (UnsupportedEncodingException e2) { } // can't happen } return edt; }
From source file:org.rhq.enterprise.server.sync.test.DeployedAgentPluginsValidatorTest.java
public void testCanExportAndImportState() throws Exception { final PluginManagerLocal pluginManager = context.mock(PluginManagerLocal.class); final DeployedAgentPluginsValidator validator = new DeployedAgentPluginsValidator(pluginManager); context.checking(new Expectations() { {//from ww w.java2 s . c o m oneOf(pluginManager).getInstalledPlugins(); will(returnValue(new ArrayList<Plugin>(getDeployedPlugins()))); } }); validator.initialize(null, null); StringWriter output = new StringWriter(); try { XMLOutputFactory ofactory = XMLOutputFactory.newInstance(); XMLStreamWriter wrt = ofactory.createXMLStreamWriter(output); //wrap the exported plugins in "something" so that we produce //a valid xml wrt.writeStartDocument(); wrt.writeStartElement("root"); validator.exportState(new ExportWriter(wrt)); wrt.writeEndDocument(); wrt.close(); StringReader input = new StringReader(output.toString()); try { XMLInputFactory ifactory = XMLInputFactory.newInstance(); XMLStreamReader rdr = ifactory.createXMLStreamReader(input); //push the reader to the start of the plugin elements //this is what is expected by the validators rdr.nextTag(); validator.initializeExportedStateValidation(new ExportReader(rdr)); rdr.close(); assertEquals(validator.getPluginsToValidate(), getDeployedPlugins()); } finally { input.close(); } } catch (Exception e) { LOG.error("Test failed. Output generated so far:\n" + output, e); throw e; } finally { output.close(); } }
From source file:org.trpr.platform.integration.impl.xml.XMLTranscoderImpl.java
/** * Interface method implementation. Note that the package name specified should contain JAXB compatible generated classes/artifacts. * @see XMLTranscoder#unmarshal(String, Class) *//*from ww w. ja v a 2s . c om*/ @SuppressWarnings("unchecked") public <T> T unmarshal(String xml, Class<T> clazz) throws XMLDataException { StringReader stringReader = new StringReader(xml); try { if (this.getUnmarshaller() == this.defaultUnmarshaller) { // the default unmarshaller is not initialized with the context path. Initialize it by calling suitable methods this.defaultUnmarshaller.setContextPath(clazz.getPackage().getName()); this.defaultUnmarshaller.afterPropertiesSet(); } return (T) this.getUnmarshaller().unmarshal(new StreamSource(stringReader)); } catch (Exception e) { throw new XMLDataException( "Error unmarshalling XML. XML:packageName is " + xml + ":" + clazz.getPackage().getName(), e); } finally { stringReader.close(); } }
From source file:com.taobao.diamond.server.service.task.processor.RemoveConfigInfoTaskProcessor.java
@SuppressWarnings("unchecked") private String generateNewContent(String oldContent, String content) throws Exception { StringBuilder sb = new StringBuilder(); StringReader strReader = new StringReader(oldContent); try {/* w ww . j a v a 2 s . com*/ List<String> lines = IOUtils.readLines(strReader); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!line.contains(content)) { sb.append(line); if (i != lines.size() - 1) { sb.append(Constants.DIAMOND_LINE_SEPARATOR); } } } String result = sb.toString(); if (result.endsWith(Constants.DIAMOND_LINE_SEPARATOR)) { result = result.substring(0, result.lastIndexOf(Constants.DIAMOND_LINE_SEPARATOR)); } return result; } finally { strReader.close(); } }
From source file:org.apache.manifoldcf.agents.output.opensearchserver.OpenSearchServerConnection.java
private void readXmlResponse() throws ManifoldCFException { if (xmlResponse != null) return;// ww w . ja va 2 s.c o m StringReader sw = null; try { sw = new StringReader(response); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); // never forget this! DocumentBuilder builder; builder = dbf.newDocumentBuilder(); xmlResponse = builder.parse(new InputSource(sw)); } catch (ParserConfigurationException e) { throw new ManifoldCFException(e); } catch (SAXException e) { throw new ManifoldCFException(e); } catch (IOException e) { throw new ManifoldCFException(e); } finally { if (sw != null) { sw.close(); } } }
From source file:org.bonitasoft.engine.api.HTTPServerAPI.java
@SuppressWarnings("unchecked") private <T> T fromXML(final String object, final XStream xstream) { final StringReader xmlReader = new StringReader(object); ObjectInputStream in = null;/*from w w w. j a v a 2 s . co m*/ try { in = xstream.createObjectInputStream(xmlReader); try { return (T) in.readObject(); } catch (final IOException e) { throw new BonitaRuntimeException("unable to deserialize object " + object, e); } catch (final ClassNotFoundException e) { throw new BonitaRuntimeException("unable to deserialize object " + object, e); } catch (final CannotResolveClassException e) { throw new BonitaRuntimeException("unable to deserialize object " + object, e); } finally { in.close(); xmlReader.close(); } } catch (final IOException e) { throw new BonitaRuntimeException("unable to deserialize object " + object, e); } }
From source file:org.gvnix.jpa.geo.hibernatespatial.util.EWKTReader.java
/** * Reads a Well-Known Text representation of a * {@link com.vividsolutions.jts.geom.Geometry} from a {@link String}. * //w w w . j a v a2 s . co m * @param wellKnownText one or more <Geometry Tagged Text>strings (see the * OpenGIS Simple Features Specification) separated by whitespace * @return a <code>Geometry</code> specified by <code>wellKnownText</code> * @throws com.vividsolutions.jts.io.ParseException if a parsing problem * occurs */ public Geometry read(String wellKnownText) throws ParseException { StringReader reader = new StringReader(fixMNames(wellKnownText)); try { return read_internal(reader); } finally { reader.close(); } }
From source file:edu.unc.lib.dl.ingest.aip.RDFAwareAIPImpl.java
public void addRELSEXT2Graph() throws AIPException { for (PID pid : this.baseAIP.getPIDs()) { Document doc = this.baseAIP.getFOXMLDocument(pid); String str = getRELSEXT(doc); if (str == null) { continue; }//from www . j a va 2 s . co m StringReader r = new StringReader(str); try { Parser parser = new GraphRdfXmlParser(this.graph); parser.parse(r, "http://example.com/"); } catch (GraphException e) { log.error(e); } catch (IOException e) { log.error(e); } catch (ParseException e) { log.error(e); } catch (StatementHandlerException e) { log.error(e); } finally { r.close(); } } }
From source file:com.thingsee.tracker.REST.KiiBucketRequestAsyncTask.java
private JSONArray readSensorDataFromString(String input, int offset) { StringReader reader = new StringReader(input); try {/*from w ww . j a v a2s . com*/ reader.skip(offset); } catch (IOException e1) { e1.printStackTrace(); } JsonReader jsonReader = new JsonReader(reader); JSONArray jsonArray = new JSONArray(); try { jsonReader.beginArray(); while (jsonReader.hasNext()) { JSONObject jsonObject = readSingleData(jsonReader); jsonArray.put(jsonObject); } jsonReader.endArray(); } catch (IOException e) { // Ignore for brevity } catch (JSONException e) { // Ignore for brevity } try { jsonReader.close(); } catch (IOException e) { // Ignore for brevity } reader.close(); return jsonArray; }