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.rom;
006:
007: import com.sun.portal.rewriter.util.Constants;
008: import com.sun.portal.rewriter.util.re.Pattern;
009: import com.sun.portal.rewriter.util.re.RegExp;
010:
011: public abstract class DataRule implements Rule {
012: public static final DataRule[] EMPTY_DATA_RULE_ARRAY = new DataRule[0];
013:
014: private final Data data;
015: private final Pattern[] sourceSpec;
016:
017: protected DataRule(final Data aData) {
018: data = aData;
019: sourceSpec = createAttributeSpec(data.getSource(), true);
020: }//constructor
021:
022: public final String getCollectionID() {
023: return data.getCollectionID();
024: }//getCollectionID()
025:
026: public final Data getData() {
027: return data;
028: }//getData()
029:
030: public final String toXML() {
031: return data.toXML();
032: }//toXML()
033:
034: public final boolean matches(Data aMache) {
035: if (aMache != null && plugableMatch(aMache)) {
036: return match(sourceSpec, aMache.getSource());
037: }
038:
039: return false;
040: }//matches()
041:
042: protected abstract boolean plugableMatch(Data aMache);
043:
044: public abstract boolean isValid();
045:
046: protected static Pattern[] createAttributeSpec(String aRule) {
047: return createAttributeSpec(aRule, false);
048: }//createAttributeSpec()
049:
050: protected static Pattern[] createAttributeSpec(String aRule,
051: boolean aIgnoreCase) {
052: return Pattern.convert2Patterns(RegExp.splitElements(aRule,
053: Constants.MULTI_VALUE_DELIMITER), aIgnoreCase);
054: }//createAttributeSpec()
055:
056: public static final Pattern[] createValuePatternSpec(
057: final String aData) {
058: return Pattern.convert2Patterns(RegExp.parsePatterns(aData,
059: Constants.MULTI_VALUE_DELIMITER,
060: Constants.CHILD_PATTERN));
061: }//createValuePatternSpec()
062:
063: /**
064: * All the strings all ready normalized so no need to do
065: * do extra checking for null and also trim() each string
066: * does validation which can't be done via DTD
067: */
068: protected static final boolean doBasicValidation(
069: final String[] valueList) {
070: //make sure all string length is not equal to 0
071: int len = 0;
072: for (int i = valueList.length - 1; i >= 0; i--) {
073: if ((len = valueList[i].length()) > 0) {
074: break;
075: }
076: }//for loop
077:
078: if (len <= 0) {
079: return false;
080: } else {
081: //make sure all the strings are not equal to *
082: // as this can disallow all other rules
083: for (int i = valueList.length - 1; i >= 0; i--) {
084: if (!(valueList[i]
085: .equals(Constants.DEFAULT_REGULAR_EXPRESSION))) {
086: return true;
087: }
088: }
089: return false;
090: }
091: }//doBasicValidation()
092:
093: protected static final boolean match(final Pattern[] aRules,
094: final String aValue) {
095: return RegExp.match(aRules, aValue);
096: }//match()
097:
098: protected static final boolean match(final Pattern aRule,
099: final String aValue) {
100: return RegExp.match(aRule, aValue);
101: }//match()
102: }//DataRuleHelper
|