1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.UnicodeProperties;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.kernel.xml.Document;
25  import com.liferay.portal.kernel.xml.Element;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.LayoutConstants;
29  import com.liferay.portal.service.LayoutLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  
32  import java.util.List;
33  
34  /**
35   * <a href="SitemapUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Jorge Ferrer
38   */
39  public class SitemapUtil {
40  
41      public static String getSitemap(
42              long groupId, boolean privateLayout, ThemeDisplay themeDisplay)
43          throws PortalException, SystemException {
44  
45          Document doc = SAXReaderUtil.createDocument();
46  
47          doc.setXMLEncoding(StringPool.UTF8);
48  
49          Element root = doc.addElement(
50              "urlset", "http://www.google.com/schemas/sitemap/0.84");
51  
52          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
53              groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
54  
55          _visitLayouts(root, layouts, themeDisplay);
56  
57          return doc.asXML();
58      }
59  
60      public static String encodeXML(String input) {
61          return StringUtil.replace(
62              input,
63              new String[] {"&", "<", ">", "'", "\""},
64              new String[] {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"});
65      }
66  
67      private static void _visitLayouts(
68              Element element, List<Layout> layouts, ThemeDisplay themeDisplay)
69          throws PortalException, SystemException {
70  
71          for (Layout layout : layouts) {
72              UnicodeProperties typeSettingsProperties =
73                  layout.getTypeSettingsProperties();
74  
75              if (PortalUtil.isLayoutSitemapable(layout) && !layout.isHidden() &&
76                  GetterUtil.getBoolean(
77                      typeSettingsProperties.getProperty("sitemap-include"),
78                      true)) {
79  
80                  Element url = element.addElement("url");
81  
82                  String layoutFullURL = PortalUtil.getLayoutFullURL(
83                      layout, themeDisplay);
84  
85                  url.addElement("loc").addText(encodeXML(layoutFullURL));
86  
87                  String changefreq = typeSettingsProperties.getProperty(
88                      "sitemap-changefreq");
89  
90                  if (Validator.isNotNull(changefreq)) {
91                      url.addElement("changefreq").addText(changefreq);
92                  }
93  
94                  String priority = typeSettingsProperties.getProperty(
95                      "sitemap-priority");
96  
97                  if (Validator.isNotNull(priority)) {
98                      url.addElement("priority").addText(priority);
99                  }
100 
101                 List<Layout> children = layout.getChildren();
102 
103                 _visitLayouts(element, children, themeDisplay);
104             }
105         }
106     }
107 
108 }