001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2011 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.ade.containerpage.inherited;
033
034import org.opencms.file.CmsFile;
035import org.opencms.file.CmsObject;
036import org.opencms.file.CmsResource;
037import org.opencms.main.CmsException;
038import org.opencms.util.CmsStringUtil;
039import org.opencms.xml.content.CmsXmlContent;
040import org.opencms.xml.content.CmsXmlContentFactory;
041import org.opencms.xml.content.CmsXmlContentRootLocation;
042import org.opencms.xml.content.I_CmsXmlContentLocation;
043import org.opencms.xml.content.I_CmsXmlContentValueLocation;
044
045import java.util.HashMap;
046import java.util.List;
047import java.util.Locale;
048import java.util.Map;
049
050/**
051 * Parser class for parsing inheritance container references.<p>
052 */
053public class CmsInheritanceReferenceParser {
054
055    /** The CMS context used for parsing the file. */
056    private CmsObject m_cms;
057
058    /** The map of parsed references. */
059    private Map<Locale, CmsInheritanceReference> m_references = new HashMap<Locale, CmsInheritanceReference>();
060
061    /** The resource being parsed. */
062    private CmsResource m_resource;
063
064    /**
065     * Creates a new parser instance.<p>
066     *
067     * @param cms the CMS context to use
068     */
069    public CmsInheritanceReferenceParser(CmsObject cms) {
070
071        m_cms = cms;
072    }
073
074    /**
075     * Gets the parsed reference for a locale.<p>
076     *
077     * Gets the reference object for the locale, and uses the reference for the English language as a fallback.<p>
078     *
079     * @param  locale the locale to get the reference for
080     * @return the reference for the locale
081     */
082    public CmsInheritanceReference getReference(Locale locale) {
083
084        CmsInheritanceReference ref = m_references.get(locale);
085        if (ref == null) {
086            ref = m_references.get(Locale.ENGLISH);
087        }
088        return ref;
089    }
090
091    /**
092     * Parses a given resource.<p>
093     *
094     * @param resource the resource to parse
095     *
096     * @throws CmsException if something goes wrong
097     */
098    public void parse(CmsResource resource) throws CmsException {
099
100        CmsFile file = m_cms.readFile(resource);
101        m_resource = resource;
102        CmsXmlContent content = CmsXmlContentFactory.unmarshal(m_cms, file);
103        parse(content);
104    }
105
106    /**
107     * Parses the given XML content.<p>
108     *
109     * @param content the XML content to parse
110     */
111    protected void parse(CmsXmlContent content) {
112
113        List<Locale> availableLocales = content.getLocales();
114        for (Locale locale : availableLocales) {
115            CmsXmlContentRootLocation location = new CmsXmlContentRootLocation(content, locale);
116            CmsInheritanceReference ref = parseReference(location, locale);
117            if (ref != null) {
118                m_references.put(locale, ref);
119            }
120        }
121    }
122
123    /**
124     * Extracts a single inheritance reference from a location in the XML content.<p>
125     *
126     * This method may return null if the given location doesn't contain a valid inheritance container reference.<p>
127     *
128     * @param location the location from which to parse the inheritance reference
129     * @param locale the locale from  which to parse the inheritance reference
130     *
131     * @return the parsed inheritance reference, or null
132     */
133    protected CmsInheritanceReference parseReference(I_CmsXmlContentLocation location, Locale locale) {
134
135        I_CmsXmlContentValueLocation nameLocation = location.getSubValue("ConfigName");
136        if (nameLocation == null) {
137            return null;
138        }
139        String configName = nameLocation.asString(m_cms);
140        if (CmsStringUtil.isEmptyOrWhitespaceOnly(configName)) {
141            return null;
142        }
143        configName = configName.trim();
144        I_CmsXmlContentValueLocation titleLocation = location.getSubValue("Title");
145        String title = null;
146        if (titleLocation != null) {
147            title = titleLocation.asString(m_cms);
148        }
149        I_CmsXmlContentValueLocation descriptionLocation = location.getSubValue("Description");
150        String description = null;
151        if (descriptionLocation != null) {
152            description = descriptionLocation.asString(m_cms);
153        }
154        return new CmsInheritanceReference(configName, title, description, m_resource, locale);
155    }
156
157}