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