001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.rewriter.rules.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.jetspeed.rewriter.rules.Attribute;
026: import org.apache.jetspeed.rewriter.rules.Rule;
027: import org.apache.jetspeed.rewriter.rules.Ruleset;
028: import org.apache.jetspeed.rewriter.rules.Tag;
029:
030: /**
031: * RulesetImpl
032: *
033: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
034: * @version $Id: RulesetImpl.java 516448 2007-03-09 16:25:47Z ate $
035: */
036: /**
037: * Ruleset
038: *
039: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
040: * @version $Id: RulesetImpl.java 516448 2007-03-09 16:25:47Z ate $
041: */
042: public class RulesetImpl extends IdentifiedImpl implements Ruleset {
043: private Collection rules = new ArrayList();
044: private Collection tags = new ArrayList();
045: private Map ruleMap = new HashMap();
046: private Map tagMap = new HashMap();
047: private boolean removeComments = false;
048:
049: /* (non-Javadoc)
050: * @see org.apache.jetspeed.cps.rewriter.rules.Ruleset#getTag(java.lang.String)
051: */
052: public Tag getTag(String tagId) {
053: return (Tag) tagMap.get(tagId);
054: }
055:
056: /* (non-Javadoc)
057: * @see org.apache.jetspeed.cps.rewriter.rules.Ruleset#getRule(java.lang.String)
058: */
059: public Rule getRule(String ruleId) {
060: return (Rule) ruleMap.get(ruleId);
061: }
062:
063: public String toString() {
064: StringBuffer buffer = new StringBuffer("Ruleset:" + id);
065: if (rules.size() == 0) {
066: buffer.append(", no rules defined, ");
067: } else {
068: buffer.append(", rules: ");
069: Iterator it = rules.iterator();
070: while (it.hasNext()) {
071: RuleImpl rule = (RuleImpl) it.next();
072: buffer.append(rule.toString());
073: buffer.append(", ");
074: }
075: }
076: if (tags.size() == 0) {
077: buffer.append(" no tags defined.");
078: } else {
079: buffer.append("tags: ");
080: Iterator it = tags.iterator();
081: while (it.hasNext()) {
082: TagImpl tag = (TagImpl) it.next();
083: buffer.append(tag.toString());
084: buffer.append(", ");
085: }
086: }
087: return buffer.toString();
088: }
089:
090: /* (non-Javadoc)
091: * @see org.apache.jetspeed.cps.rewriter.rules.Ruleset#sync()
092: */
093: public void sync() {
094: ruleMap.clear();
095: Iterator it = rules.iterator();
096: while (it.hasNext()) {
097: Rule rule = (Rule) it.next();
098: ruleMap.put(rule.getId(), rule);
099: }
100:
101: tagMap.clear();
102: it = tags.iterator();
103: while (it.hasNext()) {
104: Tag tag = (Tag) it.next();
105: tagMap.put(tag.getId(), tag);
106: Iterator attributes = tag.getAttributes().iterator();
107: while (attributes.hasNext()) {
108: Attribute attribute = (Attribute) attributes.next();
109: if (attribute instanceof AttributeImpl) {
110: String ruleId = ((AttributeImpl) attribute)
111: .getRuleId();
112: Rule rule = (Rule) ruleMap.get(ruleId);
113: if (rule != null) {
114: attribute.setRule(rule);
115: }
116: }
117: }
118: }
119: }
120:
121: /**
122: * Castor setter
123: *
124: * @param rules
125: */
126: public void setRules(Collection rules) {
127: this .rules = rules;
128: }
129:
130: /* (non-Javadoc)
131: * @see org.apache.jetspeed.cps.rewriter.rules.Ruleset#getRules()
132: */
133: public Collection getRules() {
134: return this .rules;
135: }
136:
137: /**
138: * Castor setter
139: *
140: * @param rules
141: */
142: public void setTags(Collection tags) {
143: this .tags = tags;
144: }
145:
146: /* (non-Javadoc)
147: * @see org.apache.jetspeed.cps.rewriter.rules.Ruleset#getTags()
148: */
149: public Collection getTags() {
150: return this .tags;
151: }
152:
153: /* (non-Javadoc)
154: * @see org.apache.jetspeed.cps.rewriter.rules.Ruleset#getRemoveComments()
155: */
156: public boolean getRemoveComments() {
157: return removeComments;
158: }
159:
160: /* (non-Javadoc)
161: * @see org.apache.jetspeed.cps.rewriter.rules.Ruleset#setRemoveComments(boolean)
162: */
163: public void setRemoveComments(boolean b) {
164: removeComments = b;
165: }
166:
167: }
|