1   /**
2    * Copyright (c) 2000-2008 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.model.Layout;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portal.theme.ThemeDisplay;
35  
36  import java.util.List;
37  
38  import org.dom4j.Document;
39  import org.dom4j.DocumentHelper;
40  import org.dom4j.Element;
41  
42  /**
43   * <a href="SitemapUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Jorge Ferrer
46   *
47   */
48  public class SitemapUtil {
49  
50      public static String getSitemap(
51              long groupId, boolean privateLayout, ThemeDisplay themeDisplay)
52          throws PortalException, SystemException {
53  
54          Document doc = DocumentHelper.createDocument();
55  
56          doc.setXMLEncoding(StringPool.UTF8);
57  
58          Element root = doc.addElement(
59              "urlset", "http://www.google.com/schemas/sitemap/0.84");
60  
61          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
62              groupId, privateLayout);
63  
64          _visitLayouts(root, layouts, themeDisplay);
65  
66          return doc.asXML();
67      }
68  
69      public static String encodeXML(String input) {
70          return StringUtil.replace(
71              input,
72              new String[] {"&", "<", ">", "'", "\""},
73              new String[] {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"});
74      }
75  
76      private static void _visitLayouts(
77              Element element, List<Layout> layouts, ThemeDisplay themeDisplay)
78          throws PortalException, SystemException {
79  
80          for (Layout layout : layouts) {
81              UnicodeProperties props = layout.getTypeSettingsProperties();
82  
83              if (PortalUtil.isLayoutSitemapable(layout) && !layout.isHidden() &&
84                  GetterUtil.getBoolean(
85                      props.getProperty("sitemap-include"), true)) {
86  
87                  Element url = element.addElement("url");
88  
89                  String layoutURL =
90                      themeDisplay.getPortalURL() +
91                          PortalUtil.getLayoutURL(layout, themeDisplay);
92  
93                  url.addElement("loc").addText(encodeXML(layoutURL));
94  
95                  String changefreq = props.getProperty("sitemap-changefreq");
96  
97                  if (Validator.isNotNull(changefreq)) {
98                      url.addElement("changefreq").addText(changefreq);
99                  }
100 
101                 String priority = props.getProperty("sitemap-priority");
102 
103                 if (Validator.isNotNull(priority)) {
104                     url.addElement("priority").addText(priority);
105                 }
106 
107                 List<Layout> children = layout.getChildren();
108 
109                 _visitLayouts(element, children, themeDisplay);
110             }
111         }
112     }
113 
114 }