source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/RegexpPatternMapper.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.8 KB
Line 
1/*
2 * Copyright 2000,2002-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.util;
19
20import java.util.Vector;
21import org.apache.tools.ant.BuildException;
22import org.apache.tools.ant.util.regexp.RegexpMatcher;
23import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
24
25/**
26 * Implementation of FileNameMapper that does regular expression
27 * replacements.
28 *
29 */
30public class RegexpPatternMapper implements FileNameMapper {
31 protected RegexpMatcher reg = null;
32 protected char[] to = null;
33 protected StringBuffer result = new StringBuffer();
34
35 /**
36 * Constructor for RegexpPatternMapper.
37 * @throws BuildException on error.
38 */
39 public RegexpPatternMapper() throws BuildException {
40 reg = (new RegexpMatcherFactory()).newRegexpMatcher();
41 }
42
43 private boolean handleDirSep = false;
44 private int regexpOptions = 0;
45
46 /**
47 * Attribute specifing whether to ignore the difference
48 * between / and \ (the two common directory characters).
49 * @param handleDirSep a boolean, default is false.
50 * @since Ant 1.6.3
51 */
52 public void setHandleDirSep(boolean handleDirSep) {
53 this.handleDirSep = handleDirSep;
54 }
55
56 /**
57 * Attribute specifing whether to ignore the case difference
58 * in the names.
59 *
60 * @param caseSensitive a boolean, default is false.
61 * @since Ant 1.6.3
62 */
63 public void setCaseSensitive(boolean caseSensitive) {
64 if (!caseSensitive) {
65 regexpOptions = RegexpMatcher.MATCH_CASE_INSENSITIVE;
66 } else {
67 regexpOptions = 0;
68 }
69 }
70
71 /**
72 * Sets the "from" pattern. Required.
73 * @param from the from pattern.
74 * @throws BuildException on error.
75 */
76 public void setFrom(String from) throws BuildException {
77 try {
78 reg.setPattern(from);
79 } catch (NoClassDefFoundError e) {
80 // depending on the implementation the actual RE won't
81 // get instantiated in the constructor.
82 throw new BuildException("Cannot load regular expression matcher",
83 e);
84 }
85 }
86
87 /**
88 * Sets the "to" pattern. Required.
89 * @param to the to pattern.
90 * @throws BuildException on error.
91 */
92 public void setTo(String to) {
93 this.to = to.toCharArray();
94 }
95
96 /**
97 * Returns null if the source file name doesn't match the
98 * "from" pattern, an one-element array containing the
99 * translated file otherwise.
100 * @param sourceFileName the source file name
101 * @return a one-element array containing the translated file or
102 * null if the to pattern did not match
103 */
104 public String[] mapFileName(String sourceFileName) {
105 if (handleDirSep) {
106 if (sourceFileName.indexOf("\\") != -1) {
107 sourceFileName = sourceFileName.replace('\\', '/');
108 }
109 }
110 if (reg == null || to == null
111 || !reg.matches(sourceFileName, regexpOptions)) {
112 return null;
113 }
114 return new String[] {replaceReferences(sourceFileName)};
115 }
116
117 /**
118 * Replace all backreferences in the to pattern with the matched
119 * groups of the source.
120 * @param source the source file name.
121 * @return the translated file name.
122 */
123 protected String replaceReferences(String source) {
124 Vector v = reg.getGroups(source, regexpOptions);
125
126 result.setLength(0);
127 for (int i = 0; i < to.length; i++) {
128 if (to[i] == '\\') {
129 if (++i < to.length) {
130 int value = Character.digit(to[i], 10);
131 if (value > -1) {
132 result.append((String) v.elementAt(value));
133 } else {
134 result.append(to[i]);
135 }
136 } else {
137 // XXX - should throw an exception instead?
138 result.append('\\');
139 }
140 } else {
141 result.append(to[i]);
142 }
143 }
144 return result.substring(0);
145 }
146
147}
Note: See TracBrowser for help on using the repository browser.