001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util;
020:
021: /**
022: * Implementation of FileNameMapper that does simple wildcard pattern
023: * replacements.
024: *
025: * <p>This does simple translations like *.foo -> *.bar where the
026: * prefix to .foo will be left unchanged. It only handles a single *
027: * character, use regular expressions for more complicated
028: * situations.</p>
029: *
030: * <p>This is one of the more useful Mappers, it is used by javac for
031: * example.</p>
032: *
033: */
034: public class GlobPatternMapper implements FileNameMapper {
035:
036: // CheckStyle:VisibilityModifier OFF - bc
037: /**
038: * Part of "from" pattern before the *.
039: */
040: protected String fromPrefix = null;
041:
042: /**
043: * Part of "from" pattern after the *.
044: */
045: protected String fromPostfix = null;
046:
047: /**
048: * Length of the prefix ("from" pattern).
049: */
050: protected int prefixLength;
051:
052: /**
053: * Length of the postfix ("from" pattern).
054: */
055: protected int postfixLength;
056:
057: /**
058: * Part of "to" pattern before the *.
059: */
060: protected String toPrefix = null;
061:
062: /**
063: * Part of "to" pattern after the *.
064: */
065: protected String toPostfix = null;
066:
067: // CheckStyle:VisibilityModifier ON
068:
069: private boolean handleDirSep = false;
070: private boolean caseSensitive = true;
071:
072: /**
073: * Attribute specifing whether to ignore the difference
074: * between / and \ (the two common directory characters).
075: * @param handleDirSep a boolean, default is false.
076: * @since Ant 1.6.3
077: */
078: public void setHandleDirSep(boolean handleDirSep) {
079: this .handleDirSep = handleDirSep;
080: }
081:
082: /**
083: * Attribute specifing whether to ignore the case difference
084: * in the names.
085: *
086: * @param caseSensitive a boolean, default is false.
087: * @since Ant 1.6.3
088: */
089: public void setCaseSensitive(boolean caseSensitive) {
090: this .caseSensitive = caseSensitive;
091: }
092:
093: /**
094: * Sets the "from" pattern. Required.
095: * @param from a string
096: */
097: public void setFrom(String from) {
098: int index = from.lastIndexOf("*");
099: 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 "to" pattern. Required.
112: * @param to a string
113: */
114: public void setTo(String 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: * "from" 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[] mapFileName(String sourceFileName) {
133: if (fromPrefix == null
134: || !modifyName(sourceFileName).startsWith(
135: modifyName(fromPrefix))
136: || !modifyName(sourceFileName).endsWith(
137: modifyName(fromPostfix))) {
138: return null;
139: }
140: return new String[] { toPrefix
141: + extractVariablePart(sourceFileName) + toPostfix };
142: }
143:
144: /**
145: * Returns the part of the given string that matches the * in the
146: * "from" pattern.
147: * @param name the source file name
148: * @return the variable part of the name
149: */
150: protected String extractVariablePart(String name) {
151: return name.substring(prefixLength, name.length()
152: - postfixLength);
153: }
154:
155: /**
156: * modify string based on dir char mapping and case sensitivity
157: * @param name the name to convert
158: * @return the converted name
159: */
160: private String modifyName(String name) {
161: if (!caseSensitive) {
162: name = name.toLowerCase();
163: }
164: if (handleDirSep) {
165: if (name.indexOf('\\') != -1) {
166: name = name.replace('\\', '/');
167: }
168: }
169: return name;
170: }
171: }
|