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.site.xmlsitemap;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.xml.content.CmsXmlContent;
037import org.opencms.xml.content.CmsXmlContentFactory;
038import org.opencms.xml.types.I_CmsXmlContentValue;
039
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.List;
043import java.util.Locale;
044
045/**
046 * Configuration bean which represents the options which are configurable from a 'seo-file' resource.<p>
047 */
048public class CmsXmlSeoConfiguration {
049
050    /** Mode name constant. */
051    public static final Object MODE_ROBOTS_TXT = "robotstxt";
052
053    /** Mode name constant. */
054    public static final Object MODE_XML_SITEMAP = "xmlsitemap";
055
056    /** Node name. */
057    public static final String N_COMPUTE_CONTAINER_PAGE_DATES = "ComputeContPageDates";
058
059    /** Node name. */
060    public static final String N_EXCLUDE = "SitemapExclude";
061
062    /** Node name. */
063    public static final String N_SERVER_URL = "ServerUrl";
064
065    /** Node name. */
066    public static final String N_GENERATOR_CLASS = "GeneratorClass";
067
068    /** Node name. */
069    public static final String N_INCLUDE = "SitemapInclude";
070
071    /** Node name. */
072    public static final String N_MODE = "Mode";
073
074    /** Node name. */
075    public static final String N_ROBOTS_TXT_TEXT = "RobotsTxtText";
076
077    /** The file type used for generating XML sitemaps or robots.txt files. */
078    public static final String SEO_FILE_TYPE = "seo_file";
079
080    /** The exclude paths. */
081    protected List<String> m_excludes = new ArrayList<String>();
082
083    /** The include paths. */
084    protected List<String> m_includes = new ArrayList<String>();
085
086    /** The mode. */
087    protected String m_mode;
088
089    /** Text to be included in robots.txt after the sitemap references. */
090    protected String m_robotsTxtText = "";
091
092    /** Flag which indicates whether container page modification dates should be computed. */
093    private boolean m_computeContainerPageDates;
094
095    /** The sitemap generator class name. */
096    private String m_generatorClassName;
097
098    /** The server URL replacement. */
099    private String m_serverUrl;
100
101    /**
102     * Gets the list of exclude paths.<p>
103     *
104     * @return the list of exclude paths
105     */
106    public List<String> getExcludes() {
107
108        return Collections.unmodifiableList(m_excludes);
109    }
110
111    /**
112     * Gets the list of include paths.<p>
113     *
114     * @return the list of include paths
115     */
116    public List<String> getIncludes() {
117
118        return Collections.unmodifiableList(m_includes);
119    }
120
121    /**
122     * Gets the mode.<p>
123     *
124     * @return the mode
125     */
126    public String getMode() {
127
128        return m_mode;
129    }
130
131    /**
132     * Gets the text which should be inserted in robots.txt mode.<p>
133     *
134     * @return the text to insert in robots.txt mode
135     */
136    public String getRobotsTxtText() {
137
138        return m_robotsTxtText;
139    }
140
141    /**
142     * Gets the configured server URL.<p>
143     *
144     * This, if set, replaces the host/port used by getOnlineLink() for the URLs
145     * in the XML sitemap.<p>
146     *
147     * @return the server URL
148     */
149    public String getServerUrl() {
150
151        return m_serverUrl;
152    }
153
154    /**
155     * Gets the class name for the sitemap generator class (may return null if none is explicitly configured).
156     *
157     * @return the sitemap generator class name
158     */
159    public String getSitemapGeneratorClassName() {
160
161        return m_generatorClassName;
162    }
163
164    /**
165     * Returns true if this configuration is configured as robots.txt mode.<p>
166     *
167     * @return true if the mode is set to the robots.txt mode
168     */
169    public boolean isXmlSitemapMode() {
170
171        return MODE_XML_SITEMAP.equals(m_mode);
172    }
173
174    /**
175     * Loads the bean data from the given resource.<p>
176     *
177     * @param cms the CMS context to use
178     * @param resource the resource from which to load the data
179     *
180     * @throws CmsException if something goes wrong
181     */
182    public void load(CmsObject cms, CmsResource resource) throws CmsException {
183
184        CmsFile file = cms.readFile(resource);
185        CmsObject rootCms = OpenCms.initCmsObject(cms);
186        rootCms.getRequestContext().setSiteRoot("");
187        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, file);
188        Locale en = new Locale("en");
189        for (I_CmsXmlContentValue value : content.getValues(N_INCLUDE, en)) {
190            String include = value.getStringValue(rootCms);
191            if (!CmsStringUtil.isEmpty(include)) {
192                m_includes.add(include);
193            }
194        }
195        for (I_CmsXmlContentValue value : content.getValues(N_EXCLUDE, en)) {
196            String exclude = value.getStringValue(rootCms);
197            if (!CmsStringUtil.isEmpty(exclude)) {
198                m_excludes.add(exclude);
199            }
200        }
201        I_CmsXmlContentValue robotsValue = content.getValue(N_MODE, en);
202        m_mode = robotsValue.getStringValue(rootCms);
203
204        I_CmsXmlContentValue robotsTxtTextValue = content.getValue(N_ROBOTS_TXT_TEXT, en);
205        if (robotsTxtTextValue != null) {
206            m_robotsTxtText = robotsTxtTextValue.getStringValue(rootCms);
207        }
208
209        I_CmsXmlContentValue computeContPageDates = content.getValue(N_COMPUTE_CONTAINER_PAGE_DATES, en);
210        if (computeContPageDates != null) {
211            m_computeContainerPageDates = Boolean.parseBoolean(computeContPageDates.getStringValue(rootCms));
212        }
213
214        I_CmsXmlContentValue generatorClassValue = content.getValue(N_GENERATOR_CLASS, en);
215        if (generatorClassValue != null) {
216            m_generatorClassName = generatorClassValue.getStringValue(rootCms);
217        }
218
219        I_CmsXmlContentValue serverUrlValue = content.getValue(N_SERVER_URL, en);
220        if (serverUrlValue != null) {
221            m_serverUrl = serverUrlValue.getStringValue(rootCms);
222        }
223    }
224
225    /**
226     * Returns true if container page modification dates should be computed.<p>
227     *
228     * @return true if container page modification dates should be computed
229     */
230    public boolean shouldComputeContainerPageModificationDates() {
231
232        return m_computeContainerPageDates;
233    }
234}