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.rom.common.AttributeRule;
008: import com.sun.portal.rewriter.util.Debug;
009: import com.sun.portal.rewriter.util.collections.ListSet;
010: import com.sun.portal.rewriter.util.xml.Node;
011:
012: import java.lang.reflect.Constructor;
013: import java.util.Collections;
014: import java.util.List;
015:
016: /**
017: * This is Type collection which can contain any single kind of DataRule
018: * Objects
019: *
020: * @version 1.0 12/15/2001
021: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
022: */
023: public final class DataRuleCollection extends RuleCollection {
024: private List collection = new ListSet();
025:
026: private DataRuleCollection(final String aCollectionID) {
027: super (aCollectionID);
028: }//constructor
029:
030: public DataRuleCollection(final String aCollectionID,
031: final DataRule[] aDataRuleArray) {
032: this (aCollectionID);
033: add(aDataRuleArray);
034: collection = Collections.unmodifiableList(collection);
035: }//constructor
036:
037: private boolean add(final DataRule aDataRule)
038: throws InvalidCollectionIDException {
039: if (aDataRule == null) {
040: return false;
041: }
042:
043: if (getCollectionID().equals(aDataRule.getCollectionID())) {
044: if (aDataRule.isValid()) {
045: return collection.add(aDataRule);
046: }
047:
048: if (Debug.isWarningEnabled()) {
049: //Debug.recordRuleSetWarning( "Ignoring the Invalid Rule: " + aDataRule.toXML() );
050: Object[] param = { aDataRule.toXML() };
051: Debug.recordRuleSetWarning("PSRW_CSPR_0023", param);
052: }
053: return false;
054: } else {
055: throw new InvalidCollectionIDException(
056: "Error: Collection ID Expected '"
057: + getCollectionID() + "' and not of type '"
058: + aDataRule.getCollectionID() + "'");
059: }
060: }//add()
061:
062: public List getCollection() {
063: return collection;
064: }//getCollection()
065:
066: /**
067: *Once this object is exposed, outside then this become mutable object
068: */
069: private boolean add(final DataRule[] aDataRuleArray)
070: throws InvalidCollectionIDException {
071: boolean lBool = true;
072: if (aDataRuleArray == null) {
073: return false;
074: }
075:
076: for (int i = 0; i < aDataRuleArray.length; i++) {
077: try {
078: add(aDataRuleArray[i]);
079: } catch (InvalidCollectionIDException e) {
080: lBool = false;
081: //Debug.warning( "Invalid Collection ID", e );
082: Debug.warning("PSRW_CSPR_0024", e);
083: continue;
084: }
085: }
086:
087: return lBool;
088: }//add()
089:
090: public DataRule findMatch(final Data aObject) {
091: if ((null == aObject) || (collection.size() <= 0)) {
092: return null;
093: }
094:
095: int collectionLength = collection.size();
096: for (int i = 0; i < collectionLength; i++) {
097: DataRule lRule = (DataRule) collection.get(i);
098: if (lRule.matches(aObject)) {
099: if (Debug.isMessageEnabled()) {
100: //BugNo:4899057
101: Object[] param0 = { lRule.toXML() };
102: Object[] param1 = { aObject.toXML() };
103: /*Debug.message( "\nMatch Happend : " +
104: "\nRule Object:" + lRule.toXML() +
105: "\nContent Object: " + aObject.toXML() );*/
106: Debug.message("PSRW_CSPR_0025");
107: Debug.message("PSRW_CSPR_0026", param0);
108: Debug.message("PSRW_CSPR_0027", param1);
109: }
110:
111: return lRule;
112: }
113: }
114:
115: return null;
116: }//findMatch()
117:
118: public final int size() {
119: return collection.size();
120: }//size()
121:
122: public boolean contains(final Data aObject) {
123: if (findMatch(aObject) == null) {
124: return false;
125: } else {
126: return true;
127: }
128: }//contains()
129:
130: public String toXML() {
131: final StringBuffer sb = new StringBuffer();
132: for (int i = 0; i < collection.size(); i++) {
133: Rule lValue = (Rule) collection.get(i);
134: sb.append(lValue.toXML());
135: }
136:
137: return sb.toString();
138: }//toXML()
139:
140: public static DataRuleCollection create(final Class aClassRef,
141: final Node[] lArray) {
142: DataRuleCollection lCollection = null;
143: DataRule lDataRule;
144: try {
145: Constructor constructor = aClassRef
146: .getConstructor(new Class[] { Node.class });
147:
148: /*
149: Get the collection ID by creating a dummy object
150: */
151: lDataRule = (DataRule) constructor
152: .newInstance(new Object[] { new Node(null) });
153: lCollection = new DataRuleCollection(lDataRule
154: .getCollectionID());
155: for (int i = 0; i < lArray.length; i++) {
156: try {
157: lDataRule = (DataRule) constructor
158: .newInstance(new Object[] { lArray[i] });
159: lCollection.add(lDataRule);
160: } catch (Exception e) {
161: //Debug.error( "Exception in create of DataRuleCollection:", e );
162: Debug.error("PSRW_CSPR_0028", e);
163: continue;
164: }//try/catch
165: }//for loop
166: } catch (Exception me) {
167: me.printStackTrace();
168: }//try/catch
169: return lCollection;
170: }//create()
171:
172: public static void main(String[] args) {
173: AttributeRule[] attributes = com.sun.portal.rewriter.test.util.SampleRuleObjects.defaultHTMLAttributes;
174: DataRuleCollection lCollection = new DataRuleCollection(
175: ATTRIBUTE, attributes);
176: Debug.println("DataRuleCollection : "
177: + lCollection.contains(attributes[0].getData()));
178: Debug.println("DataRuleCollection : "
179: + lCollection.contains(null));
180: }//main()
181: }//class DataRuleCollection
|