Conventions.java :  » Web-Framework » jucas » org » jucas » Java Open Source

Java Open Source » Web Framework » jucas 
jucas » org » jucas » Conventions.java
/*
 * License
 *
 *
 * Copyright (c) 2003 Essl Christian.  All rights 
 * reserved.
 * 
 * This Licence is based on the Apache Software Licence Version 1.1.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:  
 *       "This product includes software developed by Christian Essl 
 *        and others for project Jucas (http://www.jucas.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Jucas" and "Christian Essl" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written 
 *    permission, please contact essl_christian@jucas.org.
 *
 * 5. Products derived from this software may not be called "Jucas"
 *    or "Christian Essl", nor may "Jucas" or "Christian Essl"
 *    appear in their name, without prior written permission
 *    of Christian Essl (jucas@esslchristian.de).
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL CHRISTIAN ESSL OR
 * OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * 
 */
package org.jucas;

import java.net.URI;
import java.net.URISyntaxException;

/**
 *This class defines certain conventions used througout the Framework It mainly
 *deals with IJucasBean names (which are URIs).
 * <br><br>
 *  The bean name for each IJucasBean is a java.net.URI (scheme:scheme-
 * specific-part[#fragment]). The name must be absolute (a scheme must be
 * specified). The scheme and the scheme-specfic-part are the PageBean-name
 * which is used to lookup the PageBean in BeanManager. The fragment is an org.
 * apache.commons.beanutils path which is than used to look up the named bean
 * from the PageBean. So a pageBean has always an absolute URI without fragment.
 * <br><br>
 * 
 * A BeanName has the followign form and is an (absolute) instance of java.net.
 * URI:<br>
  <pre>
IJucasBeanName := scheme:scheme-specific-part[#fragment] //see java.net.URI
  scheme and scheme-specifig-part are required
  but can be of any value which is allowed by java.net.URI.
  The values for scheme and scheme-specific-part are defined
  by the various {@link IPageBeanFactory} classes
fragement := must be an org.apache.commons.beanuitls.PropertyUtils combined path
  if the fragement is not given the BeanName is a PageBeanName and
  the IJucasBean with the given name is said to be a PageBean (PageBean is no
  class)
  </pre>

 * <br><br>
 * 
 * The BeanName (the URI) is important to the framework. It not only names the
 * bean for lookup, but is also used to store different attibutes and values in
 * the HttpServleRequest and HttpSession. 
 *
 *   @author   chris
 */
final public class Conventions
{
  
  public final static String STORAGE_INSTANCE_KEY_PREFIX = "org.jucas.BeanNameConventions.STORAGE_INSTANCE";
  public final static String STORAGE_STATIC_KEY_PREFIX = "org.jucas.BeanNameConventions.STORAGE_STATIC";
  public final static String BEAN_KEY_PREFIX = "org.jucas.BeanNameConventions.BEAN";
  /**
   * Constructor for BeanConventions.
   */
  private Conventions()
  {
    super();
  }
  
  /**
   * returns true if the uri not null and isAbsolute (that is 
   * it has a scheme: and a scheme-specific-part see java.net.URI) 
   * @param uri
   * @return boolean
   */
  public static boolean isValidName(URI uri){
    return uri != null 
        && uri.isAbsolute();
        
  }
  
  /**
   * checks if it is a PageBean name. This is if the URI isAbsolute and has no
   * Fragement
   * @param uri
   * @return boolean
   */
  public static boolean isPageBeanName(URI uri){
    boolean ret = isValidName(uri) && uri.getFragment() == null;
    return ret;
  }
  
  /**
   * this append the path to the fragment with a dot prefix. if no fragment a
   * new one is created and returns a copy of the original uri.
   * @param parent a valid name
   * @param path must be provided
   * @return URI
   */
  public static URI getChildName (URI parent, String path){
    if(!isValidName(parent))
      throw new IllegalArgumentException("Must provide a valid parent name");
    if(path == null)
      throw new IllegalArgumentException("Must provide a path");
    String fragment = parent.getFragment();
    if(fragment == null)
      fragment = path;
    else
      fragment = fragment +"."+path;
    
    try
    {
      URI ret = new URI(parent.getScheme(),parent.getAuthority(),parent.getPath(),parent.getQuery(),fragment);
      return ret;
    } catch (URISyntaxException e)
    {
      throw new IllegalArgumentException(e.toString());
    }
  }
  
  /**
   * returns the fragmenet or null if none is given.
   * @param uri
   * @return String
   */
  public static String getPath(URI uri){
    if(!isValidName(uri))
      throw new IllegalArgumentException("Must provide a valid name");
    return uri.getFragment();
  }
  
  /**
   * if the given name is a PageBeanName it is returned otherwise it is fully
   * copied but without the fragement
   * @param name
   * @return URI
   */
  public static URI getPageBeanName(URI name){
    if(!isValidName(name))
      throw new IllegalArgumentException("Must provide a valid name");
    if(name.getFragment() == null)
      return name;
    try
    {
      URI ret = new URI(name.getScheme(),name.getAuthority(),name.getPath(),name.getQuery(),null);
      return ret;
    } catch (URISyntaxException e)
    {
      throw new IllegalArgumentException(e.toString());
    }
  }
  
  /**
   * checks if the first BeanName and the second BeanName belong to the same
   * PageBean. This is if the schemes are equal and the scheme-specific-parts
   * are qual. The fragment is ignored.
   * @param firstName
   * @param secondName
   * @return boolean
   */
  public static boolean isSameContainingPageBean(URI firstName,URI secondName){
    if(!isValidName(firstName))
      throw new IllegalArgumentException("Must provide a valid firstName");
    if(!isValidName(secondName))
      throw new IllegalArgumentException("Must provide a valid secondName");
    return firstName.getScheme().equals(secondName.getScheme())
        && firstName.getSchemeSpecificPart().equals(secondName.getSchemeSpecificPart());
  }
  
//  public static Object resolvePathFromContainer(IContainer from, String path){
//    if(from == null)
//      return from;
//    int lastCut = 0;
//    int cut = path.indexOf('.');
//    if(cut == -1)
//      return from.getJucasBean(path);
//    
//    IJucasBean toRet = from.getJucasBean(path.substring(lastCut,cut));
//    
//    while(true){
//      if(toRet == null)
//        return null;
//      
//      lastCut = cut+1;
//      cut = path.indexOf('.',lastCut);
//      if(cut == -1){
//        return toRet; //the normal exit      
//      }
//      //we have a path element still but no container 
//      //return null
//      if(!(toRet instanceof IContainer))
//        return null;
//      //get the next element
//      toRet = ((IContainer)toRet).getJucasBean(path.substring(lastCut,cut));
//    }
//    
//  }
  
  //////////////////////////////
  //keys for storing in Session
  static String getStorageInstanceKey(URI uri){
    if(!isValidName(uri))
      throw new IllegalArgumentException("Must provide a valid name");
    String key = STORAGE_INSTANCE_KEY_PREFIX + uri.toString();
    return key;  
  }
  
  static String getStorageStaticKey(String type){
    if(type == null)
      throw new IllegalArgumentException("Must provide a valid type");
    String key = STORAGE_STATIC_KEY_PREFIX + type;
    return key;      
  }
  
  static String getJaulBeanKey(URI uri){
    if(!isValidName(uri))
      throw new IllegalArgumentException("Must provide a valid name");
    String key = BEAN_KEY_PREFIX + uri.toString();
    return key;  
  }
  
}
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.