Generador de Sitemap

Descripción

El sitemap es un fichero XML que permite a los robots y buscadores conocer todas las páginas que tiene nuestro sitio web.

Referencias

Sitemaps.org, Sitemap on Wikipedia

Demo

A continuación tienes un enlace al sitemap de esta web:

Ver sitemap

Códigos fuente

package es.carlosmontero.webapp.t5devutil.pages;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.PersistentLocale;

import es.carlosmontero.webapp.t5devutil.pages.IndexPage.MenuGroup;
import es.carlosmontero.webapp.t5devutil.pages.IndexPage.MenuItem;

@ContentType(value = "text/xml")
public class Sitemap {

   @Inject
   private Messages messages;

   @Inject
   private PageRenderLinkSource pageRenderLinkSource;

   @Inject
   private PersistentLocale persistentLocale;

   @Property
   private List<Object> pages;

   @Property
   private Object page;

   @Property
   private List<String> locales;

   @Property
   private String locale, otherLocale;

   @Inject
   @Symbol(SymbolConstants.SUPPORTED_LOCALES)
   private String supportedLocales;

   public void setupRender() {

      locales = Arrays.asList(supportedLocales.split(","));

      pages = new ArrayList<Object>();

      pages.add(IndexPage.class);
      pages.add(AboutPage.class);

      for (final MenuGroup menuGroup : IndexPage.MENU_GROUPS) {
         for (final MenuItem menuItem : menuGroup.items) {
            if (StringUtils.isNotBlank(menuItem.page)) {
               pages.add(menuItem.page);
            }
         }
      }

   }

   public String getURL(final String pageName, final String locale) {

      persistentLocale.set(new Locale(locale));

      return pageRenderLinkSource.createPageRenderLinkWithContext(pageName).toAbsoluteURI();
   }

   public String getLink(final String pageName, final String locale) {
      return messages.format("SitemapLink", locale, getURL(pageName, locale));
   }

   public String getURL() {
      return getURL(locale);
   }

   public String getURL(final String locale) {

      persistentLocale.set(new Locale(locale));

      if (page instanceof String) {
         return pageRenderLinkSource.createPageRenderLinkWithContext((String) page).toAbsoluteURI();
      }
      return pageRenderLinkSource.createPageRenderLinkWithContext((Class) page).toAbsoluteURI();
   }

   public String getLink() {
      return messages.format("SitemapLink", otherLocale, getURL(otherLocale));
   }

   public String getLink2() {
      return messages.format("SitemapLink", locale, getURL(locale));
   }

   public List<String> getOtherLocales() {

      final List<String> otherLocales = new ArrayList<String>(locales);
      otherLocales.remove(locale);
      return otherLocales;

   }

   /**
    * Optional. For debug.
    */
   public String getXmlComment() {
      String pageName = null;
      if (page instanceof String) {
         pageName = (String) page;
      }
      else if (page instanceof Class) {
         pageName = ((Class) page).getCanonicalName().toLowerCase();
         pageName = pageName.substring(21, pageName.length() - 4);
      }
      return new StringBuilder("<!-- ").append(pageName.replace(".", "/"))
              .append(" -->").toString();
   }

}