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.framework.basic;
011:
012: import java.util.*;
013:
014: import org.mmbase.framework.FrameworkException;
015: import org.mmbase.util.functions.Parameters;
016: import org.mmbase.util.functions.Parameter;
017:
018: import org.mmbase.util.logging.Logger;
019: import org.mmbase.util.logging.Logging;
020:
021: /**
022: * Keeps track of several UrlConverters and chains them one after another.
023: * If the outcome of an UrlConverter is not <code>null</code> its result is returned. The
024: * question remains whether we want UrlConverters to be realy chained so that the
025: * outcome of a converter can be added to the outcome of its preceder.
026: *
027: * @author André van Toly
028: * @version $Id: ChainedUrlConverter.java,v 1.6 2008/02/22 14:05:57 michiel Exp $
029: * @since MMBase-1.9
030: */
031: public class ChainedUrlConverter implements UrlConverter {
032:
033: private static final Logger log = Logging
034: .getLoggerInstance(ChainedUrlConverter.class);
035:
036: /**
037: * List containing the UrlConverters found in the framework configuration.
038: */
039: private final List<UrlConverter> uclist = new ArrayList<UrlConverter>();
040: private final List<Parameter> parameterDefinition = new ArrayList<Parameter>();
041:
042: /**
043: * Adds the UrlConverters to the list.
044: */
045: public void add(UrlConverter u) {
046: uclist.add(u);
047: for (Parameter p : u.getParameterDefinition()) {
048: if (!parameterDefinition.contains(p)) {
049: parameterDefinition.add(p);
050: }
051: }
052: }
053:
054: public boolean contains(UrlConverter u) {
055: return uclist.contains(u);
056: }
057:
058: public Parameter[] getParameterDefinition() {
059: return parameterDefinition.toArray(Parameter.EMPTY);
060: }
061:
062: /**
063: * The URL to be printed in a page
064: */
065: public String getUrl(String path, Map<String, Object> params,
066: Parameters frameworkParameters, boolean escapeAmps)
067: throws FrameworkException {
068:
069: String p = path;
070: for (UrlConverter uc : uclist) {
071: String b = uc.getUrl(p, params, frameworkParameters,
072: escapeAmps);
073: if (b != null) {
074: return b;
075: }
076: //p = b;
077: }
078: //log.debug("ChainedUrlConverter has: " + b);
079:
080: return p; // this seems incorrect (what if nothing is resolved by one of the uc's? then params etc. are lost)
081: }
082:
083: public String getProcessUrl(String path,
084: Map<String, Object> params, Parameters frameworkParameters,
085: boolean escapeAmps) throws FrameworkException {
086:
087: String p = path;
088: for (UrlConverter uc : uclist) {
089: String b = uc.getProcessUrl(p, params, frameworkParameters,
090: escapeAmps);
091: if (b != null) {
092: return b;
093: }
094: //p = b;
095: }
096: //log.debug("ChainedUrlConverter has: " + b);
097:
098: return p; // this seems incorrect (what if nothing is resolved by one of the uc's? then params etc. are lost)
099: }
100:
101: /**
102: * The 'technical' url
103: */
104: public String getInternalUrl(String path,
105: Map<String, Object> params, Parameters frameworkParameters)
106: throws FrameworkException {
107: String p = new String(path);
108: for (UrlConverter uc : uclist) {
109: String b = uc.getInternalUrl(p.toString(), params,
110: frameworkParameters);
111: log.debug("ChainedUrlConverter has: " + b);
112: if (b != null)
113: return b;
114: //p = b;
115: }
116: return p; // this seems incorrect (what if nothing is resolved by one of the uc's? then params etc. are lost)
117: }
118:
119: public String toString() {
120: return "ChainedUrlConverter" + uclist;
121: }
122:
123: }
|