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_CACHE = "Cache";
064
065    /** Node name. */
066    public static final String N_SERVER_URL = "ServerUrl";
067
068    /** Node name. */
069    public static final String N_GENERATOR_CLASS = "GeneratorClass";
070
071    /** Node name. */
072    public static final String N_INCLUDE = "SitemapInclude";
073
074    /** Node name. */
075    public static final String N_MODE = "Mode";
076
077    /** Node name. */
078    public static final String N_ROBOTS_TXT_TEXT = "RobotsTxtText";
079
080    /** The file type used for generating XML sitemaps or robots.txt files. */
081    public static final String SEO_FILE_TYPE = "seo_file";
082
083    /** The exclude paths. */
084    protected List<String> m_excludes = new ArrayList<String>();
085
086    /** The include paths. */
087    protected List<String> m_includes = new ArrayList<String>();
088
089    /** The mode. */
090    protected String m_mode;
091
092    /** Text to be included in robots.txt after the sitemap references. */
093    protected String m_robotsTxtText = "";
094
095    /** Flag which indicates whether container page modification dates should be computed. */
096    private boolean m_computeContainerPageDates;
097
098    /** The sitemap generator class name. */
099    private String m_generatorClassName;
100
101    /** The server URL replacement. */
102    private String m_serverUrl;
103
104    /** True if sitemap caching should be used. */
105    private boolean m_useCache;
106
107    /**
108     * Gets the list of exclude paths.<p>
109     *
110     * @return the list of exclude paths
111     */
112    public List<String> getExcludes() {
113
114        return Collections.unmodifiableList(m_excludes);
115    }
116
117    /**
118     * Gets the list of include paths.<p>
119     *
120     * @return the list of include paths
121     */
122    public List<String> getIncludes() {
123
124        return Collections.unmodifiableList(m_includes);
125    }
126
127    /**
128     * Gets the mode.<p>
129     *
130     * @return the mode
131     */
132    public String getMode() {
133
134        return m_mode;
135    }
136
137    /**
138     * Gets the text which should be inserted in robots.txt mode.<p>
139     *
140     * @return the text to insert in robots.txt mode
141     */
142    public String getRobotsTxtText() {
143
144        return m_robotsTxtText;
145    }
146
147    /**
148     * Gets the configured server URL.<p>
149     *
150     * This, if set, replaces the host/port used by getOnlineLink() for the URLs
151     * in the XML sitemap.<p>
152     *
153     * @return the server URL
154     */
155    public String getServerUrl() {
156
157        return m_serverUrl;
158    }
159
160    /**
161     * Gets the class name for the sitemap generator class (may return null if none is explicitly configured).
162     *
163     * @return the sitemap generator class name
164     */
165    public String getSitemapGeneratorClassName() {
166
167        return m_generatorClassName;
168    }
169
170    /**
171     * Returns true if this configuration is configured as robots.txt mode.<p>
172     *
173     * @return true if the mode is set to the robots.txt mode
174     */
175    public boolean isXmlSitemapMode() {
176
177        return MODE_XML_SITEMAP.equals(m_mode);
178    }
179
180    /**
181     * Loads the bean data from the given resource.<p>
182     *
183     * @param cms the CMS context to use
184     * @param resource the resource from which to load the data
185     *
186     * @throws CmsException if something goes wrong
187     */
188    public void load(CmsObject cms, CmsResource resource) throws CmsException {
189
190        CmsFile file = cms.readFile(resource);
191        CmsObject rootCms = OpenCms.initCmsObject(cms);
192        rootCms.getRequestContext().setSiteRoot("");
193        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, file);
194        Locale en = new Locale("en");
195        for (I_CmsXmlContentValue value : content.getValues(N_INCLUDE, en)) {
196            String include = value.getStringValue(rootCms);
197            if (!CmsStringUtil.isEmpty(include)) {
198                m_includes.add(include);
199            }
200        }
201        for (I_CmsXmlContentValue value : content.getValues(N_EXCLUDE, en)) {
202            String exclude = value.getStringValue(rootCms);
203            if (!CmsStringUtil.isEmpty(exclude)) {
204                m_excludes.add(exclude);
205            }
206        }
207        I_CmsXmlContentValue robotsValue = content.getValue(N_MODE, en);
208        m_mode = robotsValue.getStringValue(rootCms);
209
210        I_CmsXmlContentValue robotsTxtTextValue = content.getValue(N_ROBOTS_TXT_TEXT, en);
211        if (robotsTxtTextValue != null) {
212            m_robotsTxtText = robotsTxtTextValue.getStringValue(rootCms);
213        }
214
215        I_CmsXmlContentValue computeContPageDates = content.getValue(N_COMPUTE_CONTAINER_PAGE_DATES, en);
216        if (computeContPageDates != null) {
217            m_computeContainerPageDates = Boolean.parseBoolean(computeContPageDates.getStringValue(rootCms));
218        }
219
220        I_CmsXmlContentValue generatorClassValue = content.getValue(N_GENERATOR_CLASS, en);
221        if (generatorClassValue != null) {
222            m_generatorClassName = generatorClassValue.getStringValue(rootCms);
223        }
224
225        I_CmsXmlContentValue cacheValue = content.getValue(N_CACHE, en);
226        if (cacheValue != null) {
227            m_useCache = Boolean.parseBoolean(cacheValue.getStringValue(rootCms));
228        }
229
230        I_CmsXmlContentValue serverUrlValue = content.getValue(N_SERVER_URL, en);
231        if (serverUrlValue != null) {
232            m_serverUrl = serverUrlValue.getStringValue(rootCms);
233        }
234    }
235
236    /**
237     * Returns true if container page modification dates should be computed.<p>
238     *
239     * @return true if container page modification dates should be computed
240     */
241    public boolean shouldComputeContainerPageModificationDates() {
242
243        return m_computeContainerPageDates;
244    }
245
246    /**
247     * True if caching should be used for the generated XML sitemap.<p>
248     *
249     * @return true if caching should be used for the generated XML sitemap
250     */
251    public boolean usesCache() {
252
253        return m_useCache;
254    }
255}