Package de.odysseus.calyxo.forms.impl

Source Code of de.odysseus.calyxo.forms.impl.AssertVariableResolver

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.ELException;

import de.odysseus.calyxo.base.misc.CalyxoVariableResolver;
import de.odysseus.calyxo.base.util.MapFacade;
import de.odysseus.calyxo.forms.FormInput;
import de.odysseus.calyxo.forms.FormInputResult;
import de.odysseus.calyxo.forms.FormInputValues;
import de.odysseus.calyxo.forms.FormResult;
import de.odysseus.calyxo.forms.conf.FieldConfig;


/**
* @author Christoph Beck
* @author Oliver Stuhr
*/
public class AssertVariableResolver extends CalyxoVariableResolver {

  private FormInputValues inputValues;
  private FormResult formResult;
  private Map input, property;

  private HashSet involvedInputs = new HashSet();
  private boolean invalidPropertyReferenced = false;

  /**
   * Creates a new VariableResolver used to evaluate assert expressions.
   */
  AssertVariableResolver(
    HttpServletRequest request,
    FormInputValues inputValues,
    FormResult formResult
  ) {
    super(request);

    this.inputValues = inputValues;
    this.formResult = formResult;
  }

  /* (non-Javadoc)
   * @see org.apache.taglibs.standard.lang.jstl.VariableResolver#resolveVariable(java.lang.String, java.lang.Object)
   */
  public Object resolveVariable(String id) throws ELException {
    if ("input".equals(id))
      return getInput();
    if ("property".equals(id))
      return getProperty();
    return super.resolveVariable(id);
  }

  void reset() {
    invalidPropertyReferenced = false;
    involvedInputs.clear();
  }

  boolean isInvalidPropertyReferenced() {
    return invalidPropertyReferenced;
  }

  public Iterator getInvolvedFormInputs() {
    if (involvedInputs == null) {
      return Collections.EMPTY_SET.iterator();
    }
    return involvedInputs.iterator();
  }

  private Map getInput() {
    if (input == null) {
      input = new MapFacade() {
        public Object get(Object key) {
          String name = (String)key;
          FormInputResult result =
            formResult.getFormInputResult(name);
          if (result == null) {
            throw new NoSuchElementException("Bad input name: " + name);
          }
          FormInput input = result.getFormInput();
          involvedInputs.add(input);
          if (input.isArray()) {
            return inputValues._getInputs(name);
          } else {
            return inputValues._getInput(name);
          }
        }
      };
    }
    return input;
  }

  private Map getProperty() {
    if (property == null) {
      property = new MapFacade() {
        public Object get(Object key) {
          String property = (String)key;
          FieldConfig field = formResult.getFormConfig().findFieldConfig(property);
          if (field == null) {
            throw new NoSuchElementException("Bad property name: " + key);
          }
          FormInputResult result =
            formResult.getFormInputResult(field.getName());
          FormInput input = result.getFormInput();
          involvedInputs.add(input);
          if (!result.isValid()) {
            invalidPropertyReferenced = true;
            return null;
          }
          if (!result.getProperty().equals(property)) {
            return null;
          }
          return result.getValue();
        }
      };
    }
    return property;
  }
}
TOP

Related Classes of de.odysseus.calyxo.forms.impl.AssertVariableResolver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.