001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: FilteredTagProcessor.java 3669 2007-02-26 13:51:23Z gbevin $
007: */
008: package com.uwyn.rife.template;
009:
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.Map;
013:
014: import com.uwyn.rife.template.exceptions.ExpressionException;
015: import com.uwyn.rife.template.exceptions.ExpressionNotBooleanException;
016:
017: public abstract class FilteredTagProcessor {
018: public abstract Object processExpression(Template template,
019: Class rootType, String rootName, Object rootValue,
020: String expression, Map<String, Object> context)
021: throws Exception;
022:
023: public abstract String getLanguage();
024:
025: public void processTags(List<String> setValues, Template template,
026: List<String[]> blocks, String id, Class rootType,
027: String rootName, Object rootValue, Map<String, Object> map) {
028: if (blocks.size() != 0) {
029: for (String[] block : blocks) {
030: if (id != null && !id.equals(block[1])) {
031: continue;
032: }
033:
034: // if the value corresponding to the filtered block is already
035: // set, don't process the block any further
036: if (null == id && template.isValueSet(block[1])) {
037: continue;
038: }
039:
040: // prepare the context map and integrate the template
041: // expression vars
042: Map<String, Object> context_map = null;
043: if (null == template.getExpressionVars()) {
044: context_map = map;
045: } else if (null == map) {
046: context_map = template.getExpressionVars();
047: } else {
048: context_map = new HashMap<String, Object>();
049:
050: context_map.putAll(template.getExpressionVars());
051: context_map.putAll(map);
052: }
053:
054: // store the root variable in the context map
055: if (rootName != null) {
056: if (null == context_map) {
057: context_map = new HashMap<String, Object>();
058: }
059: context_map.put(rootName, rootValue);
060: }
061:
062: // extract the expression
063: String expression = block[block.length - 1];
064:
065: // process the expression
066: Object result = null;
067: try {
068: result = processExpression(template, rootType,
069: rootName, rootValue, expression,
070: context_map);
071: } catch (Exception e) {
072: throw new ExpressionException(getLanguage(),
073: template.getClass().getName(),
074: block[block.length - 1], e);
075: }
076:
077: // if the result is null, just return
078: if (null == result) {
079: continue;
080: }
081:
082: // ensure that the result is boolean
083: if (!(result instanceof Boolean)) {
084: throw new ExpressionNotBooleanException(
085: getLanguage(), template.getClass()
086: .getName(),
087: block[block.length - 1], result.getClass());
088: }
089:
090: // automatically set the block if the resulting boolean value is true
091: if (((Boolean) result).booleanValue()) {
092: template.setBlock(block[1], block[0]);
093: if (setValues != null) {
094: setValues.add(block[1]);
095: }
096: }
097: }
098: }
099: }
100: }
|