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.common;
006:
007: import com.sun.portal.rewriter.rom.Data;
008: import com.sun.portal.rewriter.rom.DataRule;
009: import com.sun.portal.rewriter.util.Constants;
010: import com.sun.portal.rewriter.util.Debug;
011: import com.sun.portal.rewriter.util.collections.ListMap;
012: import com.sun.portal.rewriter.util.re.Pattern;
013: import com.sun.portal.rewriter.util.xml.Node;
014:
015: import java.util.List;
016:
017: /**
018: * Represents TagText Rule of RuleSet, used to by XMLRewriter
019: *
020: * @version 1.0 12/15/2001
021: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
022: */
023: public final class TagTextRule extends DataRule {
024: private final TagText tagText;
025: private final Pattern[] tagSpec;
026: private final Pattern[][] tagAttributeSpec;
027: private final boolean isStarAttributeValue;
028:
029: public TagTextRule(final TagText aTagText) {
030: this (aTagText, true);
031: }//constructor
032:
033: public TagTextRule(final TagText aTagText, final boolean aIgnoreCase)
034:
035: {
036: super (aTagText);
037: tagText = aTagText;
038:
039: tagSpec = createAttributeSpec(tagText.getTag(), aIgnoreCase);
040: tagAttributeSpec = new Pattern[aTagText.getAttributeMap()
041: .size()][2];
042:
043: fillTagAttributeSpec(aIgnoreCase);
044:
045: if (aTagText.getAttributePatterns().equals(
046: Constants.DEFAULT_REGULAR_EXPRESSION)) {
047: isStarAttributeValue = true;
048: } else {
049: isStarAttributeValue = false;
050: }
051: }//constructor
052:
053: public TagTextRule(final Node aNode, final boolean aIgnoreCase) {
054: this (new TagText(aNode.getAttributeValue(TAG), aNode
055: .getAttributeValue(ATTRIBUTE_PATTERNS), aNode
056: .getAttributeValue(SOURCE)), aIgnoreCase);
057: }//constructor
058:
059: private void fillTagAttributeSpec(final boolean aIgnoreCase) {
060: ListMap cAttMap = tagText.getAttributeMap();
061: List cAttNames = cAttMap.keyList();
062: for (int i = 0; i < cAttNames.size(); i++) {
063: String bAttName = (String) cAttNames.get(i);
064: tagAttributeSpec[i][0] = new Pattern(bAttName, aIgnoreCase);
065: tagAttributeSpec[i][1] = new Pattern((String) cAttMap
066: .get(bAttName), aIgnoreCase);
067: }
068: }//fillTagAttributeSpec()
069:
070: public boolean plugableMatch(final Data aMache) {
071: if (!(aMache instanceof TagText)) {
072: return false;
073: }
074:
075: TagText valueObject = (TagText) aMache;
076: if (match(tagSpec, valueObject.getTag())) {
077: //BugNo:4892729
078: if (isStarAttributeValue) {
079: return true;
080: }
081:
082: ListMap valueMap = valueObject.getAttributeMap();
083: List valueKeyList = valueMap.keyList();
084: for (int i = 0; i < valueKeyList.size(); i++) {
085: String valueKey = valueKeyList.get(i).toString();
086: Pattern valueRule2Match = findValueRule2Match(valueKey);
087:
088: if (valueRule2Match == null) {
089: return false;
090: } else if (!match(valueRule2Match, valueMap.get(
091: valueKey).toString())) {
092: return false;
093: }
094: }
095:
096: if (valueMap.size() >= tagAttributeSpec.length) {
097: return true;
098: }
099:
100: return false;
101: }//if
102:
103: return false;
104: }//matchs()
105:
106: public boolean isValid() {
107: return doBasicValidation(new String[] { tagText.getTag() });
108: }//isValid()
109:
110: private Pattern findValueRule2Match(final String aKey) {
111: for (int i = 0; i < tagAttributeSpec.length; i++) {
112: if (match(tagAttributeSpec[i][0], aKey)) {
113: return tagAttributeSpec[i][1];
114: }
115: }//for loop
116: return null;
117: }//findValueRule2Match()
118:
119: public static final TagTextRule[] createTagTextRules(
120: final Node[] aNodeList, final boolean aIgnoreCase) {
121: TagTextRule[] lResult = new TagTextRule[aNodeList.length];
122: for (int i = 0; i < aNodeList.length; i++) {
123: lResult[i] = new TagTextRule(aNodeList[i], aIgnoreCase);
124: }
125:
126: return lResult;
127: }//createTagTextRules()
128:
129: public static void main(String[] args) {
130: TagTextRule[] textStrings = com.sun.portal.rewriter.test.util.SampleRuleObjects.defaultXMLTextStrings;
131: for (int i = 0; i < textStrings.length; i++) {
132: Debug.println(textStrings[i].toXML());
133: }//for loop
134: }//main()
135:
136: }//class TagText
|