001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.backend.list;
016:
017: import org.apache.commons.lang.ArrayUtils;
018: import org.apache.commons.lang.StringUtils;
019: import org.araneaframework.uilib.list.util.like.LikeConfiguration;
020: import org.araneaframework.uilib.list.util.like.RegexpLikeUtil;
021: import org.araneaframework.uilib.list.util.like.WildcardHandler;
022: import org.araneaframework.uilib.list.util.like.WildcardUtil;
023:
024: /**
025: * Helper class for database <code>LIKE</code> expression.
026: *
027: * @author <a href="mailto:rein@araneaframework.org">Rein Raudjärv</a>
028: *
029: * @see RegexpLikeUtil
030: */
031: public class SqlLikeUtil {
032:
033: public static final String SQL_LIKE_ANY_STRING_WILDCARD = "%";
034: public static final String SQL_LIKE_ANY_CHAR_WILDCARD = "_";
035:
036: /**
037: * Converts SQL <code>LIKE</code> mask in custom format (specified by
038: * {@link LikeConfiguration}) into SQL Like standard format.
039: * <p<
040: * The output may contain escape characters. Therefore the result must be
041: * used in format (expression) LIKE (mask) ESCAPE (escapeChar).
042: * </p>
043: *
044: * @param mask mask in custom format.
045: * @param config configuration that holds mask custom format and behaivor.
046: * @param escapeChar escape character for SQL syntax.
047: * @return the mask in standard format.
048: */
049: public static String convertMask(String mask,
050: LikeConfiguration config, String escapeChar) {
051: if (mask == null) {
052: return null;
053: }
054: if (config == null) {
055: throw new IllegalArgumentException(
056: "Like configuration must be specified");
057: }
058: if (escapeChar == null) {
059: throw new IllegalArgumentException(
060: "Escape character must be specified");
061: }
062:
063: // Escape
064: mask = StringUtils.replace(mask, escapeChar, escapeChar
065: + escapeChar);
066: if (!ArrayUtils.contains(config.getAnyStringWildcards(),
067: SQL_LIKE_ANY_STRING_WILDCARD)) {
068: mask = StringUtils.replace(mask,
069: SQL_LIKE_ANY_STRING_WILDCARD, escapeChar
070: + SQL_LIKE_ANY_STRING_WILDCARD);
071: }
072: if (!ArrayUtils.contains(config.getAnyCharWildcards(),
073: SQL_LIKE_ANY_CHAR_WILDCARD)) {
074: mask = StringUtils.replace(mask,
075: SQL_LIKE_ANY_CHAR_WILDCARD, escapeChar
076: + SQL_LIKE_ANY_CHAR_WILDCARD);
077: }
078:
079: // Convert wildcards
080: mask = replace(mask, config.getAnyStringWildcards(),
081: SQL_LIKE_ANY_STRING_WILDCARD);
082: mask = replace(mask, config.getAnyCharWildcards(),
083: SQL_LIKE_ANY_CHAR_WILDCARD);
084:
085: // Handle wildcards at the start and end
086: WildcardHandler handler = config.createWildcardHandler();
087: WildcardUtil.setWildcards(handler, mask,
088: SQL_LIKE_ANY_STRING_WILDCARD,
089: SQL_LIKE_ANY_CHAR_WILDCARD);
090: if (handler.getStartsWith() != handler.shouldStartWith()) {
091: if (handler.getStartsWith() == WildcardHandler.ANY_STRING_WILDCARD) {
092: mask = mask.substring(SQL_LIKE_ANY_STRING_WILDCARD
093: .length());
094: } else if (handler.getStartsWith() == WildcardHandler.ANY_CHAR_WILDCARD) {
095: mask = mask.substring(SQL_LIKE_ANY_CHAR_WILDCARD
096: .length());
097: }
098:
099: if (handler.shouldStartWith() == WildcardHandler.ANY_STRING_WILDCARD) {
100: mask = SQL_LIKE_ANY_STRING_WILDCARD + mask;
101: } else if (handler.shouldStartWith() == WildcardHandler.ANY_CHAR_WILDCARD) {
102: mask = SQL_LIKE_ANY_CHAR_WILDCARD + mask;
103: }
104: }
105: if (handler.getEndsWith() != handler.shouldEndWith()) {
106: if (handler.getEndsWith() == WildcardHandler.ANY_STRING_WILDCARD) {
107: mask = mask.substring(0, mask.length()
108: - SQL_LIKE_ANY_STRING_WILDCARD.length());
109: } else if (handler.getEndsWith() == WildcardHandler.ANY_CHAR_WILDCARD) {
110: mask = mask.substring(0, mask.length()
111: - SQL_LIKE_ANY_CHAR_WILDCARD.length());
112: }
113:
114: if (handler.shouldEndWith() == WildcardHandler.ANY_STRING_WILDCARD) {
115: mask = mask + SQL_LIKE_ANY_STRING_WILDCARD;
116: } else if (handler.shouldEndWith() == WildcardHandler.ANY_CHAR_WILDCARD) {
117: mask = mask + SQL_LIKE_ANY_CHAR_WILDCARD;
118: }
119: }
120: return mask;
121: }
122:
123: /**
124: * <p>Replaces all occurrences of the Strings within another String.</p>
125: *
126: * @param text text to search and replace in
127: * @param repl the Strings to search for
128: * @param with the String to replace with
129: * @return the text with any replacements processed,
130: */
131: private static String replace(String test, String[] repl,
132: String with) {
133: for (int i = 0; i < repl.length; i++) {
134: test = StringUtils.replace(test, repl[i], with);
135: }
136: return test;
137: }
138:
139: }
|