001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.util;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.Layout;
029: import com.liferay.portal.service.LayoutLocalServiceUtil;
030:
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Properties;
034:
035: import org.dom4j.Document;
036: import org.dom4j.DocumentHelper;
037: import org.dom4j.Element;
038:
039: /**
040: * <a href="SitemapUtil.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Jorge Ferrer
043: *
044: */
045: public class SitemapUtil {
046:
047: public static String getSitemap(long groupId,
048: boolean privateLayout, String urlPrefix)
049: throws PortalException, SystemException {
050:
051: List layouts = LayoutLocalServiceUtil.getLayouts(groupId,
052: privateLayout);
053:
054: return getSitemap(layouts, urlPrefix);
055: }
056:
057: public static String getSitemap(List layouts, String urlPrefix)
058: throws PortalException, SystemException {
059:
060: Document doc = DocumentHelper.createDocument();
061:
062: doc.setXMLEncoding("UTF-8");
063:
064: Element root = doc.addElement("urlset",
065: "http://www.google.com/schemas/sitemap/0.84");
066:
067: _visitLayouts(root, layouts, urlPrefix);
068:
069: return doc.asXML();
070: }
071:
072: public static String encodeXML(String input) {
073: return StringUtil.replace(input, new String[] { "&", "<", ">",
074: "'", "\"" }, new String[] { "&", "<", ">",
075: "'", """ });
076: }
077:
078: private static void _visitLayouts(Element element, List layouts,
079: String urlPrefix) throws PortalException, SystemException {
080:
081: Iterator itr = layouts.iterator();
082:
083: while (itr.hasNext()) {
084: Layout layout = (Layout) itr.next();
085:
086: Properties props = layout.getTypeSettingsProperties();
087:
088: if (PortalUtil.isLayoutSitemapable(layout)
089: && !layout.isHidden()
090: && GetterUtil.getBoolean(props
091: .getProperty("sitemap-include"), true)) {
092:
093: Element url = element.addElement("url");
094:
095: String layoutURL = PortalUtil.getLayoutActualURL(
096: layout, urlPrefix);
097:
098: url.addElement("loc").addText(encodeXML(layoutURL));
099:
100: String changefreq = props
101: .getProperty("sitemap-changefreq");
102:
103: if (Validator.isNotNull(changefreq)) {
104: url.addElement("changefreq").addText(changefreq);
105: }
106:
107: String priority = props.getProperty("sitemap-priority");
108:
109: if (Validator.isNotNull(priority)) {
110: url.addElement("priority").addText(priority);
111: }
112:
113: List children = layout.getChildren();
114:
115: _visitLayouts(element, children, urlPrefix);
116: }
117: }
118: }
119:
120: }
|