/** * Tests a regular expression against each line of text in a Resource. * * @param r the Resource to check. * @return whether the Resource is selected or not */ public boolean isSelected(Resource r) { String teststr = null; BufferedReader in = null; // throw BuildException on error validate(); if (r.isDirectory()) { return true; } if (myRegExp == null) { myRegExp = new RegularExpression(); myRegExp.setPattern(userProvidedExpression); myExpression = myRegExp.getRegexp(getProject()); } try { in = new BufferedReader(new InputStreamReader(r.getInputStream())); } catch (Exception e) { throw new BuildException("Could not get InputStream from " + r.toLongString(), e); } try { teststr = in.readLine(); while (teststr != null) { if (myExpression.matches( teststr, RegexpUtil.asOptions(caseSensitive, multiLine, singleLine))) { return true; } teststr = in.readLine(); } return false; } catch (IOException ioe) { throw new BuildException("Could not read " + r.toLongString()); } finally { try { in.close(); } catch (Exception e) { throw new BuildException("Could not close " + r.toLongString()); } } }
/** * the regular expression pattern to match in the file(s); required if no nested <regexp> is * used * * @param match the match attribute. */ public void setMatch(String match) { if (regex != null) { throw new BuildException("Only one regular expression is allowed"); } regex = new RegularExpression(); regex.setPattern(match); }
/** * Invoke a regular expression (r) on a string (input) using substitutions (s) for a matching * regex. * * @param r a regular expression * @param s a Substitution * @param input the string to do the replacement on * @param options The options for the regular expression * @return the replacement result */ protected String doReplace(RegularExpression r, Substitution s, String input, int options) { String res = input; Regexp regexp = r.getRegexp(getProject()); if (regexp.matches(input, options)) { log("Found match; substituting", Project.MSG_DEBUG); res = regexp.substitute(input, s.getExpression(getProject()), options); } return res; }
/** * 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(); } } }