List of usage examples for java.io StringWriter getBuffer
public StringBuffer getBuffer()
From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java
/** * Convert inputsource to string. Used on InputSourceHandle * @param fileRead/*from ww w .j a v a 2s . co m*/ * @return * @throws IOException * @throws TransformerException */ public String convertInputSourceToString(InputSource fileRead) throws IOException, TransformerException { SAXSource saxsrc = new SAXSource(fileRead); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(saxsrc, new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java
/** * Convert xml document to string. Useful for debugging purpose * @param readContent// w w w .jav a2s . c om * @return * @throws TransformerException */ public String convertXMLDocumentToString(Document readContent) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(readContent), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:edu.stanford.muse.util.Util.java
public static String stackTrace(Throwable t) { StringWriter sw = new StringWriter(0); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw);//from ww w.j a v a2 s. c om pw.close(); return sw.getBuffer().toString(); }
From source file:com.sshtools.sshvnc.SshVNCPanel.java
public void init(SshToolsApplication application) throws SshToolsApplicationException {/*from w w w.ja v a 2 s .c o m*/ super.init(application); setLayout(new BorderLayout()); sendTimer = new javax.swing.Timer(500, this); sendTimer.setRepeats(false); receiveTimer = new javax.swing.Timer(500, this); receiveTimer.setRepeats(false); statusBar = new StatusBar(); statusBar.setUser(""); statusBar.setHost(""); statusBar.setStatusText("Disconnected"); statusBar.setConnected(false); setLayout(new BorderLayout()); // Create our terminal emulation object try { emulation = createEmulation(); } catch (IOException ioe) { throw new SshToolsApplicationException(ioe); } // Set a scrollbar for the terminal - doesn't seem to be as simple as this emulation.setBufferSize(1000); // Create our swing terminal and add it to the main frame terminal = new TerminalPanel(emulation); terminal.requestFocus(); // try { vnc = new SshVNCViewer(); // Additional connection tabs additionalTabs = new SshToolsConnectionTab[] { new VNCTab( /*vnc*/), new VNCAdvancedTab( /*vnc*/) }; add(vnc, BorderLayout.CENTER); initActions(); } catch (Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); IconWrapperPanel p = new IconWrapperPanel( UIManager.getIcon("OptionPane.errorIcon"), new ErrorTextBox( ((t.getMessage() == null) ? "<no message supplied>" : t.getMessage()) + (debug ? ("\n\n" + sw.getBuffer().toString()) : ""))); p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); add(p, BorderLayout.CENTER); throw new SshToolsApplicationException( "Failed to start SshVNC. ", t); } }
From source file:org.ambraproject.article.service.XslIngestArchiveProcessor.java
/** * Run the zip file through the xsl stylesheet * * @param zip the zip archive containing the items to ingest * @param zipInfo the document describing the zip archive (adheres to zip.dtd) * @param articleXml the stylesheet to run on <var>zipInfo</var>; this is the main script * @param doiUrlPrefix DOI URL prefix//from w ww . j av a 2s. co m * @return a document describing the fedora objects to create (must adhere to fedora.dtd) * @throws javax.xml.transform.TransformerException * if an error occurs during the processing */ private Document transformZip(ZipFile zip, String zipInfo, Document articleXml, String doiUrlPrefix) throws TransformerException, URISyntaxException { Transformer t = getTranslet(articleXml); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.setURIResolver(new ZipURIResolver(zip)); // override the doi url prefix if one is specified in the config if (doiUrlPrefix != null) t.setParameter("doi-url-prefix", doiUrlPrefix); /* * Note: it would be preferable (and correct according to latest JAXP specs) to use * t.setErrorListener(), but Saxon does not forward <xls:message>'s to the error listener. * Hence we need to use Saxon's API's in order to get at those messages. */ final StringWriter msgs = new StringWriter(); MessageWarner em = new MessageWarner(); ((Controller) t).setMessageEmitter(em); t.setErrorListener(new ErrorListener() { public void warning(TransformerException te) { log.warn("Warning received while processing zip", te); } public void error(TransformerException te) { log.warn("Error received while processing zip", te); msgs.write(te.getMessageAndLocation() + '\n'); } public void fatalError(TransformerException te) { log.warn("Fatal error received while processing zip", te); msgs.write(te.getMessageAndLocation() + '\n'); } }); Source inp = new StreamSource(new StringReader(zipInfo), "zip:/"); DOMResult res = new DOMResult(); try { t.transform(inp, res); } catch (TransformerException te) { if (msgs.getBuffer().length() > 0) { log.error(msgs.getBuffer().toString()); throw new TransformerException(msgs.toString(), te); } else throw te; } if (msgs.getBuffer().length() > 0) throw new TransformerException(msgs.toString()); return (Document) res.getNode(); }
From source file:com.evolveum.midpoint.schema.xjc.schema.SchemaProcessor.java
private String extractString(JAnnotationValue jAnnValue) { StringWriter writer = new StringWriter(); JFormatter formatter = new JFormatter(writer); jAnnValue.generate(formatter);/*from w ww . j a v a 2 s . c o m*/ String value = writer.getBuffer().toString(); return value.substring(1, value.length() - 1); }
From source file:com.sshtools.powervnc.PowerVNCPanel.java
public void init(SshToolsApplication application) throws SshToolsApplicationException { System.out.println("init"); super.init(application); setLayout(new BorderLayout()); sendTimer = new javax.swing.Timer(500, this); sendTimer.setRepeats(false);/* w w w . j a v a 2s.c o m*/ receiveTimer = new javax.swing.Timer(500, this); receiveTimer.setRepeats(false); statusBar = new StatusBar(); statusBar.setUser(""); statusBar.setHost(""); statusBar.setStatusText("Disconnected"); statusBar.setConnected(false); setLayout(new BorderLayout()); // Create our terminal emulation object try { emulation = createEmulation(); } catch (IOException ioe) { throw new SshToolsApplicationException(ioe); } // Set a scrollbar for the terminal - doesn't seem to be as simple as this emulation.setBufferSize(1000); // Create our swing terminal and add it to the main frame terminal = new TerminalPanel(emulation); terminal.requestFocus(); // try { vnc = new VNCMain(this); // Additional connection tabs additionalTabs = new SshToolsConnectionTab[] { new VNCTab( /*vnc*/), new VNCAdvancedTab( /*vnc*/) }; add(vnc, BorderLayout.CENTER); initActions(); } catch (Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); IconWrapperPanel p = new IconWrapperPanel( UIManager.getIcon("OptionPane.errorIcon"), new ErrorTextBox( ((t.getMessage() == null) ? "<no message supplied>" : t.getMessage()) + (debug ? ("\n\n" + sw.getBuffer().toString()) : ""))); p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); add(p, BorderLayout.CENTER); throw new SshToolsApplicationException( "Failed to start SshVNC. ", t); } }
From source file:mondrian.gui.Workbench.java
/** * Validates that the schema can be parsed and loaded, * showing a warning message if any errors are encountered. *//*from w w w . j av a 2s .co m*/ private boolean isSchemaValid(MondrianGuiDef.Schema schema) { try { StringWriter writer = new StringWriter(); XMLOutput xmlOutput = new XMLOutput(writer); schema.displayXML(xmlOutput); Parser xmlParser = XOMUtil.createDefaultParser(); Reader reader = new StringReader(writer.getBuffer().toString()); // attempt to create a new schema new MondrianGuiDef.Schema(xmlParser.parse(reader)); } catch (XOMException e) { JOptionPane.showMessageDialog(this, getResourceConverter().getFormattedString("workbench.save.invalid.schema", "Please correct the following error before saving:", e.getLocalizedMessage()), getResourceConverter().getFormattedString("workbench.save.invalid.schema.title", "Cannot Save"), JOptionPane.WARNING_MESSAGE); return false; } return true; }
From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java
/** * Convert source to string. Used on SourceHandle * @param fileRead// w ww . j a v a 2 s.c om * @return * @throws IOException * @throws TransformerException */ public String convertSourceToString(Source reader) throws IOException, TransformerException { StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(reader, result); String str = stringWriter.getBuffer().toString(); return str; }
From source file:com.gdo.servlet.RpcWrapper.java
private void facet(StclContext stclContext, RpcArgs args) throws IOException { HttpServletResponse response = stclContext.getResponse(); try {/*from w w w. j a va 2 s.c o m*/ // gets facet type and mode String type = args.getStringParameter(stclContext, FACETS_PARAM); String mode = args.getStringParameter(stclContext, MODES_PARAM); if (StringUtils.isBlank(type)) { // the type is undefined String msg = String.format("no facet defined (param %s)", FACETS_PARAM); response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); return; } // gets stencil PStcl stcl = args.getStencilFromPath(stclContext); if (StencilUtils.isNull(stcl)) { // null stencil may be accepted if (args.acceptNoStencil()) { StudioGdoServlet.writeHTMLResponse(stclContext.getResponse(), "", args.getCharacterEncoding(stclContext)); return; } // stencil may not be null String reason = StencilUtils.getNullReason(stcl); String msg = String.format("facet service : cannot found stencil at path %s : %s", args.getPath(), reason); response.sendError(HttpServletResponse.SC_NO_CONTENT, msg); return; } // searches facet from stencil RenderContext<StclContext, PStcl> renderCtxt = new RenderContext<StclContext, PStcl>(stclContext, stcl, type, mode); FacetResult facetResult = stcl.getFacet(renderCtxt); if (facetResult.isNotSuccess()) { // error in facet response.sendError(HttpServletResponse.SC_NOT_FOUND, facetResult.getMessage()); return; } // HTML facet if (FacetType.HTML.equals(type)) { StringWriter writer = new StringWriter(); writer.write("<html>\n"); writer.write(" <META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"); writer.write(" <META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">\n"); writer.write(" <META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\">\n"); writer.write("<body>\n"); IOUtils.copy(facetResult.getInputStream(), writer); facetResult.closeInputStream(); writer.write("</body>\n</html>\n"); String content = stcl.format(stclContext, writer.getBuffer().toString()); StudioGdoServlet.writeHTMLResponse(response, content, args.getCharacterEncoding(stclContext)); return; } // HTML 5 facet or JSON facet if (FacetType.HTML5.equals(type) || FacetType.DOM5.equals(type) || FacetType.TRANS.equals(type) || FacetType.JSON.equals(type) || FacetType.JSKEL.equals(type) || FacetType.PYTHON.equals(type) || FacetType.REST.equals(type)) { String mime = facetResult.getMimeType(); InputStream in = facetResult.getInputStream(); StudioGdoServlet.writeResponse(stclContext.getResponse(), HttpServletResponse.SC_OK, mime, in, StclContext.getCharacterEncoding()); return; } // file facet if (FacetType.FILE.equals(type)) { if (FacetType.E4X.equals(mode)) { InputStream in = facetResult.getInputStream(); String enc = StclContext.getCharacterEncoding(); StudioGdoServlet.writeXMLResponse(stclContext.getResponse(), in, enc); facetResult.closeInputStream(); return; } CatalinaUtils.writeFileResponse(stclContext, facetResult); return; } // write result Reader reader = new InputStreamReader(facetResult.getInputStream()); XmlStringWriter writer = new XmlStringWriter(args.getCharacterEncoding(stclContext)); writer.startElement("result"); args.writeAttributes(stclContext, stcl, false, writer); addStatus(writer, Result.success()); // not escaped as XML may be used in data writer.writeCDATAElement("data", StringHelper.read(reader)); writer.endElement("result"); // trace and response String xml = writer.getString(); logTrace(stclContext, xml); StudioGdoServlet.writeXMLResponse(stclContext.getResponse(), xml, args.getCharacterEncoding(stclContext)); facetResult.closeInputStream(); } catch (Exception e) { String msg = logError(stclContext, e.toString()); response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); return; } }