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.xml.containerpage;
029
030import org.opencms.ade.configuration.CmsADEManager;
031import org.opencms.ade.containerpage.inherited.CmsInheritanceReference;
032import org.opencms.ade.containerpage.inherited.CmsInheritanceReferenceParser;
033import org.opencms.ade.containerpage.inherited.CmsInheritedContainerState;
034import org.opencms.file.CmsObject;
035import org.opencms.file.CmsResource;
036import org.opencms.main.CmsException;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039import org.opencms.xml.CmsXmlContentDefinition;
040import org.opencms.xml.content.CmsDefaultXmlContentHandler;
041
042import java.util.Collections;
043import java.util.LinkedHashSet;
044import java.util.List;
045import java.util.Set;
046
047import org.apache.commons.logging.Log;
048
049/**
050 * The XML content handler class for inheritance groups.
051 */
052public class CmsXmlInheritGroupContainerHandler extends CmsDefaultXmlContentHandler {
053
054    /** The log object for this class. */
055    private static final Log LOG = CmsLog.getLog(CmsXmlInheritGroupContainerHandler.class);
056
057    /**
058     * Constructor.<p>
059     */
060    public CmsXmlInheritGroupContainerHandler() {
061
062        super();
063    }
064
065    /**
066     * Returns the elements of the given inheritance group for the request context URI.<p>
067     *
068     * @param cms the current cms context
069     * @param resource the inheritance group resource
070     *
071     * @return the elements
072     */
073    public static List<CmsContainerElementBean> loadInheritContainerElements(CmsObject cms, CmsResource resource) {
074
075        CmsInheritanceReferenceParser parser = new CmsInheritanceReferenceParser(cms);
076        try {
077            parser.parse(resource);
078            CmsInheritanceReference ref = parser.getReference(cms.getRequestContext().getLocale());
079            if (ref != null) {
080                String name = ref.getName();
081                CmsADEManager adeManager = OpenCms.getADEManager();
082                CmsInheritedContainerState result = adeManager.getInheritedContainerState(
083                    cms,
084                    cms.getRequestContext().getRootUri(),
085                    name);
086                return result.getElements(false);
087            }
088        } catch (CmsException e) {
089            LOG.error(e.getLocalizedMessage(), e);
090        }
091        return Collections.emptyList();
092    }
093
094    /**
095     * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#getCSSHeadIncludes(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
096     */
097    @Override
098    public Set<String> getCSSHeadIncludes(CmsObject cms, CmsResource resource) throws CmsException {
099
100        Set<String> result = new LinkedHashSet<String>();
101
102        List<CmsContainerElementBean> containerElements = loadInheritContainerElements(cms, resource);
103        for (CmsContainerElementBean elementBean : containerElements) {
104            if (elementBean.isGroupContainer(cms) || elementBean.isInheritedContainer(cms)) {
105                throw new CmsException(
106                    Messages.get().container(
107                        Messages.ERR_ELEMENT_GROUP_REFERENCES_ANOTHER_GROUP_2,
108                        resource.getRootPath(),
109                        elementBean.getResource().getRootPath()));
110            }
111            CmsResource elementResource = elementBean.getResource();
112            Set<String> elementIncludes = CmsXmlContentDefinition.getContentHandlerForResource(
113                cms,
114                elementResource).getCSSHeadIncludes(cms, elementResource);
115            result.addAll(elementIncludes);
116        }
117        return result;
118    }
119
120    /**
121     * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#getJSHeadIncludes(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
122     */
123    @Override
124    public Set<String> getJSHeadIncludes(CmsObject cms, CmsResource resource) throws CmsException {
125
126        Set<String> result = new LinkedHashSet<String>();
127        List<CmsContainerElementBean> containerElements = loadInheritContainerElements(cms, resource);
128        for (CmsContainerElementBean elementBean : containerElements) {
129            if (elementBean.isGroupContainer(cms) || elementBean.isInheritedContainer(cms)) {
130                throw new CmsException(
131                    Messages.get().container(
132                        Messages.ERR_ELEMENT_GROUP_REFERENCES_ANOTHER_GROUP_2,
133                        resource.getRootPath(),
134                        elementBean.getResource().getRootPath()));
135            }
136            CmsResource elementResource = elementBean.getResource();
137            Set<String> elementIncludes = CmsXmlContentDefinition.getContentHandlerForResource(
138                cms,
139                elementResource).getJSHeadIncludes(cms, elementResource);
140            result.addAll(elementIncludes);
141        }
142        return result;
143    }
144
145    /**
146     * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#hasModifiableFormatters()
147     */
148    @Override
149    public boolean hasModifiableFormatters() {
150
151        return false;
152    }
153
154}