1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.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 props = layout.getTypeSettingsProperties();
73  
74              if (PortalUtil.isLayoutSitemapable(layout) && !layout.isHidden() &&
75                  GetterUtil.getBoolean(
76                      props.getProperty("sitemap-include"), true)) {
77  
78                  Element url = element.addElement("url");
79  
80                  String layoutFullURL = PortalUtil.getLayoutFullURL(
81                      layout, themeDisplay);
82  
83                  url.addElement("loc").addText(encodeXML(layoutFullURL));
84  
85                  String changefreq = props.getProperty("sitemap-changefreq");
86  
87                  if (Validator.isNotNull(changefreq)) {
88                      url.addElement("changefreq").addText(changefreq);
89                  }
90  
91                  String priority = props.getProperty("sitemap-priority");
92  
93                  if (Validator.isNotNull(priority)) {
94                      url.addElement("priority").addText(priority);
95                  }
96  
97                  List<Layout> children = layout.getChildren();
98  
99                  _visitLayouts(element, children, themeDisplay);
100             }
101         }
102     }
103 
104 }