source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/GlobPatternMapper.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: 5.0 KB
Line 
1/*
2 * Copyright 2000,2002,2004-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
20/**
21 * Implementation of FileNameMapper that does simple wildcard pattern
22 * replacements.
23 *
24 * <p>This does simple translations like *.foo -> *.bar where the
25 * prefix to .foo will be left unchanged. It only handles a single *
26 * character, use regular expressions for more complicated
27 * situations.</p>
28 *
29 * <p>This is one of the more useful Mappers, it is used by javac for
30 * example.</p>
31 *
32 */
33public class GlobPatternMapper implements FileNameMapper {
34
35 /**
36 * Part of &quot;from&quot; pattern before the *.
37 */
38 protected String fromPrefix = null;
39
40 /**
41 * Part of &quot;from&quot; pattern after the *.
42 */
43 protected String fromPostfix = null;
44
45 /**
46 * Length of the prefix (&quot;from&quot; pattern).
47 */
48 protected int prefixLength;
49
50 /**
51 * Length of the postfix (&quot;from&quot; pattern).
52 */
53 protected int postfixLength;
54
55 /**
56 * Part of &quot;to&quot; pattern before the *.
57 */
58 protected String toPrefix = null;
59
60 /**
61 * Part of &quot;to&quot; pattern after the *.
62 */
63 protected String toPostfix = null;
64
65 private boolean handleDirSep = false;
66 private boolean caseSensitive = true;
67
68 /**
69 * Attribute specifing whether to ignore the difference
70 * between / and \ (the two common directory characters).
71 * @param handleDirSep a boolean, default is false.
72 * @since Ant 1.6.3
73 */
74 public void setHandleDirSep(boolean handleDirSep) {
75 this.handleDirSep = handleDirSep;
76 }
77
78 /**
79 * Attribute specifing whether to ignore the case difference
80 * in the names.
81 *
82 * @param caseSensitive a boolean, default is false.
83 * @since Ant 1.6.3
84 */
85 public void setCaseSensitive(boolean caseSensitive) {
86 this.caseSensitive = caseSensitive;
87 }
88
89 /**
90 * Sets the &quot;from&quot; pattern. Required.
91 * @param from a string
92 */
93 public void setFrom(String from) {
94 int index = from.lastIndexOf("*");
95 if (index == -1) {
96 fromPrefix = from;
97 fromPostfix = "";
98 } else {
99 fromPrefix = from.substring(0, index);
100 fromPostfix = from.substring(index + 1);
101 }
102 prefixLength = fromPrefix.length();
103 postfixLength = fromPostfix.length();
104 }
105
106 /**
107 * Sets the &quot;to&quot; pattern. Required.
108 * @param to a string
109 */
110 public void setTo(String to) {
111 int index = to.lastIndexOf("*");
112 if (index == -1) {
113 toPrefix = to;
114 toPostfix = "";
115 } else {
116 toPrefix = to.substring(0, index);
117 toPostfix = to.substring(index + 1);
118 }
119 }
120
121 /**
122 * Returns null if the source file name doesn't match the
123 * &quot;from&quot; pattern, an one-element array containing the
124 * translated file otherwise.
125 * @param sourceFileName the filename to map
126 * @return a list of converted filenames
127 */
128 public String[] mapFileName(String sourceFileName) {
129 if (fromPrefix == null
130 || !modifyName(sourceFileName).startsWith(modifyName(fromPrefix))
131 || !modifyName(sourceFileName).endsWith(modifyName(fromPostfix))) {
132 return null;
133 }
134 return new String[] {toPrefix
135 + extractVariablePart(sourceFileName)
136 + toPostfix};
137 }
138
139 /**
140 * Returns the part of the given string that matches the * in the
141 * &quot;from&quot; pattern.
142 * @param name the source file name
143 * @return the variable part of the name
144 */
145 protected String extractVariablePart(String name) {
146 return name.substring(prefixLength,
147 name.length() - postfixLength);
148 }
149
150 /**
151 * modify string based on dir char mapping and case sensitivity
152 * @param name the name to convert
153 * @return the converted name
154 */
155 private String modifyName(String name) {
156 if (!caseSensitive) {
157 name = name.toLowerCase();
158 }
159 if (handleDirSep) {
160 if (name.indexOf('\\') != -1) {
161 name = name.replace('\\', '/');
162 }
163 }
164 return name;
165 }
166}
Note: See TracBrowser for help on using the repository browser.