All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redfin.sitemapgenerator.GoogleLinkSitemapGenerator Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.redfin.sitemapgenerator;

import java.io.File;
import java.net.*;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Builds a Google Link Sitemap (to indicate alternate language pages).
 *
 * @author Sergio Vico
 * @see Creating alternate language pages Sitemaps
 * @see Mobile SEO configurations | Separate URLs 
 */
public class GoogleLinkSitemapGenerator extends SitemapGenerator {

	private static class Renderer extends AbstractSitemapUrlRenderer
			implements ISitemapUrlRenderer {

		public Class getUrlClass() {

			return GoogleLinkSitemapUrl.class;
		}

		public String getXmlNamespaces() {

			return "xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"";
		}

		public void render(final GoogleLinkSitemapUrl url, final StringBuilder sb, final W3CDateFormat dateFormat) {

			final StringBuilder tagSb = new StringBuilder();
			for (final Entry> entry : url.getAlternates().entrySet()) {
				tagSb.append("     innerEntry : entry.getValue().entrySet()){
					tagSb.append("      " + innerEntry.getKey() + "=\"" + innerEntry.getValue() + "\"\n");
				}
				tagSb.append("      href=\"" + UrlUtils.escapeXml(entry.getKey().toString()) + "\"\n");
				tagSb.append("    />\n");
			}
			super.render(url, sb, dateFormat, tagSb.toString());
		}

	}

	/**
	 * Configures a builder so you can specify sitemap generator options
	 *
	 * @param baseUrl
	 *			All URLs in the generated sitemap(s) should appear under this base URL
	 * @param baseDir
	 *			Sitemap files will be generated in this directory as either "sitemap.xml" or
	 *			"sitemap1.xml" "sitemap2.xml" and so on.
	 * @return a builder; call .build() on it to make a sitemap generator
	 */
	public static SitemapGeneratorBuilder builder(final String baseUrl, final File baseDir)
		throws MalformedURLException {

		return new SitemapGeneratorBuilder(baseUrl, baseDir,
				GoogleLinkSitemapGenerator.class);
	}

	/**
	 * Configures a builder so you can specify sitemap generator options
	 *
	 * @param baseUrl
	 *			All URLs in the generated sitemap(s) should appear under this base URL
	 * @param baseDir
	 *			Sitemap files will be generated in this directory as either "sitemap.xml" or
	 *			"sitemap1.xml" "sitemap2.xml" and so on.
	 * @return a builder; call .build() on it to make a sitemap generator
	 */
	public static SitemapGeneratorBuilder builder(final URL baseUrl, final File baseDir) {

		return new SitemapGeneratorBuilder(baseUrl, baseDir,
				GoogleLinkSitemapGenerator.class);
	}

	/**
	 * Configures the generator with a base URL and a null directory. The object constructed is not
	 * intended to be used to write to files. Rather, it is intended to be used to obtain
	 * XML-formatted strings that represent sitemaps.
	 *
	 * @param baseUrl
	 *			All URLs in the generated sitemap(s) should appear under this base URL
	 */
	public GoogleLinkSitemapGenerator(final String baseUrl) throws MalformedURLException {
		this(new SitemapGeneratorOptions(new URL(baseUrl)));
	}

	/**
	 * Configures the generator with a base URL and directory to write the sitemap files.
	 *
	 * @param baseUrl
	 *			All URLs in the generated sitemap(s) should appear under this base URL
	 * @param baseDir
	 *			Sitemap files will be generated in this directory as either "sitemap.xml" or
	 *			"sitemap1.xml" "sitemap2.xml" and so on.
	 * @throws MalformedURLException
	 */
	public GoogleLinkSitemapGenerator(final String baseUrl, final File baseDir) throws MalformedURLException {
		this(new SitemapGeneratorOptions(baseUrl, baseDir));
	}

	/**
	 * Configures the generator with a base URL and a null directory. The object constructed is not
	 * intended to be used to write to files. Rather, it is intended to be used to obtain
	 * XML-formatted strings that represent sitemaps.
	 *
	 * @param baseUrl
	 *			All URLs in the generated sitemap(s) should appear under this base URL
	 */
	public GoogleLinkSitemapGenerator(final URL baseUrl) {
		this(new SitemapGeneratorOptions(baseUrl));
	}

	/**
	 * Configures the generator with a base URL and directory to write the sitemap files.
	 *
	 * @param baseUrl
	 *			All URLs in the generated sitemap(s) should appear under this base URL
	 * @param baseDir
	 *			Sitemap files will be generated in this directory as either "sitemap.xml" or
	 *			"sitemap1.xml" "sitemap2.xml" and so on.
	 */
	public GoogleLinkSitemapGenerator(final URL baseUrl, final File baseDir) {
		this(new SitemapGeneratorOptions(baseUrl, baseDir));
	}

	GoogleLinkSitemapGenerator(final AbstractSitemapGeneratorOptions options) {
		super(options, new Renderer());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy