KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
22  * Implementation of FileNameMapper that does simple wildcard pattern
23  * replacements.
24  *
25  * <p>This does simple translations like *.foo -> *.bar where the
26  * prefix to .foo will be left unchanged. It only handles a single *
27  * character, use regular expressions for more complicated
28  * situations.</p>
29  *
30  * <p>This is one of the more useful Mappers, it is used by javac for
31  * example.</p>
32  *
33  */

34 public class GlobPatternMapper implements FileNameMapper {
35
36     // CheckStyle:VisibilityModifier OFF - bc
37
/**
38      * Part of &quot;from&quot; pattern before the *.
39      */

40     protected String JavaDoc fromPrefix = null;
41
42     /**
43      * Part of &quot;from&quot; pattern after the *.
44      */

45     protected String JavaDoc fromPostfix = null;
46
47     /**
48      * Length of the prefix (&quot;from&quot; pattern).
49      */

50     protected int prefixLength;
51
52     /**
53      * Length of the postfix (&quot;from&quot; pattern).
54      */

55     protected int postfixLength;
56
57     /**
58      * Part of &quot;to&quot; pattern before the *.
59      */

60     protected String JavaDoc toPrefix = null;
61
62     /**
63      * Part of &quot;to&quot; pattern after the *.
64      */

65     protected String JavaDoc toPostfix = null;
66
67     // CheckStyle:VisibilityModifier ON
68

69     private boolean handleDirSep = false;
70     private boolean caseSensitive = true;
71
72     /**
73      * Attribute specifing whether to ignore the difference
74      * between / and \ (the two common directory characters).
75      * @param handleDirSep a boolean, default is false.
76      * @since Ant 1.6.3
77      */

78     public void setHandleDirSep(boolean handleDirSep) {
79         this.handleDirSep = handleDirSep;
80     }
81
82     /**
83      * Attribute specifing whether to ignore the case difference
84      * in the names.
85      *
86      * @param caseSensitive a boolean, default is false.
87      * @since Ant 1.6.3
88      */

89     public void setCaseSensitive(boolean caseSensitive) {
90         this.caseSensitive = caseSensitive;
91     }
92
93     /**
94      * Sets the &quot;from&quot; pattern. Required.
95      * @param from a string
96      */

97     public void setFrom(String JavaDoc from) {
98         int index = from.lastIndexOf("*");
99         if (index == -1) {
100             fromPrefix = from;
101             fromPostfix = "";
102         } else {
103             fromPrefix = from.substring(0, index);
104             fromPostfix = from.substring(index + 1);
105         }
106         prefixLength = fromPrefix.length();
107         postfixLength = fromPostfix.length();
108     }
109
110     /**
111      * Sets the &quot;to&quot; pattern. Required.
112      * @param to a string
113      */

114     public void setTo(String JavaDoc to) {
115         int index = to.lastIndexOf("*");
116         if (index == -1) {
117             toPrefix = to;
118             toPostfix = "";
119         } else {
120             toPrefix = to.substring(0, index);
121             toPostfix = to.substring(index + 1);
122         }
123     }
124
125     /**
126      * Returns null if the source file name doesn't match the
127      * &quot;from&quot; pattern, an one-element array containing the
128      * translated file otherwise.
129      * @param sourceFileName the filename to map
130      * @return a list of converted filenames
131      */

132     public String JavaDoc[] mapFileName(String JavaDoc sourceFileName) {
133         if (fromPrefix == null
134             || !modifyName(sourceFileName).startsWith(modifyName(fromPrefix))
135             || !modifyName(sourceFileName).endsWith(modifyName(fromPostfix))) {
136             return null;
137         }
138         return new String JavaDoc[] {toPrefix
139                                  + extractVariablePart(sourceFileName)
140                                  + toPostfix};
141     }
142
143     /**
144      * Returns the part of the given string that matches the * in the
145      * &quot;from&quot; pattern.
146      * @param name the source file name
147      * @return the variable part of the name
148      */

149     protected String JavaDoc extractVariablePart(String JavaDoc name) {
150         return name.substring(prefixLength,
151                               name.length() - postfixLength);
152     }
153
154     /**
155      * modify string based on dir char mapping and case sensitivity
156      * @param name the name to convert
157      * @return the converted name
158      */

159     private String JavaDoc modifyName(String JavaDoc name) {
160         if (!caseSensitive) {
161             name = name.toLowerCase();
162         }
163         if (handleDirSep) {
164             if (name.indexOf('\\') != -1) {
165                 name = name.replace('\\', '/');
166             }
167         }
168         return name;
169     }
170 }
171
Popular Tags