Package dk.valtech.octools.jsp

Source Code of dk.valtech.octools.jsp.CmsJstlActionElement

/*

  This library is part of octools
  Copyright (c) 2000-2007 Valtech A/S (http://www.valtech.dk)

  This library 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.1 of the License, or (at your option) any later version.

  This library 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.

 
  Alkacon OpenCms and the OpenCms logo are registered trademarks of
  Alkacon Software GmbH in Germany, the USA and other countries
 
  For further information about Alkacon Software GmbH, please see the
  company website: http://www.alkacon.com

  For further information about OpenCms, please see the
  project website: http://www.opencms.org
  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

*/
package dk.valtech.octools.jsp;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.opencms.file.CmsResource;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.jsp.CmsJspTagProperty;
import org.opencms.main.CmsException;

import dk.valtech.octools.util.collections.MapAdapter;

/**
* This is a JSTL-friendly version of CmsJspActionElement, where the methods:
*  - property(String)
*  - label(String)
*  - link(String)
*  - template(String)
*   - info(String)
*  - user(String)
*
* are accessable via lazy maps, enabling a jstl syntax like ${cms.property['Title']} 
*
* @author Stefan Uldum Grinsted (stefan.grinsted@valtech.dk)
*
*/
public class CmsJstlActionElement extends CmsJspActionElement {

  public CmsJstlActionElement() {
  }

  public CmsJstlActionElement(PageContext context, HttpServletRequest req, HttpServletResponse res) {
    super(context, req, res);
  }
 
  /**
   * This provides Map-style access to the properties of the current template, i.e. the current JSP beeing included.
   * @return
   */
  public Map<String, String> getJspProperty() {
    if (jspPropertyMap == null) {
      jspPropertyMap = new MapAdapter<String, String>() {
        private String resource = getController().getCurrentRequest().getElementUri();

        public String get(Object key) {
          try {
            return getCmsObject().readPropertyObject(resource, (String) key, false).getValue();
          } catch (CmsException e) {
            throw new RuntimeException("Unable to read property '" + key + "' from resource '" + resource + "': "
                + e.getMessage(), e);
          }
        }
      };
    }
    return jspPropertyMap;
  }
 
  /**
   * An ordinary property-getter for a jsp property. See {@link #getJspProperty()}.
   *
   * @param name
   * @return
   */
  public String getJspProperty(String name) {
    return (String) getJspProperty().get(name);
  }

  private Map<String, String> jspPropertyMap;
 
  /**
   * The path part from the {@link CmsResource#getPathPart(String, int)} as a
   * map-style
   *
   * @see #getPathPart()
   */
  private Map<Number, String> pathPart;
 
  /**
   * Makes map-style access to {@link CmsResource#getPathPart(String, int)},
   * so it can be accessed from jsp's like <code>${ controller.pathPart[0] }</code>
   * <p>
   * This is done by instantiating the {@link #pathPart} field with an
   * anonymous {@link MapAdapter} class the overrides the
   * {@link Map#get(Object)} method to use the
   * {@link CmsResource#getPathPart(String, int)}.
   * </p>
   */
  @SuppressWarnings("unchecked")
  public Map<Number, String> getPathPart() {
    if (pathPart == null) {
      pathPart = new MapAdapter() {
        /*
         * (non-Javadoc)
         * @see java.util.Map#get(java.lang.Object)
         */
        public Object get(Object key) {
          return CmsResource.getPathPart(getRequestContext().getUri(), ((Number) key).intValue());
        }
     
      };
    }
    return pathPart;
  }
 
  /**
   * This provides Map-style access to the CmsJspActionElement.property(String, String) where
   * the last argument tells is CmsJspTagProperty.USE_SEARCH_URI to indicate to search for the
   * property in parent folders.
   * This allows you to use the jstl syntax:
   *   ${cms.searchProperty['Title']} to search for the title property from the current page and up
   *
   * @see CmsJspActionElement#property(String, String)
   * @return a map, which deligates to the property() method of CmsJspActionElement
   */
  public Map<String, String> getSearchProperty() {
    return new MapAdapter<String, String>() {
      public String get(Object key) {
        return property((String) key, CmsJspTagProperty.USE_SEARCH_URI);
      }
    };
  }
 
  /*
   *
   * The following is Map-style deligates to CmsJspActionElements
   *  - property(String)
   *  - label(String)
   *  - link(String)
   *  - template(String)
   *   - info(String)
   *  - user(String)
   * 
   */
 
 
  /**
   * This provides Map-style access to the CmsJspActionElement.property(String)
   * This allows you to use the jstl syntax:
   *   ${cms.property['Title']} to get the title of the current requested page
   *
   * @see CmsJspActionElement#property(String)
   * @return a map, which deligates to the property() method of CmsJspActionElement
   */
  public Map<String, String> getProperty() {
    return new MapAdapter<String, String>() {
      public String get(Object key) {
        return property((String) key);
      }
    };
  }

  /**
   * This provides Map-style access to the CmsJspActionElement.label(String)
   * This allows you to use the jstl syntax:
   *   ${cms.label['some_workspace_label']} to get a label from the workspace
   *
   * @see CmsJspActionElement#label(String)
   * @return a map, which deligates to the label() method of CmsJspActionElement
   */
  public Map<String, String> getLabel() {
    return new MapAdapter<String, String>() {
      public String get(Object key) {
        return label((String) key);
      }
    };
  }
 
  /**
   * This provides Map-style access to the CmsJspActionElement.link(String)
   * This allows you to use the jstl syntax:
   *   ${cms.link['/index.html']} to get a link to /index.html
   *
   * @see CmsJspActionElement#link(String)
   * @return a map, which deligates to the link() method of CmsJspActionElement
   */
  public Map<String, String> getLink() {
    return new MapAdapter<String, String>() {
      public String get(Object key) {
        return link((String) key);
      }
    };
  }
 
  /**
   * This provides Map-style access to the CmsJspActionElement.template(String)
   * This allows you to use the jstl syntax:
   *   ${cms.link['body']} to see of the body element is present
   *
   * @see CmsJspActionElement#template(String)
   * @return a map, which deligates to the template() method of CmsJspActionElement
   */
  public Map<String, Boolean> getTemplate() {
    return new MapAdapter<String, Boolean>() {
      public Boolean get(Object key) {
        return template((String) key);
      }
    };
  }
 
  /**
   * This provides Map-style access to the CmsJspActionElement.info(String)
   * This allows you to use the jstl syntax:
   *   ${cms.info['java.version']} to get the System property java.version
   *
   * @see CmsJspActionElement#info(String)
   * @return a map, which deligates to the info() method of CmsJspActionElement
   */
  public Map<String, String> getInfo() {
    return new MapAdapter<String, String>() {
      public String get(Object key) {
        return info((String) key);
      }
    };
  }
 
  /**
   * This provides Map-style access to the CmsJspActionElement.user(String)
   * This allows you to use the jstl syntax:
   *   ${cms.user['Address']} to get the Address property from the current logged in user
   *
   * @see CmsJspActionElement#user(String)
   * @return a map, which deligates to the user() method of CmsJspActionElement
   */
  public Map<String, String> getUser() {
    return new MapAdapter<String, String>() {
      public String get(Object key) {
        return user((String) key);
      }
    };
  }
}
TOP

Related Classes of dk.valtech.octools.jsp.CmsJstlActionElement

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.