001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jsp.util;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.xml.I_CmsXmlDocument;
034import org.opencms.xml.content.CmsXmlContentFactory;
035
036/**
037 * Base class for context sensitive custom beans that supports creation via {@link CmsJspStandardContextBean#getBean(String className)}.
038 *
039 * It is the suitable base class for custom beans that need access to the OpenCms context.
040 *
041 * @since 11.0
042 */
043public abstract class A_CmsJspCustomContextBean {
044
045    /** The standard context. */
046    private CmsJspStandardContextBean m_context;
047    /** The cms object from the standard context. */
048    private CmsObject m_cms;
049
050    /**
051     * Returns the cms object for the current context.
052     * @return the cms object for the current context.
053     */
054    public CmsObject getCmsObject() {
055
056        return m_cms;
057    }
058
059    /**
060     * Set the context for the bean.
061     * @param context the context to set.
062     */
063    public void setContext(CmsJspStandardContextBean context) {
064
065        m_context = context;
066        m_cms = m_context.getVfs().getCmsObject();
067    }
068
069    /**
070     * Returns the standard context.
071     * @return the standard context.
072     */
073    protected CmsJspStandardContextBean getStandardContextBean() {
074
075        return m_context;
076    }
077
078    /**
079     * Convert the input to a CmsResource using the current context.
080     *
081     * @see CmsJspElFunctions#convertResource(CmsObject, Object)
082     *
083     * @param input the object to convert to a resource, e.g., a path, a structure id, an access wrapper ...
084     * @return the resource for the input.
085     * @throws CmsException if resource conversion fails.
086     */
087    protected CmsResource toResource(Object input) throws CmsException {
088
089        return CmsJspElFunctions.convertRawResource(getCmsObject(), input);
090    }
091
092    /**
093     * Converts the input (typically a specification of an XML file) to an XML document.
094     *
095     * If the input already is an XML document, it is returned.
096     * Otherwise the method assumes the input specifies an XML file
097     * and tries to determine and unmarshal that file.nd unmarshal that file
098     *
099     * To determine the file {@link #toResource(Object)} is used.
100     *
101     * @param input the object to be converted to an XML document.
102     * @return the XML document specified by the input.
103     * @throws CmsException if converting the input to a XML document fails.
104     */
105    protected I_CmsXmlDocument toXml(Object input) throws CmsException {
106
107        if (input instanceof CmsJspContentAccessBean) {
108            return ((CmsJspContentAccessBean)input).getRawContent();
109        }
110        if (input instanceof I_CmsXmlDocument) {
111            return (I_CmsXmlDocument)input;
112        } else {
113            CmsResource res = toResource(input);
114            return CmsXmlContentFactory.unmarshal(getCmsObject(), getCmsObject().readFile(res));
115        }
116    }
117}