001: package com.mockrunner.jdbc;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import com.mockrunner.util.common.StringUtil;
010:
011: /**
012: * Helper class for finding matching SQL statements based on various
013: * search parameters. The search parameters are:
014: * <br>
015: * <code>caseSensitive</code> do a case sensitive match (default is <code>false</code>)
016: * <br>
017: * <code>exactMatch</code> the strings must match exactly, the parameter <code>caseSensitive</code>
018: * is recognized, but <code>useRegularExpression</code> is irrelevant,
019: * if <code>exactMatch</code> is <code>true</code> (default is <code>false</code>)
020: * <br>
021: * <code>useRegularExpression</code> use regular expressions for matching, if this parameter is
022: * <code>false</code>, strings match, if one string starts with the other
023: * (default is <code>false</code>)
024: */
025: public class SQLStatementMatcher {
026: private boolean caseSensitive = false;
027: private boolean exactMatch = false;
028: private boolean useRegularExpressions = false;
029:
030: public SQLStatementMatcher(boolean caseSensitive, boolean exactMatch) {
031: this (caseSensitive, exactMatch, false);
032: }
033:
034: public SQLStatementMatcher(boolean caseSensitive,
035: boolean exactMatch, boolean useRegularExpressions) {
036: this .caseSensitive = caseSensitive;
037: this .exactMatch = exactMatch;
038: this .useRegularExpressions = useRegularExpressions;
039: }
040:
041: /**
042: * Compares all keys in the specified <code>Map</code> with the
043: * specified query string using the method {@link #doStringsMatch}.
044: * If the strings match, the corresponding object from the <code>Map</code>
045: * is added to the resulting <code>List</code>.
046: * @param dataMap the source <code>Map</code>
047: * @param query the query string that must match the keys in <i>dataMap</i>
048: * @param queryContainsMapData only matters if <i>isExactMatch</i> is <code>false</code>,
049: * specifies if query must be contained in the <code>Map</code> keys (<code>false</code>)
050: * or if query must contain the <code>Map</code> keys (<code>true</code>)
051: * @return the result <code>List</code>
052: */
053: public List getMatchingObjects(Map dataMap, String query,
054: boolean resolveCollection, boolean queryContainsMapData) {
055: if (null == query)
056: query = "";
057: Iterator iterator = dataMap.keySet().iterator();
058: ArrayList resultList = new ArrayList();
059: while (iterator.hasNext()) {
060: String nextKey = (String) iterator.next();
061: String source, currentQuery;
062: if (queryContainsMapData) {
063: source = query;
064: currentQuery = nextKey;
065: } else {
066: source = nextKey;
067: currentQuery = query;
068: }
069: if (doStringsMatch(source, currentQuery)) {
070: Object matchingObject = dataMap.get(nextKey);
071: if (resolveCollection
072: && (matchingObject instanceof Collection)) {
073: resultList.addAll((Collection) matchingObject);
074: } else {
075: resultList.add(dataMap.get(nextKey));
076: }
077: }
078: }
079: return resultList;
080: }
081:
082: /**
083: * Compares all elements in the specified <code>Collection</code> with the
084: * specified query string using the method {@link #doStringsMatch}.
085: * @param col the <code>Collections</code>
086: * @param query the query string that must match the keys in <i>col</i>
087: * @param queryContainsData only matters if <i>exactMatch</i> is <code>false</code>,
088: * specifies if query must be contained in the <code>Collection</code> data (<code>false</code>)
089: * or if query must contain the <code>Collection</code> data (<code>true</code>)
090: * @return <code>true</code> if <i>col</i> contains <i>query</i>, false otherwise
091: */
092: public boolean contains(Collection col, String query,
093: boolean queryContainsData) {
094: Iterator iterator = col.iterator();
095: while (iterator.hasNext()) {
096: String nextKey = (String) iterator.next();
097: String source, currentQuery;
098: if (queryContainsData) {
099: source = query;
100: currentQuery = nextKey;
101: } else {
102: source = nextKey;
103: currentQuery = query;
104: }
105: if (doStringsMatch(source, currentQuery))
106: return true;
107: }
108: return false;
109: }
110:
111: /**
112: * Compares two strings and returns if they match.
113: * @param query the query string that must match source
114: * @param source the source string
115: * @return <code>true</code> of the strings match, <code>false</code> otherwise
116: */
117: public boolean doStringsMatch(String source, String query) {
118: if (null == source)
119: source = "";
120: if (null == query)
121: query = "";
122: if (useRegularExpressions && !exactMatch) {
123: return doPerl5Match(source, query);
124: } else {
125: return doSimpleMatch(source, query);
126: }
127: }
128:
129: private boolean doSimpleMatch(String source, String query) {
130: if (exactMatch) {
131: return StringUtil
132: .matchesExact(source, query, caseSensitive);
133: } else {
134: return StringUtil.matchesContains(source, query,
135: caseSensitive);
136: }
137: }
138:
139: private boolean doPerl5Match(String source, String query) {
140: return StringUtil.matchesPerl5(source, query, caseSensitive);
141: }
142: }
|