1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.UnicodeProperties;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.kernel.xml.Document;
33  import com.liferay.portal.kernel.xml.Element;
34  import com.liferay.portal.kernel.xml.SAXReaderUtil;
35  import com.liferay.portal.model.Layout;
36  import com.liferay.portal.service.LayoutLocalServiceUtil;
37  import com.liferay.portal.theme.ThemeDisplay;
38  
39  import java.util.List;
40  
41  /**
42   * <a href="SitemapUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Jorge Ferrer
45   */
46  public class SitemapUtil {
47  
48      public static String getSitemap(
49              long groupId, boolean privateLayout, ThemeDisplay themeDisplay)
50          throws PortalException, SystemException {
51  
52          Document doc = SAXReaderUtil.createDocument();
53  
54          doc.setXMLEncoding(StringPool.UTF8);
55  
56          Element root = doc.addElement(
57              "urlset", "http://www.google.com/schemas/sitemap/0.84");
58  
59          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
60              groupId, privateLayout);
61  
62          _visitLayouts(root, layouts, themeDisplay);
63  
64          return doc.asXML();
65      }
66  
67      public static String encodeXML(String input) {
68          return StringUtil.replace(
69              input,
70              new String[] {"&", "<", ">", "'", "\""},
71              new String[] {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"});
72      }
73  
74      private static void _visitLayouts(
75              Element element, List<Layout> layouts, ThemeDisplay themeDisplay)
76          throws PortalException, SystemException {
77  
78          for (Layout layout : layouts) {
79              UnicodeProperties props = layout.getTypeSettingsProperties();
80  
81              if (PortalUtil.isLayoutSitemapable(layout) && !layout.isHidden() &&
82                  GetterUtil.getBoolean(
83                      props.getProperty("sitemap-include"), true)) {
84  
85                  Element url = element.addElement("url");
86  
87                  String layoutFullURL = PortalUtil.getLayoutFullURL(
88                      layout, themeDisplay);
89  
90                  url.addElement("loc").addText(encodeXML(layoutFullURL));
91  
92                  String changefreq = props.getProperty("sitemap-changefreq");
93  
94                  if (Validator.isNotNull(changefreq)) {
95                      url.addElement("changefreq").addText(changefreq);
96                  }
97  
98                  String priority = props.getProperty("sitemap-priority");
99  
100                 if (Validator.isNotNull(priority)) {
101                     url.addElement("priority").addText(priority);
102                 }
103 
104                 List<Layout> children = layout.getChildren();
105 
106                 _visitLayouts(element, children, themeDisplay);
107             }
108         }
109     }
110 
111 }