PathNavigator.java :  » J2EE » hgcommons » biz » hammurapi » config » Java Open Source

Java Open Source » J2EE » hgcommons 
hgcommons » biz » hammurapi » config » PathNavigator.java
/*
 * hgcommons 7
 * Hammurapi Group Common Library 
 * Copyright (C) 2003  Hammurapi Group
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
 * e-Mail: support@hammurapi.biz
 */
package biz.hammurapi.config;

import biz.hammurapi.util.Attributable;

/**
 * Helper class to navigate context trees. Follows unix filesystem naming convention: / is a path separator,
 * . is current element, .. is parent element, absolute path starts with /
 * @author Pavel Vlasov
 * @revision $Revision$
 */
public abstract class PathNavigator implements Context {
  
  protected Object master;

  public PathNavigator(Object master) {
    this.master=master;
  }

  protected abstract Object getParent();
  protected abstract Object getChild(String name);
  
  public Object get(String name) {
    try {
      Object ret=lookup(name);
      if (ret instanceof Service) {
        ((Service) ret).start();
      }
      
      if (ret instanceof Wrapper) {
        ret = ((Wrapper) ret).getMaster();
      }
      
      if (ret instanceof Service) {
        ((Service) ret).start();
      }
      
      return ret;
    } catch (ConfigurationException e) {
      throw new RuntimeConfigurationException(e);
    }
  }
  
  private Object lookup(String name) {
    if (name.startsWith("/")) {
      if (getParent() instanceof Context) {
        return ((Context) getParent()).get(name);
      } 
      return name.equals("/") ? master : get(name.substring(1));
    } else if ("..".equals(name)) {
      return getParent();      
    } else if (name.startsWith("../")) {
      return (getParent() instanceof Context) ? ((Context) getParent()).get(name.substring(3)) : null;
    } 
    
    int idx=name.indexOf('/');
    if (idx==-1) {
      if (name.startsWith("@") && master instanceof Attributable) {
        return ((Attributable) master).getAttribute(name.substring(1));
      } 
      
      return ".".equals(name) ? master : getChild(name);
    }
    
    String nameElement = name.substring(0, idx);
    Object child = ".".equals(nameElement) ? master : getChild(nameElement);
    if (child instanceof Context) {
      return ((Context) child).get(name.substring(idx+1));
    }
    
    return null;    
  }
  
  /**
   * @param master Master context.
   * @param parent Parent object.
   * @return Instance of PathNavigator.
   */
  public static Context newInstance(Context master, final Object parent) {
    return new PathNavigator(master) {

      protected Object getParent() {
        return parent;
      }

      protected Object getChild(String name) {
        return ((Context) master).get(name);
      }
      
    };
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.