001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.xml.CmsXmlContentDefinition;
035import org.opencms.xml.content.CmsDefaultXmlContentHandler;
036
037import java.util.ArrayList;
038import java.util.LinkedHashSet;
039import java.util.List;
040import java.util.Set;
041
042import org.apache.commons.logging.Log;
043
044/**
045 * The XML content handler class for group containers.
046 */
047public class CmsXmlGroupContainerHandler extends CmsDefaultXmlContentHandler {
048
049    /** The log instance for this class. */
050    protected static final Log LOG = CmsLog.getLog(CmsXmlGroupContainerHandler.class);
051
052    /**
053     * Default constructor.<p>
054     *
055     */
056    public CmsXmlGroupContainerHandler() {
057
058        super();
059    }
060
061    /**
062     * Helper method to load and prepare the elements of a group container.<p>
063     *
064     * @param cms the current CMS context
065     * @param resource the group container resource
066     *
067     * @return the elements which have been read from the group container
068     *
069     * @throws CmsException if something goes wrong
070     */
071    protected static List<CmsContainerElementBean> loadGroupContainerElements(CmsObject cms, CmsResource resource)
072    throws CmsException {
073
074        CmsXmlGroupContainer xmlGroupContainer = CmsXmlGroupContainerFactory.unmarshal(cms, resource);
075        CmsGroupContainerBean groupContainer = xmlGroupContainer.getGroupContainer(cms);
076        List<CmsContainerElementBean> elemBeans = groupContainer.getElements();
077        List<CmsContainerElementBean> result = new ArrayList<CmsContainerElementBean>();
078        for (CmsContainerElementBean elementBean : elemBeans) {
079            if (!elementBean.isInMemoryOnly()) {
080                elementBean.initResource(cms);
081                result.add(elementBean);
082            }
083        }
084        return result;
085    }
086
087    /**
088     * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#getCSSHeadIncludes(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
089     */
090    @Override
091    public Set<String> getCSSHeadIncludes(CmsObject cms, CmsResource resource) throws CmsException {
092
093        Set<String> result = new LinkedHashSet<String>();
094
095        List<CmsContainerElementBean> containerElements = loadGroupContainerElements(cms, resource);
096        for (CmsContainerElementBean elementBean : containerElements) {
097            if (elementBean.isGroupContainer(cms) || elementBean.isInheritedContainer(cms)) {
098                throw new CmsException(
099                    Messages.get().container(
100                        Messages.ERR_ELEMENT_GROUP_REFERENCES_ANOTHER_GROUP_2,
101                        resource.getRootPath(),
102                        elementBean.getResource().getRootPath()));
103            }
104            CmsResource elementResource = elementBean.getResource();
105            Set<String> elementIncludes = CmsXmlContentDefinition.getContentHandlerForResource(
106                cms,
107                elementResource).getCSSHeadIncludes(cms, elementResource);
108            result.addAll(elementIncludes);
109        }
110        return result;
111    }
112
113    /**
114     * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#getJSHeadIncludes(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
115     */
116    @Override
117    public Set<String> getJSHeadIncludes(CmsObject cms, CmsResource resource) throws CmsException {
118
119        Set<String> result = new LinkedHashSet<String>();
120        List<CmsContainerElementBean> containerElements = loadGroupContainerElements(cms, resource);
121        for (CmsContainerElementBean elementBean : containerElements) {
122            if (elementBean.isGroupContainer(cms) || elementBean.isInheritedContainer(cms)) {
123                throw new CmsException(
124                    Messages.get().container(
125                        Messages.ERR_ELEMENT_GROUP_REFERENCES_ANOTHER_GROUP_2,
126                        resource.getRootPath(),
127                        elementBean.getResource().getRootPath()));
128            }
129            CmsResource elementResource = elementBean.getResource();
130            Set<String> elementIncludes = CmsXmlContentDefinition.getContentHandlerForResource(
131                cms,
132                elementResource).getJSHeadIncludes(cms, elementResource);
133            result.addAll(elementIncludes);
134        }
135        return result;
136    }
137
138    /**
139     * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#hasModifiableFormatters()
140     */
141    @Override
142    public boolean hasModifiableFormatters() {
143
144        return false;
145    }
146
147}