/** * Replace all backreferences in the to pattern with the matched * groups of the source. * @param source the source file name. * @return the translated file name. */ protected String replaceReferences(String source) { List<String> v = reg.getGroups(source, regexpOptions); result.setLength(0); for (int i = 0; i < to.length; i++) { if (to[i] == '\\') { if (++i < to.length) { int value = Character.digit(to[i], DECIMAL); if (value > -1) { result.append(v.get(value)); } else { result.append(to[i]); } } else { // TODO - should throw an exception instead? result.append('\\'); } } else { result.append(to[i]); } } return result.substring(0); }
/** * Sets the "from" pattern. Required. * @param from the from pattern. * @throws BuildException on error. */ @Override public void setFrom(String from) throws BuildException { if (from == null) { throw new BuildException("this mapper requires a 'from' attribute"); } try { reg.setPattern(from); } catch (NoClassDefFoundError e) { // depending on the implementation the actual RE won't // get instantiated in the constructor. throw new BuildException("Cannot load regular expression matcher", e); } }
RegexpMatcherFactory matchMaker = new RegexpMatcherFactory(); RegexpMatcher matcher = matchMaker.newRegexpMatcher(); matcher.setPattern(r.regex); for (String name : props.keySet()) { if (matcher.matches(name)) { names.add(name);
/** * Returns null if the source file name doesn't match the * "from" pattern, an one-element array containing the * translated file otherwise. * @param sourceFileName the source file name * @return a one-element array containing the translated file or * null if the to pattern did not match */ @Override public String[] mapFileName(String sourceFileName) { if (sourceFileName == null) { return null; } if (handleDirSep) { if (sourceFileName.contains("\\")) { sourceFileName = sourceFileName.replace('\\', '/'); } } if (reg == null || to == null || !reg.matches(sourceFileName, regexpOptions)) { return null; } return new String[] {replaceReferences(sourceFileName)}; }