/** * Creates a new instance loading any providers found in the provided skin. * * @param site The skin to generate a sitemap for * @param dest The directory to write files to. * @param url The URI at which this directory is accessible using HTTP. * @throws IOException If the files can't be written. */ public SitemapGenerator(final URI site, final File dest, final URI url) throws IOException { this(new ArrayList<PageProvider>(), dest, url); // HACK add site into system properties, a la the global context System.setProperty("no.sesat.sitemap.SitemapGenerator.site", site.toString()); for (PageProvider provider : ServiceLoader.load(PageProvider.class, getClassLoader(site))) { LOG.info("Found " + provider.getName()); providers.add(provider); } }
/** * Generate the sitemap files. * * @throws IOException if the files could not be created. * @throws SAXException if a xml error occurs. */ public void generate() throws IOException, SAXException { int totalCount = 0; AttributesImpl schemaLocation = new AttributesImpl(); transformerHandler.startDocument(); transformerHandler.startPrefixMapping("xsd", XMLConstants.W3C_XML_SCHEMA_NS_URI); transformerHandler.startPrefixMapping("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); schemaLocation.addAttribute( XMLConstants.W3C_XML_SCHEMA_NS_URI, "schemaLocation", "xsi:schemaLocation", "CDATA", "http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"); transformerHandler.startElement(NS, "", "sitemapindex", schemaLocation); for (final PageProvider provider : providers) { LOG.info("Processing " + provider.getName()); final SiteMap group = new SiteMap(provider.getName()); try { for (final Page page : provider) { if (null != page) { group.addPage(page); } } } finally { group.finish(); LOG.info(group.getCount() + " entries processed for " + provider.getName()); totalCount += group.getCount(); } for (final SiteMap.SiteMapFile map : group.getSiteMaps()) { transformerHandler.startElement("", "", "sitemap", new AttributesImpl()); addElement("loc", uri.resolve(map.getFileName()).toString()); addElement("lastmod", formatDateW3c((new Date()))); transformerHandler.endElement("", "", "sitemap"); } } transformerHandler.endElement(NS, "", "sitemapindex"); transformerHandler.endDocument(); writer.close(); LOG.info("All done (" + totalCount + " entries)"); }