List of usage examples for java.io StringWriter getBuffer
public StringBuffer getBuffer()
From source file:eu.sisob.uma.restserver.services.gate.GateTask.java
public static boolean launch(String user, String pass, String task_code, String code_task_folder, String email, StringWriter message, boolean verbose, boolean split_by_keyword) { if (message == null) { return false; }/*from w ww .j av a2 s. c o m*/ boolean success = false; message.getBuffer().setLength(0); File code_task_folder_dir = new File(code_task_folder); File documents_dir = code_task_folder_dir; File csv_data_source_file = FileSystemManager.getFileIfExists(code_task_folder_dir, input_data_source_filename_prefix_csv, input_data_source_filename_ext_csv); boolean validate = csv_data_source_file != null; if (!validate) { success = false; message.write("You have not uploaded any file like this '" + input_data_source_filename_prefix_csv + "*" + input_data_source_filename_ext_csv + "' file"); } else { String middle_data_folder = code_task_folder + File.separator + AuthorizationManager.middle_data_dirname; File middle_data_dir = null; try { middle_data_dir = FileSystemManager.createFileAndIfExistsDelete(middle_data_folder); } catch (Exception ex) { ProjectLogger.LOGGER.error(ex.toString(), ex); message.append("The file couldn't be created " + middle_data_dir.getName() + "\r\n"); } String results_data_folder = code_task_folder + File.separator + AuthorizationManager.results_dirname; File results_data_dir = null; try { results_data_dir = FileSystemManager.createFileAndIfExistsDelete(results_data_folder); } catch (Exception ex) { ProjectLogger.LOGGER.error(ex.toString(), ex); message.append("The file couldn't be created " + results_data_dir.getName() + "\r\n"); } File zip_file = new File(code_task_folder_dir, input_data_documents_in_zip); if (zip_file.exists()) { documents_dir = new File(code_task_folder_dir, AuthorizationManager.middle_data_dirname); if (!ZipUtil.unZipItToSameFolder(zip_file, documents_dir)) { success = false; message.write(input_data_documents_in_zip + " cannot bet unziped"); //FIXME return success; } else { } } RepositoryPreprocessDataMiddleData preprocessedRep = null; try { File verbose_dir = null; if (verbose) { verbose_dir = new File(code_task_folder_dir, AuthorizationManager.verbose_dirname); if (!verbose_dir.exists()) verbose_dir.mkdir(); } HashMap<String, String[]> blocks_and_keywords = null; if (split_by_keyword) { blocks_and_keywords = GateDataExtractorService.getInstance().getBlocksAndKeywords(); } preprocessedRep = GateDataExtractorSingle.createPreprocessRepositoryFromCSVFile( csv_data_source_file, ';', documents_dir, verbose, verbose_dir, split_by_keyword, blocks_and_keywords, middle_data_dir); } catch (Exception ex) { message.append("The format of '" + csv_data_source_file.getName() + "' does not seems be correct. Please check the headers of the csv file (read in the instructions which are optionals) and be sure that the field separators are ';'. Please read the intructions of the task. \r\nAlso check that you have uploaded all the documents referenced in the csv file (if you have upload all the documents compressed in " + input_data_documents_in_zip + " file, please, check that it has all the files referenced in the csv).<br>Message: " + ex.getMessage()); //FIXME ProjectLogger.LOGGER.error(message.toString(), ex); } if (preprocessedRep != null) { H2DBCredentials cred_resolver = GateDataExtractorService.getH2DBCredentials_Resolver(); H2DBCredentials cred_trad = GateDataExtractorService.getH2DBCredentials_Trad_Tables_Academic(); GateDataExtractorTaskInRest task = new GateDataExtractorTaskInRest(preprocessedRep, true, cred_trad, true, cred_resolver, user, pass, task_code, code_task_folder, email); try { GateDataExtractorService.getInstance() .addExecution((new CallbackableTaskExecutionWithResource(task))); success = true; message.write(TheResourceBundle.getString("Jsp Task Executed Msg")); } catch (Exception ex) { success = false; message.write(TheResourceBundle.getString("Jsp Task Executed Error Msg")); ProjectLogger.LOGGER.error(message.toString(), ex); validate = false; } } } return success; }
From source file:com.maddyhome.idea.copyright.pattern.VelocityHelper.java
@NotNull public static String evaluate(@Nullable PsiFile file, @Nullable Project project, @Nullable Module module, @NotNull String template) throws Exception { VelocityEngine engine = getEngine(); VelocityContext vc = new VelocityContext(); vc.put("today", new DateInfo()); if (file != null) vc.put("file", new FileInfo(file)); if (project != null) vc.put("project", new ProjectInfo(project)); if (module != null) vc.put("module", new ModuleInfo(module)); vc.put("username", System.getProperty("user.name")); StringWriter sw = new StringWriter(); boolean stripLineBreak = false; if (template.endsWith("$")) { template += getVelocitySuffix(); stripLineBreak = true;//from w w w . j a va2s . c o m } engine.evaluate(vc, sw, CopyrightManager.class.getName(), template); final String result = sw.getBuffer().toString(); return stripLineBreak ? StringUtil.trimEnd(result, getVelocitySuffix()) : result; }
From source file:org.apache.axiom.om.util.CommonUtils.java
/** * Get a string containing the stack of the specified exception * * @param e/*from ww w . j a va2 s . c o m*/ * @return TODO */ 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(); String text = sw.getBuffer().toString(); // Jump past the throwable text = text.substring(text.indexOf("at")); text = replace(text, "at ", "DEBUG_FRAME = "); return text; }
From source file:Main.java
/** * Convert Properties to string// w w w . j a v a 2s . com * * @param props * @return xml string * @throws IOException */ public static String writePropToString(Properties props) throws IOException { try { org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); org.w3c.dom.Element conf = doc.createElement("configuration"); doc.appendChild(conf); conf.appendChild(doc.createTextNode("\n")); for (Enumeration e = props.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); Object object = props.get(name); String value; if (object instanceof String) { value = (String) object; } else { continue; } org.w3c.dom.Element propNode = doc.createElement("property"); conf.appendChild(propNode); org.w3c.dom.Element nameNode = doc.createElement("name"); nameNode.appendChild(doc.createTextNode(name.trim())); propNode.appendChild(nameNode); org.w3c.dom.Element valueNode = doc.createElement("value"); valueNode.appendChild(doc.createTextNode(value.trim())); propNode.appendChild(valueNode); conf.appendChild(doc.createTextNode("\n")); } 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 (Exception e) { throw new IOException(e); } }
From source file:Main.java
/** * Convert a W3C Document to a String.//from w ww . ja va 2 s . c om * * Note: if you are working with a dom4j Document, you can use it's asXml() method. * * @param doc * org.w3c.dom.Document to be converted to a String. * @return String representing the XML document. * * @throws TransformerConfigurationException * If unable to get an instance of a Transformer * @throws TransformerException * If the attempt to transform the document fails. */ public static final StringBuffer docToString(final org.w3c.dom.Document doc) throws TransformerConfigurationException, TransformerException { StringBuffer sb = null; StringWriter writer = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); // can throw // TransformerConfigurationException Source docSrc = new DOMSource(doc); t.transform(docSrc, new StreamResult(writer)); // can throw // TransformerException sb = writer.getBuffer(); return sb; }
From source file:com.indicator_engine.dao.GLAEventDaoImpl.java
/** * Adds a new GLA Entity Object to the Database. * @param glaEntity New GLA Entity Object to be saved. * @return ID of the Newly Created GLA Entity object in DB. **///from w w w . jav a2s. c o m public static String getStackTrace(final Throwable throwable) { final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); return sw.getBuffer().toString(); }
From source file:com.netflix.hystrix.contrib.requests.stream.HystrixRequestEventsJsonStream.java
public static String convertRequestsToJson(Collection<HystrixRequestEvents> requests) throws IOException { StringWriter jsonString = new StringWriter(); JsonGenerator json = jsonFactory.createGenerator(jsonString); json.writeStartArray();/*from w w w . j a va2 s . c om*/ for (HystrixRequestEvents request : requests) { writeRequestAsJson(json, request); } json.writeEndArray(); json.close(); return jsonString.getBuffer().toString(); }
From source file:com.vmware.gemfire.tools.pulse.tests.junit.BaseServiceTest.java
/** * Print exception string to system.out/*from ww w .ja v a 2 s . c o m*/ * * @param failed */ protected static void logException(Exception failed) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); failed.printStackTrace(pw); System.out.println("BaseServiceTest :: Logging exception details : " + sw.getBuffer().toString()); }
From source file:org.apache.axiom.om.util.DetachableInputStream.java
private 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);//from ww w . j a va 2 s . c o m pw.close(); String text = sw.getBuffer().toString(); return text; }
From source file:Main.java
public static StringBuffer transfer(Element element, StreamSource source) { try {//from www. j a va 2 s. c o m StringWriter sw = new StringWriter(); Transformer trans = null; if (source != null) trans = TransformerFactory.newInstance().newTransformer(source); else trans = TransformerFactory.newInstance().newTransformer(); trans.transform(new DOMSource(element), new StreamResult(sw)); sw.close(); return sw.getBuffer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }