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.html.HTMLRules;
008: import com.sun.portal.rewriter.rom.js.JSRules;
009: import com.sun.portal.rewriter.rom.xml.XMLRules;
010: import com.sun.portal.rewriter.util.Debug;
011: import com.sun.portal.rewriter.util.StringHelper;
012: import com.sun.portal.rewriter.util.xml.Node;
013:
014: /**
015: * Java representation of RuleSet Tag
016: *
017: * @version 1.0 12/15/2001
018: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
019: */
020: public final class RuleSet extends RecursiveRuleCollection {
021: //XPath Definations
022: private static final String XPATH_SPERATOR = "\\";
023: private static final String XPATH_ROOT = XPATH_SPERATOR
024: + XPATH_SPERATOR;
025: private static final String RULESET_XPATH = XPATH_ROOT + RULESET;
026: private static final String HTMLRULES_XPATH = RULESET_XPATH
027: + XPATH_SPERATOR + HTMLRULES;
028: private static final String JSRULES_XPATH = RULESET_XPATH
029: + XPATH_SPERATOR + JSRULES;
030: private static final String XMLRULES_XPATH = RULESET_XPATH
031: + XPATH_SPERATOR + XMLRULES;
032:
033: private String cacheRuleSetXML;
034:
035: private final String id;
036: private final String parentRuleSetID;
037: private final RuleSet parentRuleSet;
038:
039: public RuleSet(final String aID, final RuleCollection[] aCollection)
040: throws InvalidXMLException {
041: this (aID, null, aCollection);
042: }//constructor
043:
044: public RuleSet(final String aID, final String aChildRuleSetID,
045: final RuleCollection[] aCollection)
046: throws InvalidXMLException {
047: super (RULESET, aCollection);
048:
049: id = checkID(aID);
050: parentRuleSetID = StringHelper.normalize(aChildRuleSetID,
051: Rule.NONE);
052:
053: //in data migration getDefault() would be null as component will not be intialized
054: if (RuleSetManager.getDefault() != null) {
055: parentRuleSet = RuleSetManager.getDefault().fetchRuleSet(
056: parentRuleSetID);
057: } else {
058: parentRuleSet = null;
059: }
060: }//constructor
061:
062: protected RuleSet(final Node aNode) throws InvalidXMLException {
063: this (aNode.getAttributeValue(ID), aNode
064: .getAttributeValue(EXTENDS), null);
065:
066: addRuleCollection(new HTMLRules(aNode
067: .selectNode(HTMLRULES_XPATH)));
068: addRuleCollection(new JSRules(aNode.selectNode(JSRULES_XPATH)));
069: addRuleCollection(new XMLRules(aNode.selectNode(XMLRULES_XPATH)));
070: }//constructor
071:
072: /**
073: * RuleSet ID should be in small case only. This needs to enforced as the
074: * sub config key is mapped with the ID. IDSAME does not support the case
075: * sensitive config key.
076: */
077: private String checkID(final String aID) throws InvalidXMLException {
078: String lResult = StringHelper.normalize(aID);
079: if (!(lResult.toLowerCase().equals(lResult))) {
080: throw new InvalidXMLException("ID of Tag "
081: + getCollectionID() + " Should be in Small Case",
082: null, aID, InvalidXMLException.INVALID_ID);
083: }
084:
085: return lResult;
086: }//setID()
087:
088: public String getID() {
089: return id;
090: }//getID()
091:
092: public String getParentRuleSetID() {
093: return parentRuleSetID;
094: }//getParentRuleSetID()
095:
096: public RuleSet getParentRuleSet() {
097: return parentRuleSet;
098: }//getParentRuleSet()
099:
100: private HTMLRules htmlRules;
101: private JSRules jsRules;
102: private XMLRules xmlRules;
103:
104: public HTMLRules getHTMLRules() {
105: if (htmlRules == null) {
106: htmlRules = (HTMLRules) getRuleCollection(HTMLRULES);
107: }
108:
109: return htmlRules;
110: }//getHTMLRules()
111:
112: public JSRules getJSRules() {
113: if (jsRules == null) {
114: jsRules = (JSRules) getRuleCollection(JSRULES);
115: }
116:
117: return jsRules;
118: }//getJSRules()
119:
120: public XMLRules getXMLRules() {
121: if (xmlRules == null) {
122: xmlRules = (XMLRules) getRuleCollection(XMLRULES);
123: }
124:
125: return xmlRules;
126: }//getXMLRules()
127:
128: public DataRuleCollection getDataRuleCollection(
129: final String aFirstTag, final String aSecondTag) {
130: final RecursiveRuleCollection lRecursiveRuleCollection = (RecursiveRuleCollection) getRuleCollection(aFirstTag);
131: return (DataRuleCollection) (lRecursiveRuleCollection
132: .getRuleCollection(aSecondTag));
133: }//getDataRuleCollection()
134:
135: public final StringBuffer writeCustomAttributes(StringBuffer aBuffer) {
136: aBuffer.insert(0, XML_HEADER);
137:
138: aBuffer.append(" ").append(ID).append("=\"").append(getID())
139: .append("\"");
140:
141: if (!parentRuleSetID.equals(Rule.NONE)) {
142: aBuffer.append(" ").append(EXTENDS).append("=\"").append(
143: parentRuleSetID).append("\"");
144: }
145:
146: return aBuffer;
147: }//writeCustomAttributes()
148:
149: public String toString() {
150: if (cacheRuleSetXML == null) {
151: cacheRuleSetXML = toXML();
152: }
153:
154: return cacheRuleSetXML;
155: }//toString()
156:
157: public static void main(String[] args) {
158: String xmlString = com.sun.portal.rewriter.test.util.SampleRuleObjects.defaultRuleSet
159: .toXML();
160: Debug.println(xmlString);
161: }//main()
162: }//class RuleSet
|