001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.forms.impl;
017:
018: import java.util.Collections;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.NoSuchElementException;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.jsp.el.ELException;
026:
027: import de.odysseus.calyxo.base.misc.CalyxoVariableResolver;
028: import de.odysseus.calyxo.base.util.MapFacade;
029: import de.odysseus.calyxo.forms.FormInput;
030: import de.odysseus.calyxo.forms.FormInputResult;
031: import de.odysseus.calyxo.forms.FormInputValues;
032: import de.odysseus.calyxo.forms.FormResult;
033: import de.odysseus.calyxo.forms.conf.FieldConfig;
034:
035: /**
036: * @author Christoph Beck
037: * @author Oliver Stuhr
038: */
039: public class AssertVariableResolver extends CalyxoVariableResolver {
040:
041: private FormInputValues inputValues;
042: private FormResult formResult;
043: private Map input, property;
044:
045: private HashSet involvedInputs = new HashSet();
046: private boolean invalidPropertyReferenced = false;
047:
048: /**
049: * Creates a new VariableResolver used to evaluate assert expressions.
050: */
051: AssertVariableResolver(HttpServletRequest request,
052: FormInputValues inputValues, FormResult formResult) {
053: super (request);
054:
055: this .inputValues = inputValues;
056: this .formResult = formResult;
057: }
058:
059: /* (non-Javadoc)
060: * @see org.apache.taglibs.standard.lang.jstl.VariableResolver#resolveVariable(java.lang.String, java.lang.Object)
061: */
062: public Object resolveVariable(String id) throws ELException {
063: if ("input".equals(id))
064: return getInput();
065: if ("property".equals(id))
066: return getProperty();
067: return super .resolveVariable(id);
068: }
069:
070: void reset() {
071: invalidPropertyReferenced = false;
072: involvedInputs.clear();
073: }
074:
075: boolean isInvalidPropertyReferenced() {
076: return invalidPropertyReferenced;
077: }
078:
079: public Iterator getInvolvedFormInputs() {
080: if (involvedInputs == null) {
081: return Collections.EMPTY_SET.iterator();
082: }
083: return involvedInputs.iterator();
084: }
085:
086: private Map getInput() {
087: if (input == null) {
088: input = new MapFacade() {
089: public Object get(Object key) {
090: String name = (String) key;
091: FormInputResult result = formResult
092: .getFormInputResult(name);
093: if (result == null) {
094: throw new NoSuchElementException(
095: "Bad input name: " + name);
096: }
097: FormInput input = result.getFormInput();
098: involvedInputs.add(input);
099: if (input.isArray()) {
100: return inputValues._getInputs(name);
101: } else {
102: return inputValues._getInput(name);
103: }
104: }
105: };
106: }
107: return input;
108: }
109:
110: private Map getProperty() {
111: if (property == null) {
112: property = new MapFacade() {
113: public Object get(Object key) {
114: String property = (String) key;
115: FieldConfig field = formResult.getFormConfig()
116: .findFieldConfig(property);
117: if (field == null) {
118: throw new NoSuchElementException(
119: "Bad property name: " + key);
120: }
121: FormInputResult result = formResult
122: .getFormInputResult(field.getName());
123: FormInput input = result.getFormInput();
124: involvedInputs.add(input);
125: if (!result.isValid()) {
126: invalidPropertyReferenced = true;
127: return null;
128: }
129: if (!result.getProperty().equals(property)) {
130: return null;
131: }
132: return result.getValue();
133: }
134: };
135: }
136: return property;
137: }
138: }
|