001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.UnicodeProperties;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.model.LayoutConstants;
029    import com.liferay.portal.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.theme.ThemeDisplay;
031    
032    import java.util.List;
033    
034    /**
035     * @author Jorge Ferrer
036     */
037    public class SitemapUtil {
038    
039            public static String getSitemap(
040                            long groupId, boolean privateLayout, ThemeDisplay themeDisplay)
041                    throws PortalException, SystemException {
042    
043                    Document doc = SAXReaderUtil.createDocument();
044    
045                    doc.setXMLEncoding(StringPool.UTF8);
046    
047                    Element root = doc.addElement(
048                            "urlset", "http://www.google.com/schemas/sitemap/0.84");
049    
050                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
051                            groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
052    
053                    _visitLayouts(root, layouts, themeDisplay);
054    
055                    return doc.asXML();
056            }
057    
058            public static String encodeXML(String input) {
059                    return StringUtil.replace(
060                            input,
061                            new String[] {"&", "<", ">", "'", "\""},
062                            new String[] {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"});
063            }
064    
065            private static void _visitLayouts(
066                            Element element, List<Layout> layouts, ThemeDisplay themeDisplay)
067                    throws PortalException, SystemException {
068    
069                    for (Layout layout : layouts) {
070                            UnicodeProperties props = layout.getTypeSettingsProperties();
071    
072                            if (PortalUtil.isLayoutSitemapable(layout) && !layout.isHidden() &&
073                                    GetterUtil.getBoolean(
074                                            props.getProperty("sitemap-include"), true)) {
075    
076                                    Element url = element.addElement("url");
077    
078                                    String layoutFullURL = PortalUtil.getLayoutFullURL(
079                                            layout, themeDisplay);
080    
081                                    url.addElement("loc").addText(encodeXML(layoutFullURL));
082    
083                                    String changefreq = props.getProperty("sitemap-changefreq");
084    
085                                    if (Validator.isNotNull(changefreq)) {
086                                            url.addElement("changefreq").addText(changefreq);
087                                    }
088    
089                                    String priority = props.getProperty("sitemap-priority");
090    
091                                    if (Validator.isNotNull(priority)) {
092                                            url.addElement("priority").addText(priority);
093                                    }
094    
095                                    List<Layout> children = layout.getChildren();
096    
097                                    _visitLayouts(element, children, themeDisplay);
098                            }
099                    }
100            }
101    
102    }