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.CmsResource;
031import org.opencms.i18n.CmsEncoder;
032
033import java.text.DateFormat;
034import java.text.SimpleDateFormat;
035import java.util.Date;
036import java.util.Locale;
037
038import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
039
040/**
041 * A bean which represents an entry in an XML sitemap for SEO purposes.<p>
042 */
043public class CmsXmlSitemapUrlBean {
044
045    /** The format used to format the last modification date. */
046    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
047
048    /** The change frequency. */
049    private String m_changeFrequency;
050
051    /** The detail page resource. */
052    private CmsResource m_detailPageResource;
053
054    /** The last modification date. */
055    private Date m_lastModified;
056
057    /** The locale for which the bean has been created (only used for detail pages). */
058    private Locale m_locale;
059
060    /** The original resource. */
061    private CmsResource m_origResource;
062
063    /** The priority. */
064    private double m_priority;
065
066    /** The subsite for which the bean has been created (only used for detail pages). */
067    private String m_subsite;
068
069    /** The URL. */
070    private String m_url;
071
072    /**
073     * Creates a new instance.<p>
074     *
075     * @param url the URL
076     * @param lastModified the last modification date
077     * @param changeFrequency the change frequency string
078     * @param priority the priority
079     */
080    public CmsXmlSitemapUrlBean(String url, long lastModified, String changeFrequency, double priority) {
081
082        m_url = url;
083
084        if (lastModified >= 0) {
085            m_lastModified = new Date(lastModified);
086        }
087        m_changeFrequency = changeFrequency;
088        m_priority = priority;
089    }
090
091    /**
092     * Helper method to format a date in the W3C datetime format.<p>
093     *
094     * @param date the date to format
095     *
096     * @return the formatted date
097     */
098    private static String formatDate(Date date) {
099
100        String dateStr = dateFormat.format(date);
101        // insert colon into timezone
102        return dateStr.substring(0, 22) + ":" + dateStr.substring(22);
103    }
104
105    /**
106     * Gets the change frequency string.<p>
107     *
108     * @return the change frequency string
109     */
110    public String getChangeFrequency() {
111
112        return m_changeFrequency;
113    }
114
115    /**
116     * Gets the last modification date.<p>
117     *
118     * @return the last modification date
119     */
120    public Date getDateLastModified() {
121
122        return m_lastModified;
123    }
124
125    /**
126     * Gets the detail page resource in case the link is the link to a detail page, else returns null.<p>
127     *
128     * @return the container page used as the detail page
129     */
130    public CmsResource getDetailPageResource() {
131
132        return m_detailPageResource;
133    }
134
135    /**
136     * Gets the last modification date formatted as W3C datetime.<p>
137     *
138     * @return the formatted last modification date
139     */
140    public String getFormattedDate() {
141
142        return formatDate(m_lastModified);
143    }
144
145    /**
146     * Returns the locale.<p>
147     *
148     * @return the locale
149     */
150    public Locale getLocale() {
151
152        return m_locale;
153    }
154
155    /**
156     * Gets the original resource belonging to the link.<p>
157     *
158     * In case this is a link to a detail page, the resource will be the resource displayed on the detail page
159     *
160     * @return the original resource
161     */
162    public CmsResource getOriginalResource() {
163
164        return m_origResource;
165    }
166
167    /**
168     * Gets the priority for the page.<p>
169     *
170     * @return the priority
171     */
172    public double getPriority() {
173
174        return m_priority;
175    }
176
177    /**
178     * Returns the subsite.<p>
179     *
180     * @return the subsite
181     */
182    public String getSubsite() {
183
184        return m_subsite;
185    }
186
187    /**
188     * Gets the page URL.<p>
189     *
190     * @return the page URL
191     */
192    public String getUrl() {
193
194        return m_url;
195    }
196
197    /**
198     * Sets the detail page resource.<p>
199     *
200     * @param detailPageResource the detail page resource
201     */
202    public void setDetailPageResource(CmsResource detailPageResource) {
203
204        m_detailPageResource = detailPageResource;
205    }
206
207    /**
208     * Sets the locale.<p>
209     *
210     * @param locale the locale to set
211     */
212    public void setLocale(Locale locale) {
213
214        m_locale = locale;
215    }
216
217    /**
218     * Sets the original resource.<p>
219     *
220     * @param resource the original resource
221     */
222    public void setOriginalResource(CmsResource resource) {
223
224        m_origResource = resource;
225    }
226
227    /**
228     * Sets the subsite.<p>
229     *
230     * @param subsite the subsite to set
231     */
232    public void setSubsite(String subsite) {
233
234        m_subsite = subsite;
235    }
236
237    /**
238     * @see java.lang.Object#toString()
239     */
240    @Override
241    public String toString() {
242
243        return m_url + "   [" + ReflectionToStringBuilder.toString(this) + "]";
244    }
245
246    /**
247     * Writes the changefreq node to the buffer.<p>
248     *
249     * @param buffer the buffer to write to
250     */
251    public void writeChangefreq(StringBuffer buffer) {
252
253        if (m_changeFrequency != null) {
254            writeElement(buffer, "changefreq", getChangeFrequency());
255        }
256    }
257
258    /**
259     * Writes a single XML element with text content to a string buffer.<p>
260     *
261     * @param buffer the string buffer to write to
262     * @param tag the XML tag name
263     * @param content the content of the XML element
264     */
265    public void writeElement(StringBuffer buffer, String tag, String content) {
266
267        buffer.append("<" + tag + ">");
268        buffer.append(CmsEncoder.escapeXml(content));
269        buffer.append("</" + tag + ">");
270    }
271
272    /**
273     * Writes the lastmod node to the buffer.<p>
274     *
275     * @param buffer the buffer to write to
276     */
277    public void writeLastmod(StringBuffer buffer) {
278
279        if (m_lastModified != null) {
280            writeElement(buffer, "lastmod", getFormattedDate());
281        }
282    }
283
284    /**
285     * Writes the priority node to the buffer.<p>
286     *
287     * @param buffer the buffer to write to
288     */
289    public void writePriority(StringBuffer buffer) {
290
291        if ((m_priority >= 0) && (m_priority <= 1)) {
292            writeElement(buffer, "priority", "" + getPriority());
293        }
294    }
295
296}