001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.util.re;
006:
007: import com.sun.portal.rewriter.util.Constants;
008: import com.sun.portal.rewriter.util.StringHelper;
009:
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: /**
014: * Class which does the required Regular Expression matching. The current
015: * implementation just supports "*" as Regular Expression pattern
016: *
017: * @version 1.0 12/15/2001
018: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
019: */
020: public final class RegExp {
021: public static final String[] splitElements(
022: final String aPatternsString, final String aPatternSeperator) {
023: return parsePatterns(aPatternsString, aPatternSeperator, null);
024: }//createValuePatternSpec()
025:
026: public static final String[] parsePatterns(
027: final String aPatternsString,
028: final String aPatternSeperator, final String aChildPattern) {
029: if (aPatternsString.length() == 0) {
030: return Constants.EMPTY_STRING_ARRAY;
031: }
032:
033: List lList = new ArrayList();
034: String[] stz = StringHelper.tokenize(aPatternsString,
035: aPatternSeperator);
036: String rule;
037: String childRule;
038: for (int i = 0; i < stz.length; i++) {
039: rule = stz[i];
040:
041: if (rule.equals(Constants.TRANSLATE_FULL_STRING)) {
042: lList.add(Constants.EMPTY_STRING);
043: continue;
044: }
045:
046: if (aChildPattern != null) {
047: int childIndex = 0;
048: while (childIndex < rule.length()) {
049: childRule = "";
050: childIndex = rule.indexOf(aChildPattern);
051: if (childIndex == -1) {
052: break;
053: } else if (childIndex == 0) {
054: lList
055: .add(Constants.ZERO_POSTITION_CHILD_PATTERN_INDICATOR);
056: } else if (rule.charAt(childIndex - 1) != Constants.ESCAPE_FORWARD_SLASH_CHAR) {
057: childRule = rule.substring(0, childIndex);
058: lList.add(childRule);
059: }
060:
061: rule = childRule
062: + "*"
063: + rule.substring(childIndex
064: + aChildPattern.length()); //one is skipt the lenght of child pattern
065: }//while loop
066: }//if
067:
068: lList.add(rule);
069: }//while loop
070:
071: return (String[]) lList.toArray(Constants.EMPTY_STRING_ARRAY);
072: }//parsedPatterns()
073:
074: public static int indexOf(final Pattern aRulePattern, String aValue) {
075: if (aValue == null || aValue.length() == 0
076: || aRulePattern.input.length() == 0) {
077: return -1;
078: }
079:
080: if (aRulePattern.ignoreCase) {
081: aValue = aValue.toLowerCase();
082: }
083:
084: String[] sArray = aRulePattern.getSubStrings();
085: int size = sArray.length;
086: if (size == 0) {
087: return aValue.length();
088: }
089:
090: String token;
091: boolean isFirstToken = true;
092: int index;
093: int fromIndex = 0;
094: for (int i = 0; i < size; i++) {
095: if (fromIndex >= aValue.length()) {
096: return -1;
097: }
098:
099: token = sArray[i];
100: index = aValue.indexOf(token, fromIndex);
101: if (index == -1) {
102: return -1;
103: }
104:
105: if (isFirstToken) {
106: if (!aRulePattern.starsWithAsterisk && (index != 0)) {
107: return -1;
108: }
109:
110: isFirstToken = false;
111: }
112:
113: fromIndex = index + token.length();
114: }//while loop
115:
116: if (aRulePattern.endsWithAsterisk) {
117: return aValue.length();
118: }
119:
120: return fromIndex;
121: }//indexOf()
122:
123: public static final boolean match(final Pattern[] aMatchPatterns,
124: final String aValue) {
125: for (int i = 0; i < aMatchPatterns.length; i++) {
126: if (match(aMatchPatterns[i], aValue)) {
127: return true;
128: }
129: }
130:
131: return false;
132: }//match()
133:
134: private final static boolean matchImpl(final Pattern aMatchPattern,
135: String aValue) {
136: if (aValue == null) {
137: return false;
138: }
139:
140: if (aMatchPattern.onlyAsterisk) {
141: return true;
142: }
143:
144: aValue = aValue.trim();
145: if (aMatchPattern.hasAsterisk == false) {
146: if (aMatchPattern.ignoreCase == true) {
147: return aMatchPattern.input.equalsIgnoreCase(aValue);
148: } else {
149: return aMatchPattern.input.equals(aValue);
150: }
151: }
152:
153: int index = RegExp.indexOf(aMatchPattern, aValue);
154: if (index == -1) {
155: return false;
156: }
157:
158: return index == aValue.length();
159: }//matchImpl()
160:
161: public final static boolean match(final Pattern aMatchPattern,
162: String aValue) {
163: boolean lResult = matchImpl(aMatchPattern, aValue);
164: if (aMatchPattern.negate) {
165: lResult = !lResult;
166: }
167:
168: return lResult;
169: }//match()
170:
171: public static void main(String[] args) {
172: System.out.println(indexOf(new Pattern(args[0]), args[1]));
173: }//main()
174:
175: }//class RegExp
|