BeanContext.java

  1. package fr.sii.ogham.core.template.context;

  2. import java.util.Map;

  3. import fr.sii.ogham.core.exception.template.BeanContextException;
  4. import fr.sii.ogham.core.exception.template.ContextException;
  5. import fr.sii.ogham.core.exception.util.BeanWrapperException;
  6. import fr.sii.ogham.core.util.BeanUtils;
  7. import fr.sii.ogham.core.util.EqualsBuilder;
  8. import fr.sii.ogham.core.util.HashCodeBuilder;
  9. import fr.sii.ogham.core.util.bean.MapBeanReadWrapper;

  10. /**
  11.  * Template context that provides variable values using a Java object. Each
  12.  * property name of the object acts as a potential template variable name. Each
  13.  * property value acts as a potential template variable value.
  14.  *
  15.  * The variables can contain dot character (.) to indicate nested properties. A
  16.  * nested property is a property on a child object. For example, if the provided
  17.  * Java object looks like:
  18.  *
  19.  * <pre>
  20.  * public class NestedBean {
  21.  *  private SimpleBean nested;
  22.  *
  23.  *  public NestedBean(SimpleBean nested) {
  24.  *      super();
  25.  *      this.nested = nested;
  26.  *  }
  27.  *
  28.  *  public SimpleBean getNested() {
  29.  *      return nested;
  30.  *  }
  31.  *
  32.  *  public static class SimpleBean {
  33.  *      private String value;
  34.  *
  35.  *      public SimpleBean(String value) {
  36.  *          super();
  37.  *          this.value = value;
  38.  *      }
  39.  *
  40.  *      public String getValue() {
  41.  *          return value;
  42.  *      }
  43.  *
  44.  *  }
  45.  * }
  46.  * </pre>
  47.  *
  48.  * Then the value of the nested object is accessible through the template
  49.  * variable name "nested.value".
  50.  *
  51.  * @author AurĂ©lien Baudet
  52.  * @see BeanUtils More information about bean conversion
  53.  */
  54. public class BeanContext implements Context {
  55.     /**
  56.      * A Java object that is the source for the variable substitutions
  57.      */
  58.     private Object bean;

  59.     /**
  60.      * Register the bean to use for variable substitutions.
  61.      *
  62.      * @param bean
  63.      *            the bean to use as source for the variable substitutions
  64.      */
  65.     public BeanContext(Object bean) {
  66.         super();
  67.         this.bean = bean;
  68.     }

  69.     @Override
  70.     public Map<String, Object> getVariables() throws ContextException {
  71.         try {
  72.             return new MapBeanReadWrapper(bean);
  73.         } catch (BeanWrapperException e) {
  74.             throw new BeanContextException("Failed to generate context from bean", bean, e);
  75.         }
  76.     }

  77.     @Override
  78.     public String toString() {
  79.         return bean.toString();
  80.     }
  81.    
  82.     @Override
  83.     public int hashCode() {
  84.         return new HashCodeBuilder().append(bean).hashCode();
  85.     }

  86.     @Override
  87.     public boolean equals(Object obj) {
  88.         return new EqualsBuilder(this, obj).appendFields("bean").isEqual();
  89.     }
  90. }