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: public final class Pattern {
014: public static final Pattern[] EMPTY_PATTERN_ARRAY = new Pattern[0];
015:
016: private final String orgInput;
017: private final String[] subStrings;
018:
019: final boolean ignoreCase;
020: final boolean starsWithAsterisk;
021: final boolean endsWithAsterisk;
022: final boolean onlyAsterisk;
023: final boolean negate;
024: final String input;
025: final boolean hasAsterisk;
026:
027: boolean recursive = false;
028:
029: public Pattern(final String aString) {
030: this (aString, false);
031: }//constructor
032:
033: public Pattern(String aString, final boolean aIgnoreCase) {
034:
035: orgInput = aString;
036: String lInput;
037: //BUG 4965568
038: if (aString.startsWith(Constants.RECURSIVE)) {
039: recursive = true;
040: aString = aString.substring(Constants.RECURSIVE.length());
041: }
042: if (aString.startsWith(Constants.NEGATE)) {
043: negate = true;
044: lInput = aString.substring(1);
045: } else {
046: negate = false;
047: if (aString.startsWith("\\!")) //escape the not match
048: {
049: lInput = aString.substring(1);
050: } else {
051: lInput = aString;
052: }
053: }
054:
055: ignoreCase = aIgnoreCase;
056:
057: starsWithAsterisk = lInput.startsWith(Constants.ASTERISK);
058: if (!lInput.endsWith(Constants.ESCAPE_ASTERISK)
059: && lInput.endsWith(Constants.ASTERISK)) {
060: endsWithAsterisk = true;
061: } else {
062: endsWithAsterisk = false;
063: }
064:
065: if (lInput.length() == 1) {
066: onlyAsterisk = starsWithAsterisk;
067: } else {
068: onlyAsterisk = false;
069: }
070:
071: int i = 0;
072: boolean lHasAsterisk = false;
073: while ((i < lInput.length()) && !lHasAsterisk) {
074: i = lInput.indexOf(Constants.CHAR_ASTERISK, i);
075:
076: if (i == -1) {
077: lHasAsterisk = false;
078: break;
079: } else if (i == 0) {
080: lHasAsterisk = true;
081: } else if (lInput.charAt(i - 1) == Constants.ESCAPE_FORWARD_SLASH_CHAR) {
082: i = i + 1;
083: } else {
084: lHasAsterisk = true;
085: }
086: }
087:
088: hasAsterisk = lHasAsterisk;
089: subStrings = parseSubStrings(lInput, ignoreCase);
090: input = StringHelper.searchAndReplace(lInput,
091: Constants.ESCAPE_ASTERISK, Constants.ASTERISK);
092: }//constructor
093:
094: public String getInput() {
095: return input;
096: }//getInput()
097:
098: public String[] getSubStrings() {
099: return subStrings;
100: }//getSubStrings()
101:
102: private static final String[] parseSubStrings(String aInput,
103: boolean aIgnoreCase) {
104: String[] lSubStrings;
105:
106: if (aIgnoreCase) {
107: lSubStrings = StringHelper.tokenize(aInput.toLowerCase(),
108: Constants.ASTERISK);
109: } else {
110: lSubStrings = StringHelper.tokenize(aInput,
111: Constants.ASTERISK);
112: }
113:
114: return lSubStrings;
115: }//parseSubStrings();
116:
117: public static final Pattern[] convert2Patterns(final String[] aArray) {
118: return convert2Patterns(aArray, false);
119: }//convert2Patterns()
120:
121: public static final Pattern[] convert2Patterns(
122: final String[] aArray, final boolean aIgnoreCase) {
123: if (aArray == null || aArray.length == 0) {
124: return EMPTY_PATTERN_ARRAY;
125: }
126:
127: List lList = new ArrayList();
128: for (int i = 0; i < aArray.length; i++) {
129: if (aArray[i] != null) {
130: lList.add(new Pattern(aArray[i], aIgnoreCase));
131: }
132: }
133:
134: return (Pattern[]) lList.toArray(Pattern.EMPTY_PATTERN_ARRAY);
135: }//convert2Patterns()
136:
137: public String toString() {
138: return orgInput;
139: }//toString()
140:
141: public boolean isRecursive() {
142: return recursive;
143: }
144:
145: }//class Pattern
|