Google Sitemap.xml
From OpenCms Wiki
The following code will recurse down a directory tree, generating a sitemaps.org compatible sitemap.xml file. You can submit this to google, yahoo, and microsoft, and control how they index your files (including finding links to files that aren't available through text links the spider can read. The protocol names the resources and tells the spider the last time they were modified, improving search spider efficiency.
It respects a set of properties (which inherit, so you only need to set them at a folder level to change the elements within):
- sitemap_hidden : Hides the resource from the sitemap
- sitemap_priority : sets the priority field (1 by default if not otherwise set)
- sitemap_change_frequency : how often the resource changes (weekly by default)
<?xml version="1.0" encoding="UTF-8" ?> <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"> <%@ page session="false" %> <%@ page import="java.util.*,org.opencms.jsp.*,org.opencms.file.*,java.text.DateFormat, java.text.SimpleDateFormat, org.opencms.main.*" %> <%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%! protected String BASE_URL=""; public static SimpleDateFormat ISO8601FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private String recurseTree(CmsObject cmso,CmsJspActionElement jsp, String path) { StringBuffer sb = new StringBuffer(); try { ArrayList files = (ArrayList) cmso.getFilesInFolder(path); for (Iterator i = files.iterator(); i.hasNext(); ) { CmsFile f = (CmsFile) i.next(); String thispath = jsp.link(cmso.getSitePath(f)); CmsProperty secret = cmso.readPropertyObject(f,"sitemap_hidden",true); CmsProperty changeFreqProperty = cmso.readPropertyObject(f,"sitemap_change_frequency",true); CmsProperty priorityProperty = cmso.readPropertyObject(f,"sitemap_priority",true); String changeFrequency = changeFreqProperty.getValue("weekly"); String priority = priorityProperty.getValue("1"); if (secret.getValue("false").equals("false") && (thispath.endsWith("html")||thispath.endsWith("jsp")||thispath.endsWith("pdf"))) { sb.append("<url>\n"); sb.append("<loc>"+BASE_URL+thispath+"</loc>\n"); //DateFormat df = DateFormat.getDateInstance(); String niceDate = ISO8601FORMAT.format(new Date(f.getDateLastModified())); sb.append("<lastmod>"+niceDate+"</lastmod>\n"); sb.append("<changefreq>"+changeFrequency+"</changefreq>\n"); sb.append("<priority>"+priority+"</priority>\n"); sb.append("</url>\n"); } } ArrayList folders = (ArrayList) cmso.getSubFolders(path); for (Iterator j = folders.iterator(); j.hasNext(); ) { CmsFolder f = (CmsFolder) j.next(); sb.append( recurseTree(cmso,jsp, cmso.getSitePath(f) ) ); } } catch (CmsException cmsException) { sb.append("A CMS exception occurred: "+cmsException.toString()); } return sb.toString(); }//the close / open delimiters are necessary %> <% CmsJspActionElement cms = new org.opencms.jsp.CmsJspActionElement(pageContext, request, response); CmsObject cmso = cms.getCmsObject(); String url = cms.info("opencms.url"); int lastSlash = url.indexOf("/",8); BASE_URL = url.substring(0,lastSlash); out.println (recurseTree(cmso,cms, "/")); %> </urlset>