List of usage examples for java.io StringWriter append
public StringWriter append(char c)
From source file:com.github.gekoh.yagen.ddl.CreateDDL.java
private String getHsqlDBHistTriggerSql(Dialect dialect, String tableName, String histTableName, String histColName, Set<String> columns, List<String> pkColumns, List<String> histRelevantCols) { VelocityContext context = new VelocityContext(); Set<String> nonPkColumns = new HashSet<String>(columns); nonPkColumns.removeAll(pkColumns);//from ww w .j av a 2 s . c om context.put("VERSION_COLUMN_NAME", VERSION_COLUMN_NAME); context.put("MODIFIER_COLUMN_NAME", AuditInfo.LAST_MODIFIED_BY); context.put("liveTableName", tableName); context.put("hstTableName", histTableName); context.put("columns", columns); context.put("histColName", histColName); context.put("pkColumns", pkColumns); context.put("nonPkColumns", nonPkColumns); context.put("histRelevantCols", histRelevantCols); StringWriter wr = new StringWriter(); try { wr.append(STATEMENT_SEPARATOR); writeTriggerSingleOperation(dialect, wr, "HstTriggerSingleOperation.vm.pl.sql", context, tableName, "_ht", "I"); wr.write("\n/\n"); wr.append(STATEMENT_SEPARATOR); writeTriggerSingleOperation(dialect, wr, "HstTriggerSingleOperation.vm.pl.sql", context, tableName, "_ht", "U"); wr.write("\n/\n"); wr.append(STATEMENT_SEPARATOR); writeTriggerSingleOperation(dialect, wr, "HstTriggerSingleOperation.vm.pl.sql", context, tableName, "_ht", "D"); wr.write("\n/\n"); } catch (IOException e) { throw new IllegalStateException("cannot read history trigger template"); } return wr.toString(); }
From source file:edu.harvard.i2b2.query.ui.ConceptTreePanel.java
private String generateMessageId() { StringWriter strWriter = new StringWriter(); for (int i = 0; i < 20; i++) { int num = getValidAcsiiValue(); // System.out.println("Generated number: " + num + " char: // "+(char)num); strWriter.append((char) num); }//www . ja v a 2s. com return strWriter.toString(); }
From source file:nl.knaw.dans.dccd.rest.AbstractProjectResource.java
/** * TimeRange (or Temporal Coverage)//from ww w . j a v a 2 s.c om * * @param sw * @param dccdSB */ protected void appendProjectTimeRangeAsXml(java.io.StringWriter sw, DccdSB dccdSB) { // concat all the lists, but only non null elements List<Integer> years = new ArrayList<Integer>(); List<Integer> yearsFromTridas = dccdSB.getTridasMeasurementseriesInterpretationPithyear(); if (yearsFromTridas != null) { for (Integer year : yearsFromTridas) { if (year != null) years.add(year); } } yearsFromTridas = dccdSB.getTridasMeasurementseriesInterpretationFirstyear(); if (yearsFromTridas != null) { for (Integer year : yearsFromTridas) { if (year != null) years.add(year); } } yearsFromTridas = dccdSB.getTridasMeasurementseriesInterpretationLastyear(); if (yearsFromTridas != null) { for (Integer year : yearsFromTridas) { if (year != null) years.add(year); } } yearsFromTridas = dccdSB.getTridasMeasurementseriesInterpretationDeathyear(); if (yearsFromTridas != null) { for (Integer year : yearsFromTridas) { if (year != null) years.add(year); } } if (!years.isEmpty()) { // we have at least one year (and it is not null) Integer min = years.get(0); Integer max = min; for (int i = 1; i < years.size(); i++) { if (years.get(i) < min) min = years.get(i); if (years.get(i) > max) max = years.get(i); } // firstDate = min // lastDate = max sw.append("<timeRange>"); sw.append(getXMLElementString("firstYear", min.toString())); sw.append(getXMLElementString("lastYear", max.toString())); sw.append("</timeRange>"); } }
From source file:org.nuxeo.launcher.config.ConfigurationGenerator.java
private void writeConfiguration() throws ConfigurationException { final MessageDigest newContentDigest = DigestUtils.getMd5Digest(); StringWriter newContent = new StringWriter() { @Override// w ww . ja v a 2 s . com public void write(String str) { if (str != null) { newContentDigest.update(str.getBytes()); } super.write(str); } }; // Copy back file content newContent.append(readConfiguration()); // Write changed parameters newContent.write(BOUNDARY_BEGIN + System.getProperty("line.separator")); for (Object o : new TreeSet<>(userConfig.keySet())) { String key = (String) o; // Ignore parameters already stored in newContent if (PARAM_FORCE_GENERATION.equals(key) || PARAM_WIZARD_DONE.equals(key) || PARAM_TEMPLATES_NAME.equals(key)) { continue; } String oldValue = storedConfig.getProperty(key, ""); String newValue = userConfig.getRawProperty(key, ""); if (!newValue.equals(oldValue)) { newContent.write("#" + key + "=" + oldValue + System.getProperty("line.separator")); newContent.write(key + "=" + newValue + System.getProperty("line.separator")); } } newContent.write(BOUNDARY_END + System.getProperty("line.separator")); // Write file only if content has changed if (!Hex.encodeHexString(newContentDigest.digest()).equals(currentConfigurationDigest)) { try (Writer writer = new FileWriter(nuxeoConf, false)) { writer.append(newContent.getBuffer()); } catch (IOException e) { throw new ConfigurationException("Error writing in " + nuxeoConf, e); } } }
From source file:com.aurel.track.admin.customize.category.report.execute.ReportBeansToLaTeXConverter.java
/** * * @param context// www .ja v a 2s .co m * @param templateBuffer */ protected File createProcessedLaTeXFile(Map<String, Object> context, StringBuilder templateBuffer, String fileName) { Template freemarkerTemplate = null; File latexFile = new File(latexTmpDir + File.separator + fileName); StringWriter texWriter = new StringWriter(); StringBuffer debugMessages = new StringBuffer(); try { debugMessages.append("Creating Freemarker template..." + CRLF); Configuration freemarkerConfig = new Configuration(); freemarkerConfig.setTemplateExceptionHandler(new LaTeXFreemarkerExceptionHandler()); freemarkerTemplate = new Template(fileName, new StringReader(templateBuffer.toString()), freemarkerConfig); debugMessages.append("Processing the Freemarker LaTeX template..." + CRLF); freemarkerTemplate.process(context, texWriter); debugMessages.append("Freemmarker LaTeX template processed." + CRLF); } catch (Exception e) { LOGGER.error("Problem processing template: " + CRLF + debugMessages.toString()); String st = ExceptionUtils.getStackTrace(e); debugTrace.append(debugMessages.toString() + CRLF); debugTrace.append(e.getMessage()); LOGGER.debug("Problem processing template:", e); texWriter = new StringWriter(); String tb = templateBuffer.toString(); tb.replace("\\end{document}", "Problem processing template: " + CRLF + CRLF + debugMessages.toString() + CRLF + CRLF + st + CRLF + CRLF + "\\end{document}"); texWriter.append(templateBuffer.toString()); } texWriter.append(CRLF + "% The following keys are available for the enclosing document itself:" + CRLF); for (String item : context.keySet()) { texWriter.append("% " + item + CRLF); } texWriter.append(CRLF + "% The following keys are available for the document sections:" + CRLF); if (!(context.get("meetingTopics") == null) && !((ArrayList<Map<String, Object>>) context.get("meetingTopics")).isEmpty()) { for (String item : ((ArrayList<Map<String, Object>>) context.get("meetingTopics")).get(0).keySet()) { texWriter.append("% meetingTopics." + item + CRLF); } } else if (!(context.get("items") == null) && !((ArrayList<Map<String, Object>>) context.get("items")).isEmpty()) { for (String item : ((ArrayList<Map<String, Object>>) context.get("items")).get(0).keySet()) { texWriter.append("% items." + item + CRLF); } } texWriter.flush(); String processedTex = texWriter.toString(); LOGGER.debug(processedTex); try { FileUtils.writeStringToFile(latexFile, processedTex.toString(), "UTF-8"); } catch (Exception e) { LOGGER.error("Cannot write file :" + latexFile.getAbsolutePath()); } return latexFile; }
From source file:eu.sisob.uma.extractors.adhoc.websearchers_cv.WebSearchersCVExtractor.java
/** * * @param input_file//from w w w.ja v a2s . c o m * @param results_dir * @param zip_output_file * @param output_file_2 * @param error_sw */ public static void download_files(File input_file, File results_dir, File zip_output_file, File output_file_2, StringWriter error_sw) { CSVReader reader = null; try { reader = new CSVReader(new FileReader(input_file), CSV_SEPARATOR); } catch (FileNotFoundException ex) { Logger.getRootLogger().error("Error reading " + input_file.getName() + " - " + ex.toString()); return; } int idStaffIdentifier = -1; int idName = -1; int idFirstName = -1; int idLastName = -1; int idInitials = -1; int idUnitOfAssessment_Description = -1; int idInstitutionName = -1; int idWebAddress = -1; int idResearchGroupDescription = -1; int idResearcherWebAddress = -1; int idResearcherWebAddressType = -1; int idResearcherWebAddressExt = -1; int idScoreUrl = -1; String[] nextLine; try { if ((nextLine = reader.readNext()) != null) { //Locate indexes //Locate indexes for (int i = 0; i < nextLine.length; i++) { String column_name = nextLine[i]; if (column_name.equals(FileFormatConversor.CSV_COL_ID)) idStaffIdentifier = i; else if (column_name.equals(FileFormatConversor.CSV_COL_NAME)) idName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_FIRSTNAME)) idFirstName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_LASTNAME)) idLastName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INITIALS)) idInitials = i; else if (column_name.equals(FileFormatConversor.CSV_COL_SUBJECT)) idUnitOfAssessment_Description = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_NAME)) idInstitutionName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_URL)) idWebAddress = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_URL)) idResearcherWebAddress = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_TYPE)) idResearcherWebAddressType = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_EXT)) idResearcherWebAddressExt = i; else if (column_name.equals(FileFormatConversor.CSV_COL_SCORE_URL)) idScoreUrl = i; } } } catch (Exception ex) { String error_msg = "Error reading headers of " + input_file.getName(); Logger.getRootLogger().error(error_msg + " - " + ex.toString()); if (error_sw != null) error_sw.append(error_msg + "\r\n"); return; } if (idResearcherWebAddress != -1 && idResearcherWebAddressType != -1 && idResearcherWebAddressExt != -1) { Logger.getRootLogger().info("Going to downloads results files"); try { for (int i = 0; i < nextLine.length; i++) nextLine[i] = "\"" + nextLine[i] + "\""; FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n", "UTF-8", false); } catch (IOException ex) { Logger.getLogger("root").error(ex.toString()); } if (!results_dir.exists()) results_dir.mkdirs(); try { while ((nextLine = reader.readNext()) != null) { String url = nextLine[idResearcherWebAddress]; String ext = nextLine[idResearcherWebAddressExt]; String id = nextLine[idStaffIdentifier]; try { Logger.getRootLogger().info("Downloading " + url); String filename = id + "." + ext; FileUtils.copyURLToFile(new URL(url), new File(results_dir, filename)); nextLine[idResearcherWebAddress] = filename; try { for (int i = 0; i < nextLine.length; i++) { nextLine[i] = "\"" + nextLine[i] + "\""; } FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n", "UTF-8", true); } catch (IOException ex) { Logger.getLogger("root").error(ex.toString()); } } catch (IOException ex) { Logger.getRootLogger().error("Error downloading " + url); } } } catch (IOException ex) { Logger.getRootLogger().error("Error reading " + input_file.getName() + " " + ex.getMessage()); } ZipFile zf; try { zip_output_file.delete(); zf = new ZipFile(zip_output_file); zf.createZipFileFromFolder(results_dir, new ZipParameters(), false, 0); } catch (ZipException ex) { Logger.getRootLogger().error("Error zipping results from " + input_file.getName()); } } else { Logger.getRootLogger().error("Headers incorrect " + input_file.getName()); } }
From source file:org.jsweet.transpiler.JSweetTranspiler.java
/** * Evaluates the given source files with the given evaluation engine. * <p>// w ww . j a va2 s . co m * If given engine name is "Java", this function looks up for the classes in * the classpath and run the main methods when found. * * @param engineName * the engine name: either "Java" or any valid and installed * JavaScript engine. * @param transpilationHandler * the log handler * @param sourceFiles * the source files to be evaluated (transpiled first if needed) * @return the evaluation result * @throws Exception * when an internal error occurs */ public EvaluationResult eval(String engineName, TranspilationHandler transpilationHandler, SourceFile... sourceFiles) throws Exception { logger.info("[" + engineName + " engine] eval files: " + Arrays.asList(sourceFiles)); if ("Java".equals(engineName)) { // search for main functions JSweetContext context = new JSweetContext(this); Options options = Options.instance(context); if (classPath != null) { options.put(Option.CLASSPATH, classPath); } options.put(Option.XLINT, "path"); JavacFileManager.preRegister(context); JavaFileManager fileManager = context.get(JavaFileManager.class); List<JavaFileObject> fileObjects = toJavaFileObjects(fileManager, Arrays.asList(SourceFile.toFiles(sourceFiles))); JavaCompiler compiler = JavaCompiler.instance(context); compiler.attrParseOnly = true; compiler.verbose = true; compiler.genEndPos = false; logger.info("parsing: " + fileObjects); List<JCCompilationUnit> compilationUnits = compiler.enterTrees(compiler.parseFiles(fileObjects)); MainMethodFinder mainMethodFinder = new MainMethodFinder(); try { for (JCCompilationUnit cu : compilationUnits) { cu.accept(mainMethodFinder); } } catch (Exception e) { // swallow on purpose } if (mainMethodFinder.mainMethod != null) { try { initExportedVarMap(); Class<?> c = Class.forName( mainMethodFinder.mainMethod.getEnclosingElement().getQualifiedName().toString()); c.getMethod("main", String[].class).invoke(null, (Object) null); } catch (Exception e) { throw new Exception("evalution error", e); } } final Map<String, Object> map = getExportedVarMap(); return new EvaluationResult() { @SuppressWarnings("unchecked") @Override public <T> T get(String variableName) { return (T) map.get("_exportedVar_" + variableName); } @Override public String toString() { return map.toString(); } @Override public String getExecutionTrace() { return "<not available>"; } }; } else { if (!areAllTranspiled(sourceFiles)) { ErrorCountTranspilationHandler errorHandler = new ErrorCountTranspilationHandler( transpilationHandler); transpile(errorHandler, sourceFiles); if (errorHandler.getErrorCount() > 0) { throw new Exception("unable to evaluate: transpilation errors remain"); } } StringWriter trace = new StringWriter(); Process runProcess; if (context.useModules) { File f = null; if (!context.entryFiles.isEmpty()) { f = context.entryFiles.get(0); for (SourceFile sf : sourceFiles) { if (sf.getJavaFile().equals(f)) { f = sf.getJsFile(); } } } if (f == null) { f = sourceFiles[sourceFiles.length - 1].getJsFile(); } logger.info("[modules] eval file: " + f); runProcess = ProcessUtil.runCommand(ProcessUtil.NODE_COMMAND, line -> trace.append(line + "\n"), null, f.getPath()); } else { File tmpFile = new File(new File(TMP_WORKING_DIR_NAME), "eval.tmp.js"); FileUtils.deleteQuietly(tmpFile); if (jsLibFiles != null) { for (File jsLibFile : jsLibFiles) { String script = FileUtils.readFileToString(jsLibFile); FileUtils.write(tmpFile, script + "\n", true); } } for (SourceFile sourceFile : sourceFiles) { String script = FileUtils.readFileToString(sourceFile.getJsFile()); FileUtils.write(tmpFile, script + "\n", true); } logger.info("[no modules] eval file: " + tmpFile); runProcess = ProcessUtil.runCommand(ProcessUtil.NODE_COMMAND, line -> trace.append(line + "\n"), null, tmpFile.getPath()); } int returnCode = runProcess.exitValue(); logger.info("return code=" + returnCode); if (returnCode != 0) { throw new Exception("evaluation error (code=" + returnCode + ") - trace=" + trace); } return new TraceBasedEvaluationResult(trace.getBuffer().toString()); } }
From source file:com.github.gekoh.yagen.ddl.CreateDDL.java
private void addAuditTrigger(Dialect dialect, StringBuffer buf, String nameLC, Set<String> columns) { TableConfig tableConfig = tblNameToConfig.get(nameLC); String templateName = "AuditTrigger"; if (!columns.containsAll(AUDIT_COLUMNS)) { if (tableConfig != null && tableConfig.getTableAnnotationOfType(Auditable.class) != null && columns.contains(AuditInfo.LAST_MODIFIED_AT)) { templateName += "SingleTimestamp"; } else {// w w w.j av a 2 s. co m return; } } if (isPostgreSql(dialect)) { writePostgreSqlAuditTrigger(dialect, buf, nameLC); return; } StringWriter wr = new StringWriter(); VelocityContext context = new VelocityContext(); context.put("liveTableName", nameLC); context.put("created_at", AuditInfo.CREATED_AT); context.put("created_by", AuditInfo.CREATED_BY); context.put("last_modified_at", AuditInfo.LAST_MODIFIED_AT); context.put("last_modified_by", AuditInfo.LAST_MODIFIED_BY); if (isOracle(dialect)) { writeOracleAuditTrigger(dialect, buf, context, nameLC, templateName + ".vm.pl.sql"); } else { try { templateName += "SingleOperation.vm.pl.sql"; wr.append(STATEMENT_SEPARATOR); writeTriggerSingleOperation(dialect, wr, templateName, context, nameLC, "_at", "I"); wr.write("\n/\n"); wr.append(STATEMENT_SEPARATOR); writeTriggerSingleOperation(dialect, wr, templateName, context, nameLC, "_at", "U"); wr.write("\n/\n"); buf.append(wr.toString()); } catch (IOException e) { LOG.error("error writing audit triggers", e); } } }
From source file:eu.sisob.uma.extractors.adhoc.websearchers.WebSearchersExtractorCommons.java
/** * * @param input_file/* w w w . j av a2s.c om*/ * @param output_file * @param notfound_output_file * @param error_sw */ public void scrap_duckduckgo(File input_file, File output_file, File notfound_output_file, StringWriter error_sw) { CSVReader reader = null; try { reader = new CSVReader(new FileReader(input_file), CSV_SEPARATOR); } catch (FileNotFoundException ex) { Logger.getRootLogger().error("Error reading " + input_file.getName() + " - " + ex.toString()); return; } int idStaffIdentifier = -1; int idName = -1; int idFirstName = -1; int idLastName = -1; int idInitials = -1; int idSubject = -1; int idInstitutionName = -1; int idWebAddress = -1; Logger.getRootLogger().info("Going to search researchers using duckduckgo"); String[] nextLine; try { if ((nextLine = reader.readNext()) != null) { //Locate indexes //Locate indexes for (int i = 0; i < nextLine.length; i++) { String column_name = nextLine[i]; if (column_name.equals(FileFormatConversor.CSV_COL_ID)) idStaffIdentifier = i; else if (column_name.equals(FileFormatConversor.CSV_COL_NAME)) idName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_FIRSTNAME)) idFirstName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_LASTNAME)) idLastName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INITIALS)) idInitials = i; else if (column_name.equals(FileFormatConversor.CSV_COL_SUBJECT)) idSubject = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_NAME)) idInstitutionName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_URL)) idWebAddress = i; } } } catch (IOException ex) { String error_msg = "Error reading headers of " + input_file.getName(); Logger.getRootLogger().error(error_msg + " - " + ex.toString()); if (error_sw != null) error_sw.append(error_msg + "\r\n"); return; } Logger.getRootLogger().info("Headers info of result file writed"); if (idLastName != -1 && idInitials != -1 && idStaffIdentifier != -1 && idWebAddress != -1 && idSubject != -1) { try { String header = ""; header += "\"" + FileFormatConversor.CSV_COL_ID + "\";"; header += "\"" + FileFormatConversor.CSV_COL_LASTNAME + "\";"; header += "\"" + FileFormatConversor.CSV_COL_INITIALS + "\";"; if (idFirstName != -1) header += "\"" + FileFormatConversor.CSV_COL_FIRSTNAME + "\";"; if (idName != -1) header += "\"" + FileFormatConversor.CSV_COL_NAME + "\";"; header += "\"" + FileFormatConversor.CSV_COL_RESEARCHER_PAGE_URL + "\";"; header += "\"" + FileFormatConversor.CSV_COL_RESEARCHER_PAGE_EXT + "\";"; header += "\"" + FileFormatConversor.CSV_COL_RESEARCHER_PAGE_TYPE + "\";"; header += "\"" + FileFormatConversor.CSV_COL_SCORE_URL + "\";"; header += "\r\n"; FileUtils.write(output_file, header, "UTF-8", false); header = ""; header += "\"" + FileFormatConversor.CSV_COL_ID + "\";"; header += "\"" + FileFormatConversor.CSV_COL_LASTNAME + "\";"; header += "\"" + FileFormatConversor.CSV_COL_INITIALS + "\";"; if (idFirstName != -1) header += "\"" + FileFormatConversor.CSV_COL_FIRSTNAME + "\";"; if (idName != -1) header += "\"" + FileFormatConversor.CSV_COL_NAME + "\";"; if (idInstitutionName != -1) header += "\"" + FileFormatConversor.CSV_COL_INSTITUTION_NAME + "\";"; header += "\"" + FileFormatConversor.CSV_COL_INSTITUTION_URL + "\";"; header += "\"" + FileFormatConversor.CSV_COL_SCORE_URL + "\";"; header += "\r\n"; FileUtils.write(notfound_output_file, header, "UTF-8", false); } catch (IOException ex) { Logger.getLogger("root").error(ex.toString()); error_sw.append("Error creating output files\r\n"); } try { while ((nextLine = reader.readNext()) != null) { nextLine[idLastName] = nextLine[idLastName].replaceAll("[^a-zA-Z]", " ").toLowerCase(); nextLine[idInitials] = nextLine[idInitials].replaceAll("[^a-zA-Z]", " ").toLowerCase(); if (idFirstName != -1) nextLine[idFirstName] = nextLine[idFirstName].replaceAll("[^a-zA-Z]", " ").toLowerCase(); if (idName != -1) nextLine[idName] = nextLine[idName].replaceAll("[^a-zA-Z]", " ").toLowerCase(); String expression = ""; String aux = nextLine[idLastName]; expression += aux + " AND "; if (idFirstName != -1) { String ss[] = nextLine[idFirstName].split(" "); for (String s : ss) { if (s.length() > 1) expression += s + " AND "; } expression = expression.substring(0, expression.length() - 5); } else { String ss[] = nextLine[idInitials].split(" "); for (String s : ss) { expression += s + " AND "; } //expression += aux + " "; expression = expression.substring(0, expression.length() - 5); } String final_result = get_result(nextLine, idStaffIdentifier, idName, idFirstName, idLastName, idInitials, idSubject, idInstitutionName, idWebAddress, expression, null); if (!final_result.equals("")) { try { FileUtils.write(output_file, final_result, "UTF-8", true); Logger.getRootLogger().info("Writed results"); } catch (IOException ex) { Logger.getLogger("root").error(ex.toString()); } } else { final_result = ""; final_result += "\"" + nextLine[idStaffIdentifier] + "\";"; final_result += "\"" + nextLine[idLastName] + "\";"; final_result += "\"" + nextLine[idInitials] + "\";"; if (idFirstName != -1) final_result += "\"" + nextLine[idFirstName] + "\";"; if (idName != -1) final_result += "\"" + nextLine[idName] + "\";"; if (idInstitutionName != -1) final_result += "\"" + nextLine[idInstitutionName] + "\";"; final_result += "\"" + nextLine[idWebAddress] + "\""; final_result += "\r\n"; try { Logger.getRootLogger().info("No results"); FileUtils.write(notfound_output_file, final_result, "UTF-8", true); } catch (IOException ex) { Logger.getLogger("root").error(ex.toString()); } } } reader.close(); Logger.getRootLogger().info("Researchers data info of results file writed"); } catch (Exception ex) { String error_msg = "Error extracting web researchers from DuckDuckGo " + input_file.getName(); Logger.getRootLogger().error(error_msg + " - " + ex.toString()); if (error_sw != null) error_sw.append(error_msg + "\r\n"); return; } } }
From source file:eu.sisob.uma.extractors.adhoc.cvfilesinside.InternalCVFilesExtractor.java
/** * * @param input_file//from ww w. j a v a 2s . c o m * @param results_dir * @param zip_output_file * @param output_file_2 * @param error_sw */ public static void download_files(File input_file, File results_dir, File zip_output_file, File output_file_2, StringWriter error_sw) { CSVReader reader = null; try { reader = new CSVReader(new FileReader(input_file), CSV_SEPARATOR); } catch (FileNotFoundException ex) { Logger.getRootLogger().error("Error reading " + input_file.getName() + " - " + ex.toString()); return; } int idStaffIdentifier = -1; int idName = -1; int idFirstName = -1; int idLastName = -1; int idInitials = -1; int idUnitOfAssessment_Description = -1; int idInstitutionName = -1; int idWebAddress = -1; int idResearchGroupDescription = -1; int idResearcherWebAddress = -1; int idResearcherWebAddressType = -1; int idResearcherWebAddressExt = -1; int idScoreUrl = -1; String[] nextLine; try { if ((nextLine = reader.readNext()) != null) { for (int i = 0; i < nextLine.length; i++) { String column_name = nextLine[i]; if (column_name.equals(FileFormatConversor.CSV_COL_ID)) idStaffIdentifier = i; else if (column_name.equals(FileFormatConversor.CSV_COL_NAME)) idName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_FIRSTNAME)) idFirstName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_LASTNAME)) idLastName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INITIALS)) idInitials = i; else if (column_name.equals(FileFormatConversor.CSV_COL_SUBJECT)) idUnitOfAssessment_Description = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_NAME)) idInstitutionName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_URL)) idWebAddress = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_URL)) idResearcherWebAddress = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_TYPE)) idResearcherWebAddressType = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_EXT)) idResearcherWebAddressExt = i; else if (column_name.equals(FileFormatConversor.CSV_COL_SCORE_URL)) idScoreUrl = i; } } } catch (Exception ex) { String error_msg = "Error reading headers of " + input_file.getName(); Logger.getRootLogger().error(error_msg + " - " + ex.toString()); if (error_sw != null) error_sw.append(error_msg + "\r\n"); return; } try { for (int i = 0; i < nextLine.length; i++) nextLine[i] = "\"" + nextLine[i] + "\""; FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n", "UTF-8", false); } catch (IOException ex) { Logger.getLogger("root").error(ex.toString()); } if (idResearcherWebAddress != -1 && idResearcherWebAddressType != -1 && idResearcherWebAddressExt != -1) { Logger.getRootLogger().info("Going to downloads results files"); MessageDigest digest = null; try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException ex) { } if (!results_dir.exists()) results_dir.mkdirs(); // File cv_results_dirs = new File(results_dir, "CV"); // if(!cv_results_dirs.exists()) // cv_results_dirs.mkdirs(); // // File pub_results_dirs = new File(results_dir, "PUB"); // if(!pub_results_dirs.exists()) // pub_results_dirs.mkdirs(); // // File homepage_results_dirs = new File(results_dir, "HOMEPAGE"); // if(!homepage_results_dirs.exists()) // homepage_results_dirs.mkdirs(); try { while ((nextLine = reader.readNext()) != null) { String url = nextLine[idResearcherWebAddress]; String ext = nextLine[idResearcherWebAddressExt]; String type = nextLine[idResearcherWebAddressType]; String id = nextLine[idStaffIdentifier]; try { Logger.getRootLogger().info("Downloading " + url); String filename = type + "_" + id + "_" + MD5(url) + "." + ext; File dest = null; // if(type.equals("CV")) // dest = new File(cv_results_dirs, filename); // else if(type.equals("PUB")) // dest = new File(pub_results_dirs, filename); // else if(type.equals("HOMEPAGE")) // dest = new File(homepage_results_dirs, filename); // else dest = new File(results_dir, filename); int max = 10; int num = 0; boolean download_finish = false; while (!download_finish) { try { Thread.sleep(200); URL fetched_url = Downloader.fetchURL(url); FileUtils.copyURLToFile(fetched_url, dest); download_finish = true; } catch (Exception ex) { Logger.getRootLogger().error("Error downloading " + url, ex); num++; } if (max <= num) throw new Exception("Error download time overflowed"); } nextLine[idResearcherWebAddress] = filename; try { for (int i = 0; i < nextLine.length; i++) nextLine[i] = "\"" + nextLine[i] + "\""; FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n", "UTF-8", true); } catch (Exception ex) { Logger.getLogger("root").error(ex.toString()); } } catch (Exception ex) { Logger.getRootLogger().error("Error manage downloading " + url, ex); } } } catch (Exception ex) { Logger.getRootLogger().error("Error reading " + input_file.getName() + " " + ex.getMessage()); } ZipFile zf; try { zf = new ZipFile(zip_output_file); zf.createZipFileFromFolder(results_dir, new ZipParameters(), false, 0); } catch (Exception ex) { Logger.getRootLogger().error("Error zipping results from " + input_file.getName()); } } else { Logger.getRootLogger().error("Headers incorrect " + input_file.getName()); } }