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.HttpUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.UnicodeProperties;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.kernel.xml.Document;
34  import com.liferay.portal.kernel.xml.Element;
35  import com.liferay.portal.kernel.xml.SAXReaderUtil;
36  import com.liferay.portal.model.Layout;
37  import com.liferay.portal.service.LayoutLocalServiceUtil;
38  import com.liferay.portal.theme.ThemeDisplay;
39  
40  import java.util.List;
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 = SAXReaderUtil.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 = PortalUtil.getLayoutURL(
90                      layout, themeDisplay);
91  
92                  if (!HttpUtil.hasDomain(layoutURL)) {
93                      layoutURL = themeDisplay.getPortalURL() + layoutURL;
94                  }
95  
96                  url.addElement("loc").addText(encodeXML(layoutURL));
97  
98                  String changefreq = props.getProperty("sitemap-changefreq");
99  
100                 if (Validator.isNotNull(changefreq)) {
101                     url.addElement("changefreq").addText(changefreq);
102                 }
103 
104                 String priority = props.getProperty("sitemap-priority");
105 
106                 if (Validator.isNotNull(priority)) {
107                     url.addElement("priority").addText(priority);
108                 }
109 
110                 List<Layout> children = layout.getChildren();
111 
112                 _visitLayouts(element, children, themeDisplay);
113             }
114         }
115     }
116 
117 }