KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > RegexpPatternMapper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util;
20
21 import java.util.Vector JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.util.regexp.RegexpMatcher;
24 import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
25
26 /**
27  * Implementation of FileNameMapper that does regular expression
28  * replacements.
29  *
30  */

31 public class RegexpPatternMapper implements FileNameMapper {
32     // CheckStyle:VisibilityModifier OFF - bc
33
protected RegexpMatcher reg = null;
34     protected char[] to = null;
35     protected StringBuffer JavaDoc result = new StringBuffer JavaDoc();
36     // CheckStyle:VisibilityModifier ON
37

38     /**
39      * Constructor for RegexpPatternMapper.
40      * @throws BuildException on error.
41      */

42     public RegexpPatternMapper() throws BuildException {
43         reg = (new RegexpMatcherFactory()).newRegexpMatcher();
44     }
45
46     private boolean handleDirSep = false;
47     private int regexpOptions = 0;
48
49     /**
50      * Attribute specifing whether to ignore the difference
51      * between / and \ (the two common directory characters).
52      * @param handleDirSep a boolean, default is false.
53      * @since Ant 1.6.3
54      */

55     public void setHandleDirSep(boolean handleDirSep) {
56         this.handleDirSep = handleDirSep;
57     }
58
59     /**
60      * Attribute specifing whether to ignore the case difference
61      * in the names.
62      *
63      * @param caseSensitive a boolean, default is false.
64      * @since Ant 1.6.3
65      */

66     public void setCaseSensitive(boolean caseSensitive) {
67         if (!caseSensitive) {
68             regexpOptions = RegexpMatcher.MATCH_CASE_INSENSITIVE;
69         } else {
70             regexpOptions = 0;
71         }
72     }
73
74     /**
75      * Sets the "from" pattern. Required.
76      * @param from the from pattern.
77      * @throws BuildException on error.
78      */

79     public void setFrom(String JavaDoc from) throws BuildException {
80         try {
81             reg.setPattern(from);
82         } catch (NoClassDefFoundError JavaDoc e) {
83             // depending on the implementation the actual RE won't
84
// get instantiated in the constructor.
85
throw new BuildException("Cannot load regular expression matcher",
86                                      e);
87         }
88     }
89
90     /**
91      * Sets the "to" pattern. Required.
92      * @param to the to pattern.
93      * @throws BuildException on error.
94      */

95     public void setTo(String JavaDoc to) {
96         this.to = to.toCharArray();
97     }
98
99     /**
100      * Returns null if the source file name doesn't match the
101      * "from" pattern, an one-element array containing the
102      * translated file otherwise.
103      * @param sourceFileName the source file name
104      * @return a one-element array containing the translated file or
105      * null if the to pattern did not match
106      */

107     public String JavaDoc[] mapFileName(String JavaDoc sourceFileName) {
108         if (handleDirSep) {
109             if (sourceFileName.indexOf("\\") != -1) {
110                 sourceFileName = sourceFileName.replace('\\', '/');
111             }
112         }
113         if (reg == null || to == null
114             || !reg.matches(sourceFileName, regexpOptions)) {
115             return null;
116         }
117         return new String JavaDoc[] {replaceReferences(sourceFileName)};
118     }
119
120     /**
121      * Replace all backreferences in the to pattern with the matched
122      * groups of the source.
123      * @param source the source file name.
124      * @return the translated file name.
125      */

126     protected String JavaDoc replaceReferences(String JavaDoc source) {
127         Vector JavaDoc v = reg.getGroups(source, regexpOptions);
128
129         result.setLength(0);
130         for (int i = 0; i < to.length; i++) {
131             if (to[i] == '\\') {
132                 if (++i < to.length) {
133                     int value = Character.digit(to[i], 10);
134                     if (value > -1) {
135                         result.append((String JavaDoc) v.elementAt(value));
136                     } else {
137                         result.append(to[i]);
138                     }
139                 } else {
140                     // XXX - should throw an exception instead?
141
result.append('\\');
142                 }
143             } else {
144                 result.append(to[i]);
145             }
146         }
147         return result.substring(0);
148     }
149
150 }
151
Popular Tags