1   /**
2    * Copyright (c) 2000-2007 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.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.service.LayoutLocalServiceUtil;
32  
33  import java.util.Iterator;
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 layouts = LayoutLocalServiceUtil.getLayouts(
54              groupId, privateLayout);
55  
56          return getSitemap(layouts, urlPrefix);
57      }
58  
59      public static String getSitemap(List layouts, String urlPrefix)
60          throws PortalException, SystemException {
61  
62          Document doc = DocumentHelper.createDocument();
63  
64          doc.setXMLEncoding("UTF-8");
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 layouts, String urlPrefix)
83          throws PortalException, SystemException {
84  
85          Iterator itr = layouts.iterator();
86  
87          while (itr.hasNext()) {
88              Layout layout = (Layout)itr.next();
89  
90              Properties props = layout.getTypeSettingsProperties();
91  
92              if (PortalUtil.isLayoutSitemapable(layout) && !layout.isHidden() &&
93                  GetterUtil.getBoolean(
94                      props.getProperty("sitemap-include"), true)) {
95  
96                  Element url = element.addElement("url");
97  
98                  String layoutURL = PortalUtil.getLayoutActualURL(
99                      layout, urlPrefix);
100 
101                 url.addElement("loc").addText(encodeXML(layoutURL));
102 
103                 String changefreq = props.getProperty("sitemap-changefreq");
104 
105                 if (Validator.isNotNull(changefreq)) {
106                     url.addElement("changefreq").addText(changefreq);
107                 }
108 
109                 String priority = props.getProperty("sitemap-priority");
110 
111                 if (Validator.isNotNull(priority)) {
112                     url.addElement("priority").addText(priority);
113                 }
114 
115                 List children = layout.getChildren();
116 
117                 _visitLayouts(element, children, urlPrefix);
118             }
119         }
120     }
121 
122 }