Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.sapia.util.text;
 import java.util.Map;

A template context that tries to resolve properties from provided properties (in the form of a Map, and which can hold a reference to an "ancestor" context. This pattern allows to hierarchically organize template contexts in the from of a chain of responsability.

The following code creates a MapContext that resolves values from a Map or from the system properties.

  Map someValues = new HashMap();
  // here init the map with some data...
  MapContext ctx = new MapContext(map, new SystemContext(), false);

Yanick Duchesne 2003-04-08
Copyright &.169; 2002-2003 Sapia Open Source Software. All Rights Reserved.
Read the license.txt file of the jar or visit the license page at the Sapia OSS web site
public class MapContext implements TemplateContextIF {
  private Map               _props;
  private boolean           _ancestorFirst;

Creates an instance of this class.

a Map instance that will be used to resolve values.
ancestor a TemplateContextIF instance to which the lookup can eventually be delegated.
ancestorFirst if true, indicates that this instance should first try to delegate the value lookup to its ancestor prior to trying itself - the opposite applies if false.
  public MapContext(Map propsTemplateContextIF ancestorboolean ancestorFirst) {
               = props;
            = ancestor;
       = ancestorFirst;

  public Object getValue(String aName) {
    Object value;
    if () {
      value = fromAncestor(aName);
      if (value == null) {
        value = .get(aName);
    } else {
      value = .get(aName);
      if (value == null) {
        value = fromAncestor(aName);
    return value;

  public void put(String nameObject value) {
  protected Object fromAncestor(String aName) {
    if ( != null) {
      return .getValue(aName);
    return null;
New to GrepCode? Check out our FAQ X