001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.transformers;
011:
012: import java.util.*;
013: import java.util.regex.Pattern;
014: import java.net.URL;
015:
016: import org.mmbase.util.Entry;
017:
018: import org.mmbase.util.functions.*;
019: import org.mmbase.util.logging.*;
020:
021: /**
022: * An example for parameterized transformers. The Google highlighter transformers have a REQUEST
023: * parameter, which are used to explore the 'Referer' HTTP header and highlight the google search
024: * words.
025: * This can be used in taglib e.g. by <mm:content postprocessor="google" expires="0" />
026: *
027: * Because you need expires=0, you need be reluctant to use this, because this means that your page
028: * cannot be cached in front-proxies. Perhaps it is better to find some client-side solution.
029: *
030: * It produces instances of extensions of {@link RegexpReplacer}
031: *
032: * @author Michiel Meeuwissen
033: * @since MMBase-1.8
034: */
035:
036: public class GoogleHighlighterFactory implements
037: ParameterizedTransformerFactory<CharTransformer> {
038: private static final Logger log = Logging
039: .getLoggerInstance(GoogleHighlighterFactory.class);
040:
041: private static final Parameter<String> FORMAT = new Parameter<String>(
042: "format", String.class, "<span class=\"google\">$1</span>");
043: private static final Parameter<String> HOST = new Parameter<String>(
044: "host", String.class, "google");
045: private static final Parameter[] PARAM = new Parameter[] { FORMAT,
046: HOST, Parameter.REQUEST };
047:
048: public CharTransformer createTransformer(final Parameters parameters) {
049: parameters.checkRequiredParameters();
050: if (log.isDebugEnabled()) {
051: log.debug("Creating transformer, with " + parameters);
052: }
053: URL referrer;
054: String referer = (parameters.get(Parameter.REQUEST))
055: .getHeader("Referer");
056: if (referer == null)
057: return CopyCharTransformer.INSTANCE;
058:
059: try {
060: referrer = new URL(referer);
061: } catch (java.net.MalformedURLException mfue) {
062: log
063: .warn(mfue.getMessage() + " for '" + referer + "'",
064: mfue);
065: return CopyCharTransformer.INSTANCE;
066: }
067: log.debug("Using referrer " + referrer);
068: if (referrer.getHost().indexOf(parameters.get(HOST)) == -1) { // did not refer
069: // from google
070: log.debug("Wrong host, returning COPY");
071: return CopyCharTransformer.INSTANCE;
072: }
073: String queryString = referrer.getQuery();
074: if (queryString == null) {
075: // odd
076: log.debug("No query, returning COPY");
077: return CopyCharTransformer.INSTANCE;
078: }
079: String[] query = queryString.split("&");
080:
081: String s = null;
082: for (String q : query) {
083: if (q.startsWith("q=")) {
084: try {
085: s = java.net.URLDecoder.decode(q.substring(2),
086: "UTF-8");
087: } catch (java.io.UnsupportedEncodingException uee) { // cannot happen
088: s = q.substring(2);
089: }
090: break;
091: }
092: }
093: if (s == null) {
094: // odd
095: log.debug("No search, returning COPY");
096: return CopyCharTransformer.INSTANCE;
097: }
098: final String search = s;
099: log.debug("Using search " + search);
100:
101: RegexpReplacer trans = new RegexpReplacer() {
102: private Collection<Entry<Pattern, String>> patterns = new ArrayList<Entry<Pattern, String>>();
103: {
104: Pattern p = Pattern.compile("("
105: + search.replace('+', '|') + ")");
106: patterns.add(new Entry<Pattern, String>(p, parameters
107: .get(FORMAT)));
108: }
109:
110: public Collection<Entry<Pattern, String>> getPatterns() {
111: return patterns;
112: }
113: };
114: if (log.isDebugEnabled()) {
115: log.debug("Using google transformer " + trans);
116: }
117: return trans;
118:
119: }
120:
121: public Parameters createParameters() {
122: return new Parameters(PARAM);
123: }
124:
125: }
|