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;
018:
019: import java.util.Iterator;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import org.apache.jetspeed.rewriter.rules.Attribute;
025: import org.apache.jetspeed.rewriter.rules.Rule;
026: import org.apache.jetspeed.rewriter.rules.Ruleset;
027: import org.apache.jetspeed.rewriter.rules.Tag;
028:
029: /**
030: * RuleBasedRewriter
031: *
032: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
033: * @version $Id: RulesetRewriterImpl.java 517121 2007-03-12 07:45:49Z ate $
034: */
035: public class RulesetRewriterImpl extends BasicRewriter implements
036: RulesetRewriter {
037: protected final static Log log = LogFactory
038: .getLog(RulesetRewriterImpl.class);
039:
040: private Ruleset ruleset = null;
041: private boolean removeComments = false;
042:
043: public boolean shouldStripTag(String tagid) {
044: if (null == ruleset) {
045: return false;
046: }
047:
048: Tag tag = ruleset.getTag(tagid.toUpperCase());
049: if (null == tag) {
050: return false;
051: }
052: return tag.getStrip();
053: }
054:
055: /* (non-Javadoc)
056: * @see org.apache.jetspeed.cps.rewriter.Rewriter#shouldRemoveTag(java.lang.String)
057: */
058: public boolean shouldRemoveTag(String tagid) {
059: if (null == ruleset) {
060: return false;
061: }
062:
063: Tag tag = ruleset.getTag(tagid.toUpperCase());
064: if (null == tag) {
065: return false;
066: }
067: return tag.getRemove();
068: }
069:
070: /* (non-Javadoc)
071: * @see org.apache.jetspeed.cps.rewriter.RulesetRewriter#setRuleset(org.apache.jetspeed.cps.rewriter.rules.Ruleset)
072: */
073: public void setRuleset(Ruleset ruleset) {
074: this .ruleset = ruleset;
075: }
076:
077: /* (non-Javadoc)
078: * @see org.apache.jetspeed.cps.rewriter.RulesetRewriter#getRuleset()
079: */
080: public Ruleset getRuleset() {
081: return this .ruleset;
082: }
083:
084: /* (non-Javadoc)
085: * @see org.apache.jetspeed.cps.rewriter.Rewriter#shouldRemoveComments()
086: */
087: public boolean shouldRemoveComments() {
088: if (null == ruleset) {
089: return false;
090: }
091:
092: return ruleset.getRemoveComments();
093: }
094:
095: /* (non-Javadoc)
096: * @see org.apache.jetspeed.syndication.services.crawler.rewriter.Rewriter#convertTagEvent(java.lang.String, org.xml.sax.Attributes)
097: */
098: public void enterConvertTagEvent(String tagid,
099: MutableAttributes attributes) {
100: if (null == ruleset) {
101: return;
102: }
103:
104: Tag tag = ruleset.getTag(tagid.toUpperCase());
105: if (null == tag) {
106: return;
107: }
108:
109: Iterator attribRules = tag.getAttributes().iterator();
110: while (attribRules.hasNext()) {
111: Attribute attribute = (Attribute) attribRules.next();
112: String name = attribute.getId();
113: String value = attributes.getValue(name);
114:
115: if (value != null) // && name.equalsIgnoreCase(attribute.getId()))
116: {
117: Rule rule = attribute.getRule();
118: if (null == rule) {
119: continue;
120: }
121:
122: if (!rule.shouldRewrite(value)) {
123: continue;
124: }
125:
126: String rewritten = rewriteUrl(value, tag.getId(), name,
127: attributes);
128: if (null != rewritten) // return null indicates "don't rewrite"
129: {
130: if (rule.getSuffix() != null) {
131: rewritten = rewritten.concat(rule.getSuffix());
132: }
133:
134: attributes.addAttribute(name, rewritten);
135:
136: if (rule.getPopup()) {
137: attributes.addAttribute("TARGET", "_BLANK");
138: }
139: }
140: }
141: }
142: }
143:
144: /**
145: * rewriteURL
146: *
147: * @param url
148: * @param tag
149: * @param attribute
150: * @param otherAttributes
151: * @return the modified url which is a portlet action
152: *
153: * Rewrites all urls HREFS with a portlet action
154: */
155: public String rewriteUrl(String url, String tag, String attribute,
156: MutableAttributes otherAttributes) {
157: return getBaseRelativeUrl(url);
158: }
159: }
|