List of usage examples for java.io StringWriter getBuffer
public StringBuffer getBuffer()
From source file:com.github.woozoo73.ht.XmlUtils.java
public static String getContent(String path) { try {//from w w w .ja v a 2 s . c om URL url = XmlUtils.class.getClassLoader().getResource(path); File schemaLocation = new File(url.toURI()); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(schemaLocation); StringWriter writer = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(document), new StreamResult(writer)); String content = writer.getBuffer().toString(); return content; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.overlord.sramp.server.servlets.MavenRepositoryServlet.java
/** * Gets the root stack trace as a string. * * @param t/* w w w . j a v a2 s. c o m*/ * the t * @return the root stack trace */ public static String getRootStackTrace(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); getRootCause(t).printStackTrace(writer); return sw.getBuffer().toString(); }
From source file:Main.java
/** * Returns a String representation of the DOM hierarchy rooted at the argument Node. * @param node The root Node of the DOM hierarchy to translate. * @return A String representation of the DOM hierarchy rooted at the * argument Node, or null if the operation fails. *//*from w w w.ja v a 2 s . com*/ public static String toXMLString(Node node, boolean header) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); if (!header) transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } // try - catch return null; }
From source file:ac.uk.diamond.sample.HttpClientTest.Utils.java
static String xmlToString(XMLObject aObject) throws IOException { Document doc;//from ww w. j av a2s. co m try { doc = Configuration.getMarshallerFactory().getMarshaller(aObject).marshall(aObject).getOwnerDocument(); } catch (MarshallingException e) { throw new IOException(e); } try { Source source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerException e) { throw new IOException(e); } }
From source file:com.atlassian.theplugin.idea.ui.DialogWithDetails.java
public static String getExceptionString(Throwable t) { StringWriter sw = new StringWriter(); if (t != null) { t.printStackTrace(new PrintWriter(sw)); }/* w ww .j a v a 2s . c o m*/ return sw.getBuffer().toString(); }
From source file:eu.sisob.uma.restserver.TaskManager.java
/** * Create a new folder for a new task if is possible to launch new task * Note:/* w w w. j a va 2 s . c o m*/ * use MAX_TASKS_PER_USER and validateAccess() * Is possible that use thee locker of AuthorizationManager * @param user * @param pass * @param status * @param message * @return */ public static String prepareNewTask(String user, String pass, StringWriter status, StringWriter message) { String new_folder_name = ""; if (message == null) return ""; message.getBuffer().setLength(0); if (user != null && pass != null) { if (AuthorizationManager.validateAccess(user, pass, message)) { message.getBuffer().setLength(0); List<String> task_code_list = TaskManager.listTasks(user, pass); int num_tasks_alive = 0; int max = -1; if (task_code_list.size() > 0) { for (String task_code : task_code_list) { OutputTaskStatus task_status = TaskManager.getTaskStatus(user, pass, task_code, false, false, false); if (task_status.status.equals(OutputTaskStatus.TASK_STATUS_EXECUTED)) { } else { //Think about it num_tasks_alive++; } try { int act = Integer.parseInt(task_code); if (max < act) { max = act; } } catch (Exception ex) { } } } if (num_tasks_alive < AuthorizationManager.MAX_TASKS_PER_USER) { new_folder_name = String.valueOf(max + 1); String code_task_folder = TASKS_USERS_PATH + File.separator + user + File.separator + new_folder_name; File task_dir = new File(code_task_folder); if (!task_dir.exists()) { task_dir.mkdir(); status.append(OutputTaskStatus.TASK_STATUS_TO_EXECUTE); if (message != null) message.append("A new task has been created successfully."); //FIXME } else { new_folder_name = ""; status.append(OutputTaskStatus.TASK_STATUS_NO_ACCESS); if (message != null) message.append("Error creating place for the new task."); //FIXME } } else { new_folder_name = ""; status.append(OutputTaskStatus.TASK_STATUS_EXECUTING); if (message != null) message.append( "There are still tasks running or there are a task created ready to be launched."); //FIXME } } else { new_folder_name = ""; status.append(OutputTaskStatus.TASK_STATUS_NO_AUTH); } } else { new_folder_name = ""; status.write(OutputTaskStatus.TASK_STATUS_NO_AUTH); if (message != null) message.write(TheResourceBundle.getString("Jsp Params Invalid Msg")); } return new_folder_name; }
From source file:Main.java
public static String xml2Str(Document document) { try {//from w ww . j ava 2 s . c o m DOMSource source = new DOMSource(document); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); return (writer.getBuffer().toString()); } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:com.medlog.webservice.lifecycle.Security.java
public static String getStackTrace(Throwable t) { String stackTrace = null;//from w w w. jav a2 s .co m try { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); pw.close(); sw.close(); stackTrace = sw.getBuffer().toString(); } catch (Exception ex) { } return stackTrace; }
From source file:org.exoplatform.social.client.api.util.SocialHttpClientSupport.java
/** * Gets the byte array from Model object which provides to HttpPost to Rest Service. * * @param model Model object//from w w w . j a v a 2s . com * @return * @throws IOException */ public static byte[] convertModelToByteArray(Model model) throws IOException { if (model == null) { return null; } StringWriter writer = new StringWriter(); model.writeJSONString(writer); return writer.getBuffer().toString().getBytes("UTF-8"); }
From source file:org.apache.axis2.jaxws.ExceptionFactory.java
/** * Get a string containing the stack of the specified exception * @param e/* w w w . j a va2 s . c o m*/ * @return */ public static String stackToString(Throwable e) { java.io.StringWriter sw = new java.io.StringWriter(); java.io.BufferedWriter bw = new java.io.BufferedWriter(sw); java.io.PrintWriter pw = new java.io.PrintWriter(bw); e.printStackTrace(pw); pw.close(); return sw.getBuffer().toString(); }