public void fill() throws JRException { try { long start = System.currentTimeMillis(); String rtfText = FileUtils.readFully(new FileReader(new File("reports/rtf.txt"))); String htmlText = FileUtils.readFully(new FileReader(new File("reports/html.txt"))); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("RtfText", rtfText); parameters.put("HtmlText", htmlText); JasperFillManager.fillReportToFile( "build/reports/MarkupReport.jasper", parameters, (JRDataSource) null); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); } catch (FileNotFoundException e) { throw new JRException(e); } catch (IOException e) { throw new JRException(e); } }
public List<String> getKeyRings() throws IOException { List<String> keys = new ArrayList<String>(); for (Resource resource : keyrings) { log("Include keyring: " + resource.getName()); String key = FileUtils.readFully( new InputStreamReader(resource.getInputStream(), StandardCharsets.US_ASCII)); if (key != null) { keys.add(key); } } return keys; }
/** * Return the result of the filters on the sourcefilename. * * @param sourceFileName the filename to map * @return a one-element array of converted filenames, or null if the filterchain returns an empty * string. */ public String[] mapFileName(String sourceFileName) { try { Reader stringReader = new StringReader(sourceFileName); ChainReaderHelper helper = new ChainReaderHelper(); helper.setBufferSize(8192); helper.setPrimaryReader(stringReader); helper.setProject(getProject()); Vector filterChains = new Vector(); filterChains.add(this); helper.setFilterChains(filterChains); String result = FileUtils.readFully(helper.getAssembledReader()); if (result.length() == 0) { return null; } else { return new String[] {result}; } } catch (BuildException ex) { throw ex; } catch (Exception ex) { throw new BuildException(ex); } }
/** * Read data from the reader and return the contents as a string. * * @param rdr the reader object * @return the contents of the file as a string * @exception IOException if an error occurs */ public String readFully(Reader rdr) throws IOException { return FileUtils.readFully(rdr, bufferSize); }
public List<Map<String, Object>> getPackages() throws IOException { List<Map<String, Object>> packages = new ArrayList<Map<String, Object>>(); for (SPK spk : spks) { log("Include SPK: " + spk.file.getName()); // make sure file is cached locally if (spk.url != null) { log("Using " + spk.url); if (!spk.file.exists()) { spk.file.getParentFile().mkdirs(); } if (spk.url == null) { spk.url = spk.url; } Get get = new Get(); get.bindToOwner(this); get.setQuiet(true); get.setUseTimestamp(true); get.setSrc(spk.url); get.setDest(spk.file); get.execute(); } else { log("Using " + spk.file); } // import SPK INFO Map<String, Object> info = new LinkedHashMap<String, Object>(); TarFileSet tar = new TarFileSet(); tar.setProject(getProject()); tar.setSrc(spk.file); tar.setIncludes(INFO); for (Resource resource : tar) { if (INFO.equals(resource.getName())) { String text = FileUtils.readFully( new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)); for (String line : text.split("\\R")) { String[] s = line.split("=", 2); if (s.length == 2) { if (s[1].startsWith("\"") && s[1].endsWith("\"")) { s[1] = s[1].substring(1, s[1].length() - 1); } importSpkInfo(info, s[0], s[1]); } } } } log(String.format("Imported %d fields from SPK: %s", info.size(), info.keySet())); // add thumbnails and snapshots if (spk.thumbnail.size() > 0) { info.put(THUMBNAIL, spk.thumbnail.toArray(new String[0])); } if (spk.snapshot.size() > 0) { info.put(SNAPSHOT, spk.snapshot.toArray(new String[0])); } // add user-defined fields info.putAll(spk.infoList); // automatically generate file size and checksum fields if (!info.containsKey(LINK)) { info.put(LINK, spk.url); } info.put(MD5, md5(spk.file)); info.put(SIZE, spk.file.length()); packages.add(info); } return packages; }
/** * Perform the replacement on a file * * @param f the file to perform the relacement on * @param options the regular expressions options * @exception IOException if an error occurs */ protected void doReplace(File f, int options) throws IOException { File temp = fileUtils.createTempFile("replace", ".txt", null); temp.deleteOnExit(); Reader r = null; Writer w = null; try { if (encoding == null) { r = new FileReader(f); w = new FileWriter(temp); } else { r = new InputStreamReader(new FileInputStream(f), encoding); w = new OutputStreamWriter(new FileOutputStream(temp), encoding); } BufferedReader br = new BufferedReader(r); BufferedWriter bw = new BufferedWriter(w); PrintWriter pw = new PrintWriter(bw); boolean changes = false; log( "Replacing pattern '" + regex.getPattern(getProject()) + "' with '" + subs.getExpression(getProject()) + "' in '" + f.getPath() + "'" + (byline ? " by line" : "") + (flags.length() > 0 ? " with flags: '" + flags + "'" : "") + ".", Project.MSG_VERBOSE); if (byline) { StringBuffer linebuf = new StringBuffer(); String line = null; String res = null; int c; boolean hasCR = false; do { c = br.read(); if (c == '\r') { if (hasCR) { // second CR -> EOL + possibly empty line line = linebuf.toString(); res = doReplace(regex, subs, line, options); if (!res.equals(line)) { changes = true; } pw.print(res); pw.print('\r'); linebuf = new StringBuffer(); // hasCR is still true (for the second one) } else { // first CR in this line hasCR = true; } } else if (c == '\n') { // LF -> EOL line = linebuf.toString(); res = doReplace(regex, subs, line, options); if (!res.equals(line)) { changes = true; } pw.print(res); if (hasCR) { pw.print('\r'); hasCR = false; } pw.print('\n'); linebuf = new StringBuffer(); } else { // any other char if ((hasCR) || (c < 0)) { // Mac-style linebreak or EOF (or both) line = linebuf.toString(); res = doReplace(regex, subs, line, options); if (!res.equals(line)) { changes = true; } pw.print(res); if (hasCR) { pw.print('\r'); hasCR = false; } linebuf = new StringBuffer(); } if (c >= 0) { linebuf.append((char) c); } } } while (c >= 0); pw.flush(); } else { String buf = fileUtils.readFully(br); if (buf == null) { buf = ""; } String res = doReplace(regex, subs, buf, options); if (!res.equals(buf)) { changes = true; } pw.print(res); pw.flush(); } r.close(); r = null; w.close(); w = null; if (changes) { log("File has changed; saving the updated file", Project.MSG_VERBOSE); try { fileUtils.rename(temp, f); temp = null; } catch (IOException e) { throw new BuildException("Couldn't rename temporary file " + temp, getLocation()); } } else { log("No change made", Project.MSG_DEBUG); } } finally { try { if (r != null) { r.close(); } } catch (Exception e) { // ignore any secondary exceptions } try { if (w != null) { w.close(); } } catch (Exception e) { // ignore any secondary exceptions } if (temp != null) { temp.delete(); } } }