Compare commits

..

6 Commits
v1.1.2 ... main

Author SHA1 Message Date
c621e05154 修改版本为 1.0.1-SNAPSHOT 2022-12-15 00:59:01 -05:00
8eeeccb213 更新软件的包和使用的 JDK 版本 2022-12-15 00:44:03 -05:00
dd08af8f9f 先不进行签名 2022-12-14 23:25:28 -05:00
871f58e1d5 Update pom file and by use JDK 11 to build 2022-12-14 23:23:54 -05:00
Marc Trölitzsch
619bcfce92
add LICENSE (#51)
Adds an Apache 2.0 license file to the repository. Closes #38
2021-06-23 10:29:30 -07:00
Matthias Kurz
c6bf718b6e
Prepare next snaphost version 2019-01-27 15:13:23 +01:00
10 changed files with 1182 additions and 917 deletions

191
LICENSE Normal file
View File

@ -0,0 +1,191 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
Copyright 2009 Dan Faublich
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

113
README.md
View File

@ -120,3 +120,116 @@ Google can understand a wide variety of custom sitemap formats that they made up
To generate a special type of sitemap, just use GoogleMobileSitemapGenerator, GoogleGeoSitemapGenerator, GoogleCodeSitemapGenerator, GoogleCodeSitemapGenerator, GoogleNewsSitemapGenerator, or GoogleVideoSitemapGenerator instead of WebSitemapGenerator.
You can't mix-and-match regular URLs with Google-specific sitemaps, so you'll also have to use a GoogleMobileSitemapUrl, GoogleGeoSitemapUrl, GoogleCodeSitemapUrl, GoogleNewsSitemapUrl, or GoogleVideoSitemapUrl instead of a WebSitemapUrl. Each of them has unique configurable options not available to regular web URLs.
<html><head><title>How to use SitemapGen4j</title></head>
<body>
<h1>How to use SitemapGen4j</h1>
SitemapGen4j is a library to generate XML sitemaps in Java.
<h2>What's an XML sitemap?</h2>
Quoting from <a href="http://sitemaps.org/index.php">sitemaps.org</a>:
<blockquote><p>Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.</p>
<p>Web crawlers usually discover pages from links within the site and from other sites. Sitemaps supplement this data to allow crawlers that support Sitemaps to pick up all URLs in the Sitemap and learn about those URLs using the associated metadata. Using the Sitemap protocol does not guarantee that web pages are included in search engines, but provides hints for web crawlers to do a better job of crawling your site.</p>
<p>Sitemap 0.90 is offered under the terms of the Attribution-ShareAlike Creative Commons License and has wide adoption, including support from Google, Yahoo!, and Microsoft.</p>
</blockquote>
<h2>Getting started</h2>
<p>The easiest way to get started is to just use the WebSitemapGenerator class, like this:
<pre name="code" class="java">WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", myDir);
wsg.addUrl("http://www.example.com/index.html"); // repeat multiple times
wsg.write();</pre>
<h2>Configuring options</h2>
But there are a lot of nifty options available for URLs and for the generator as a whole. To configure the generator, use a builder:
<pre name="code" class="java">WebSitemapGenerator wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.gzip(true).build(); // enable gzipped output
wsg.addUrl("http://www.example.com/index.html");
wsg.write();</pre>
To configure the URLs, construct a WebSitemapUrl with WebSitemapUrl.Options.
<pre name="code" class="java">WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", myDir);
WebSitemapUrl url = new WebSitemapUrl.Options("http://www.example.com/index.html")
.lastMod(new Date()).priority(1.0).changeFreq(ChangeFreq.HOURLY).build();
// this will configure the URL with lastmod=now, priority=1.0, changefreq=hourly
wsg.addUrl(url);
wsg.write();</pre>
<h2>Configuring the date format</h2>
One important configuration option for the sitemap generator is the date format. The <a href="http://www.w3.org/TR/NOTE-datetime">W3C datetime standard</a> allows you to choose the precision of your datetime (anything from just specifying the year like "1997" to specifying the fraction of the second like "1997-07-16T19:20:30.45+01:00"); if you don't specify one, we'll try to guess which one you want, and we'll use the default timezone of the local machine, which might not be what you prefer.
<pre name="code" class="java">
// Use DAY pattern (2009-02-07), Greenwich Mean Time timezone
W3CDateFormat dateFormat = new W3CDateFormat(Pattern.DAY);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
WebSitemapGenerator wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.dateFormat(dateFormat).build(); // actually use the configured dateFormat
wsg.addUrl("http://www.example.com/index.html");
wsg.write();</pre>
<h2>Lots of URLs: a sitemap index file</h2>
One sitemap can contain a maximum of 50,000 URLs. (Some sitemaps, like Google News sitemaps, can contain only 1,000 URLs.) If you need to put more URLs than that in a sitemap, you'll have to use a sitemap index file. Fortunately, WebSitemapGenerator can manage the whole thing for you.
<pre name="code" class="java">WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", myDir);
for (int i = 0; i &lt; 60000; i++) wsg.addUrl("http://www.example.com/doc"+i+".html");
wsg.write();
wsg.writeSitemapsWithIndex(); // generate the sitemap_index.xml
</pre>
<p>That will generate two sitemaps for 60K URLs: sitemap1.xml (with 50K urls) and sitemap2.xml (with the remaining 10K), and then generate a sitemap_index.xml file describing the two.</p>
<p>It's also possible to carefully organize your sub-sitemaps. For example, it's recommended to group URLs with the same changeFreq together (have one sitemap for changeFreq "daily" and another for changeFreq "yearly"), so you can modify the lastMod of the daily sitemap without modifying the lastMod of the yearly sitemap. To do that, just construct your sitemaps one at a time using the WebSitemapGenerator, then use the SitemapIndexGenerator to create a single index for all of them.</p>
<pre name="code" class="java">WebSitemapGenerator wsg;
// generate foo sitemap
wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.fileNamePrefix("foo").build();
for (int i = 0; i &lt; 5; i++) wsg.addUrl("http://www.example.com/foo"+i+".html");
wsg.write();
// generate bar sitemap
wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.fileNamePrefix("bar").build();
for (int i = 0; i &lt; 5; i++) wsg.addUrl("http://www.example.com/bar"+i+".html");
wsg.write();
// generate sitemap index for foo + bar
SitemapIndexGenerator sig = new SitemapIndexGenerator("http://www.example.com", myFile);
sig.addUrl("http://www.example.com/foo.xml");
sig.addUrl("http://www.example.com/bar.xml");
sig.write();</pre>
<p>You could also use the SitemapIndexGenerator to incorporate sitemaps generated by other tools. For example, you might use Google's official Python sitemap generator to generate some sitemaps, and use WebSitemapGenerator to generate some sitemaps, and use SitemapIndexGenerator to make an index of all of them.</p>
<h2>Validate your sitemaps</h2>
<p>SitemapGen4j can also validate your sitemaps using the official XML Schema Definition (XSD). If you used SitemapGen4j to make the sitemaps, you shouldn't need to do this unless there's a bug in our code. But you can use it to validate sitemaps generated by other tools, and it provides an extra level of safety.</p>
<p>It's easy to configure the WebSitemapGenerator to automatically validate your sitemaps right after you write them (but this does slow things down, naturally).</p>
<pre name="code" class="java">WebSitemapGenerator wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.autoValidate(true).build(); // validate the sitemap after writing
wsg.addUrl("http://www.example.com/index.html");
wsg.write();</pre>
<p>You can also use the SitemapValidator directly to manage sitemaps. It has two methods: validateWebSitemap(File f) and validateSitemapIndex(File f).</p>
<h2>Google-specific sitemaps</h2>
<p>Google can understand a wide variety of custom sitemap formats that they made up, including a Mobile sitemaps, Geo sitemaps, Code sitemaps (for Google Code search), Google News sitemaps, and Video sitemaps. SitemapGen4j can generate any/all of these different types of sitemaps.</p>
<p>To generate a special type of sitemap, just use GoogleMobileSitemapGenerator, GoogleGeoSitemapGenerator, GoogleCodeSitemapGenerator, GoogleCodeSitemapGenerator, GoogleNewsSitemapGenerator, or GoogleVideoSitemapGenerator instead of WebSitemapGenerator.</p>
<p>You can't mix-and-match regular URLs with Google-specific sitemaps, so you'll also have to use a GoogleMobileSitemapUrl, GoogleGeoSitemapUrl, GoogleCodeSitemapUrl, GoogleNewsSitemapUrl, or GoogleVideoSitemapUrl instead of a WebSitemapUrl. Each of them has unique configurable options not available to regular web URLs.</p>
</body>
</html>

236
pom.xml
View File

@ -1,126 +1,114 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dfabulich</groupId>
<artifactId>sitemapgen4j</artifactId>
<packaging>jar</packaging>
<version>1.1.2</version>
<name>SitemapGen4J</name>
<url>https://github.com/dfabulich/sitemapgen4j/</url>
<description>SitemapGen4j is an XML sitemap generator written in Java.</description>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:git:git://github.com:dfabulich/sitemapgen4j.git</connection>
<developerConnection>scm:git:git@github.com:dfabulich/sitemapgen4j.git</developerConnection>
<url>https://github.com/dfabulich/sitemapgen4j/</url>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<developers>
<developer>
<id>dfabulich</id>
<name>Dan Fabulich</name>
<email>dan@fabulich.com</email>
<organization>Redfin</organization>
<organizationUrl>http://www.redfin.com/</organizationUrl>
<timezone>-8</timezone>
</developer>
</developers>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ossez</groupId>
<artifactId>sitemap-j</artifactId>
<packaging>jar</packaging>
<version>1.0.1-SNAPSHOT</version>
<name>SitemapJ</name>
<url>https://github.com/honeymoose/sitemap-j</url>
<description>SitemapJ is an XML sitemap generator written in Java.</description>
<scm>
<connection>scm:git:git://github.com:dfabulich/sitemapgen4j.git</connection>
<developerConnection>scm:git:git@github.com:dfabulich/sitemapgen4j.git</developerConnection>
<url>https://github.com/dfabulich/sitemapgen4j/</url>
</scm>
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<developers>
<developer>
<name>YuCheng Hu</name>
<id>honeymoose</id>
<email>huyuchengus@gmail.com</email>
<timezone>-5</timezone>
<organization>Open Source</organization>
<roles>
<role>Sr. Java Developer</role>
</roles>
</developer>
</developers>
<licenses>
<license>
<name>The MIT license</name>
<url>https://opensource.org/licenses/mit-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
<distributionManagement>
<repository>
<id>ossez-repo</id>
<url>https://repo.ossez.com/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>ossez-repo</id>
<url>https://repo.ossez.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<fork>true</fork>
<compilerReuseStrategy>alwaysNew</compilerReuseStrategy>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>create-javadoc-jar</id>
<goals>
<goal>javadoc</goal>
<goal>jar</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -6,100 +6,109 @@ import java.net.URL;
/**
* Builds a code sitemap for Google Code Search. To configure options, use {@link #builder(URL, File)}
*
* @author Dan Fabulich
* @see <a href="http://www.google.com/support/webmasters/bin/answer.py?answer=75224">Creating Code Search Sitemaps</a>
*/
public class GoogleCodeSitemapGenerator extends SitemapGenerator<GoogleCodeSitemapUrl,GoogleCodeSitemapGenerator> {
GoogleCodeSitemapGenerator(AbstractSitemapGeneratorOptions<?> options) {
super(options, new Renderer());
}
public class GoogleCodeSitemapGenerator extends SitemapGenerator<GoogleCodeSitemapUrl, GoogleCodeSitemapGenerator> {
/** 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 GoogleCodeSitemapGenerator(String baseUrl, File baseDir)
throws MalformedURLException {
this(new SitemapGeneratorOptions(baseUrl, baseDir));
}
GoogleCodeSitemapGenerator(AbstractSitemapGeneratorOptions<?> options) {
super(options, new Renderer());
}
/**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 GoogleCodeSitemapGenerator(URL baseUrl, File baseDir) {
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 GoogleCodeSitemapGenerator(String baseUrl) throws MalformedURLException {
this(new SitemapGeneratorOptions(new URL(baseUrl)));
}
/**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 GoogleCodeSitemapGenerator(URL baseUrl) {
this(new SitemapGeneratorOptions(baseUrl));
}
/** 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<GoogleCodeSitemapGenerator> builder(URL baseUrl, File baseDir) {
return new SitemapGeneratorBuilder<GoogleCodeSitemapGenerator>(baseUrl, baseDir, GoogleCodeSitemapGenerator.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
* @throws MalformedURLException
*/
public static SitemapGeneratorBuilder<GoogleCodeSitemapGenerator> builder(String baseUrl, File baseDir) throws MalformedURLException {
return new SitemapGeneratorBuilder<GoogleCodeSitemapGenerator>(baseUrl, baseDir, GoogleCodeSitemapGenerator.class);
}
/**
* 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 Exception
*/
public GoogleCodeSitemapGenerator(String baseUrl, File baseDir)
throws MalformedURLException {
this(new SitemapGeneratorOptions(baseUrl, baseDir));
}
private static class Renderer extends AbstractSitemapUrlRenderer<GoogleCodeSitemapUrl> implements ISitemapUrlRenderer<GoogleCodeSitemapUrl> {
/**
* 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 GoogleCodeSitemapGenerator(URL baseUrl, File baseDir) {
this(new SitemapGeneratorOptions(baseUrl, baseDir));
}
public Class<GoogleCodeSitemapUrl> getUrlClass() {
return GoogleCodeSitemapUrl.class;
}
public String getXmlNamespaces() {
return "xmlns:codesearch=\"http://www.google.com/codesearch/schemas/sitemap/1.0\"";
}
public void render(GoogleCodeSitemapUrl url, StringBuilder sb,
W3CDateFormat dateFormat) {
StringBuilder tagSb = new StringBuilder();
tagSb.append(" <codesearch:codesearch>\n");
renderTag(tagSb, "codesearch", "filetype", url.getFileType());
renderTag(tagSb, "codesearch", "license", url.getLicense());
renderTag(tagSb, "codesearch", "filename", url.getFileName());
renderTag(tagSb, "codesearch", "packageurl", url.getPackageUrl());
renderTag(tagSb, "codesearch", "packagemap", url.getPackageMap());
tagSb.append(" </codesearch:codesearch>\n");
super.render(url, sb, dateFormat, tagSb.toString());
}
}
/**
* 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
* @throws MalformedURLException Exception
*/
public GoogleCodeSitemapGenerator(String baseUrl) throws MalformedURLException {
this(new SitemapGeneratorOptions(new URL(baseUrl)));
}
/**
* 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 GoogleCodeSitemapGenerator(URL baseUrl) {
this(new SitemapGeneratorOptions(baseUrl));
}
/**
* 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<GoogleCodeSitemapGenerator> builder(URL baseUrl, File baseDir) {
return new SitemapGeneratorBuilder<GoogleCodeSitemapGenerator>(baseUrl, baseDir, GoogleCodeSitemapGenerator.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
* @throws MalformedURLException
*/
public static SitemapGeneratorBuilder<GoogleCodeSitemapGenerator> builder(String baseUrl, File baseDir) throws MalformedURLException {
return new SitemapGeneratorBuilder<GoogleCodeSitemapGenerator>(baseUrl, baseDir, GoogleCodeSitemapGenerator.class);
}
private static class Renderer extends AbstractSitemapUrlRenderer<GoogleCodeSitemapUrl> implements ISitemapUrlRenderer<GoogleCodeSitemapUrl> {
public Class<GoogleCodeSitemapUrl> getUrlClass() {
return GoogleCodeSitemapUrl.class;
}
public String getXmlNamespaces() {
return "xmlns:codesearch=\"http://www.google.com/codesearch/schemas/sitemap/1.0\"";
}
public void render(GoogleCodeSitemapUrl url, StringBuilder sb,
W3CDateFormat dateFormat) {
StringBuilder tagSb = new StringBuilder();
tagSb.append(" <codesearch:codesearch>\n");
renderTag(tagSb, "codesearch", "filetype", url.getFileType());
renderTag(tagSb, "codesearch", "license", url.getLicense());
renderTag(tagSb, "codesearch", "filename", url.getFileName());
renderTag(tagSb, "codesearch", "packageurl", url.getPackageUrl());
renderTag(tagSb, "codesearch", "packagemap", url.getPackageMap());
tagSb.append(" </codesearch:codesearch>\n");
super.render(url, sb, dateFormat, tagSb.toString());
}
}
}

View File

@ -18,7 +18,7 @@ public class GoogleCodeSitemapUrl extends WebSitemapUrl {
*/
public enum FileType {
/** A special value meaning that the URL is a compressed archive containing code.
* @see @see <a href="http://www.google.com/support/webmasters/bin/answer.py?answer=75259">Supported archive suffixes</a>
* @see <a href="http://www.google.com/support/webmasters/bin/answer.py?answer=75259">Supported archive suffixes</a>
*/
ARCHIVE("Archive"),
ADA("Ada"),

View File

@ -18,8 +18,11 @@ public class GoogleMobileSitemapUrl extends WebSitemapUrl {
public Options(String url) throws MalformedURLException {
this(new URL(url));
}
/** Specifies the url */
/**
* Specifies the url
* @param url
*/
public Options(URL url) {
super(url, GoogleMobileSitemapUrl.class);
}

View File

@ -5,347 +5,396 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
/** One configurable Google Video Search URL. To configure, use {@link Options}
*
/**
* One configurable Google Video Search URL. To configure, use {@link Options}
*
* @author Dan Fabulich
* @see Options
* @see <a href="http://www.google.com/support/webmasters/bin/answer.py?answer=80472">Creating Video Sitemaps</a>
*/
public class GoogleVideoSitemapUrl extends WebSitemapUrl {
private final URL playerUrl;
private final URL contentUrl;
private final URL thumbnailUrl;
private final String title;
private final String description;
private final Double rating;
private final Integer viewCount;
private final Date publicationDate;
private final ArrayList<String> tags;
private final String category;
// TODO can there be multiple categories?
// "Usually a video will belong to a single category."
// http://www.google.com/support/webmasters/bin/answer.py?answer=80472
private final String familyFriendly;
private final Integer durationInSeconds;
private final String allowEmbed;
/** Options to configure Google Video URLs */
public static class Options extends AbstractSitemapUrlOptions<GoogleVideoSitemapUrl, Options> {
private URL playerUrl;
private URL contentUrl;
private URL thumbnailUrl;
private String title;
private String description;
private Double rating;
private Integer viewCount;
private Date publicationDate;
private ArrayList<String> tags;
private String category;
// TODO can there be multiple categories?
// "Usually a video will belong to a single category."
// http://www.google.com/support/webmasters/bin/answer.py?answer=80472
private Boolean familyFriendly;
private Integer durationInSeconds;
private Boolean allowEmbed;
/** Specifies a landing page URL, together with a "player" (e.g. SWF)
*
* @param url the landing page URL
* @param playerUrl the URL of the "player" (e.g. SWF file)
* @param allowEmbed when specifying a player, you must specify whether embedding is allowed
*/
public Options(URL url, URL playerUrl, boolean allowEmbed) {
super(url, GoogleVideoSitemapUrl.class);
this.playerUrl = playerUrl;
this.allowEmbed = allowEmbed;
}
/** Specifies a landing page URL, together with the URL of the underlying video (e.g. FLV)
*
* @param url the landing page URL
* @param contentUrl the URL of the underlying video (e.g. FLV)
*/
public Options(URL url, URL contentUrl) {
super(url, GoogleVideoSitemapUrl.class);
this.contentUrl = contentUrl;
}
/** Specifies a player URL (e.g. SWF)
*
* @param playerUrl the URL of the "player" (e.g. SWF file)
* @param allowEmbed when specifying a player, you must specify whether embedding is allowed
*/
public Options playerUrl(URL playerUrl, boolean allowEmbed) {
this.playerUrl = playerUrl;
this.allowEmbed = allowEmbed;
return this;
}
/** Specifies the URL of the underlying video (e.g FLV) */
public Options contentUrl(URL contentUrl) {
this.contentUrl = contentUrl;
return this;
}
/**
* A URL pointing to the URL for the video thumbnail image file. This
* allows you to suggest the thumbnail you want displayed in search
* results. If you provide a {@link #contentUrl(URL)}, Google will attempt
* to generate a set of representative thumbnail images from your actual
* video content. However, we strongly recommended that you provide a
* thumbnail URL to increase the likelihood of your video being included
* in the video index.
*/
public Options thumbnailUrl(URL thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
return this;
}
/** The title of the video. Limited to 100 characters. */
public Options title(String title) {
if (title != null) {
if (title.length() > 100) {
throw new RuntimeException("Video title is limited to 100 characters: " + title);
}
}
this.title = title;
return this;
}
/** The description of the video. Descriptions longer than 2048 characters will be truncated. */
public Options description(String description) {
if (description != null) {
if (description.length() > 2048) {
throw new RuntimeException("Truncate video descriptions to 2048 characters: " + description);
}
}
this.description = description;
return this;
}
/** The rating of the video. The value must be number in the range 0.0-5.0. */
public Options rating(Double rating) {
if (rating != null) {
if (rating < 0 || rating > 5.0) {
throw new RuntimeException("Rating must be between 0.0 and 5.0:" + rating);
}
}
this.rating = rating;
return this;
}
/** The number of times the video has been viewed */
public Options viewCount(int viewCount) {
this.viewCount = viewCount;
return this;
}
/** The date the video was first published, in {@link W3CDateFormat}. */
public Options publicationDate(Date publicationDate) {
this.publicationDate = publicationDate;
return this;
}
/**
* Tag associated with the video; tags are generally very short
* descriptions of key concepts associated with a video or piece of
* content. A single video could have several tags, although it might
* belong to only one category. For example, a video about grilling food
* may belong in the Grilling category, but could be tagged "steak",
* "meat", "summer", and "outdoor". Create a new <video:tag> element for
* each tag associated with a video. A maximum of 32 tags is permitted.
*/
public Options tags(ArrayList<String> tags) {
this.tags = tags;
return this;
}
/**
* Tag associated with the video; tags are generally very short
* descriptions of key concepts associated with a video or piece of
* content. A single video could have several tags, although it might
* belong to only one category. For example, a video about grilling food
* may belong in the Grilling category, but could be tagged "steak",
* "meat", "summer", and "outdoor". Create a new <video:tag> element for
* each tag associated with a video. A maximum of 32 tags is permitted.
*/
public Options tags(Iterable<String> tags) {
this.tags = new ArrayList<String>();
for (String tag : tags) {
this.tags.add(tag);
}
return this;
}
/**
* Tag associated with the video; tags are generally very short
* descriptions of key concepts associated with a video or piece of
* content. A single video could have several tags, although it might
* belong to only one category. For example, a video about grilling food
* may belong in the Grilling category, but could be tagged "steak",
* "meat", "summer", and "outdoor". Create a new <video:tag> element for
* each tag associated with a video. A maximum of 32 tags is permitted.
*/
public Options tags(String... tags) {
return tags(Arrays.asList(tags));
}
/**
* The video's category; for example, <code>cooking</code>. The value
* should be a string no longer than 256 characters. In general,
* categories are broad groupings of content by subject. Usually a video
* will belong to a single category. For example, a site about cooking
* could have categories for Broiling, Baking, and Grilling
*/
public Options category(String category) {
if (category != null) {
if (category.length() > 256) {
throw new RuntimeException("Video category is limited to 256 characters: " + title);
}
}
this.category = category;
return this;
}
/** Whether the video is suitable for viewing by children */
public Options familyFriendly(boolean familyFriendly) {
this.familyFriendly = familyFriendly;
return this;
}
/** The duration of the video in seconds; value must be between 0 and 28800 (8 hours). */
public Options durationInSeconds(int durationInSeconds) {
if (durationInSeconds < 0 || durationInSeconds > 28800) {
throw new RuntimeException("Duration must be between 0 and 28800 (8 hours):" + durationInSeconds);
}
this.durationInSeconds = durationInSeconds;
return this;
}
}
private final URL playerUrl;
private final URL contentUrl;
private final URL thumbnailUrl;
private final String title;
private final String description;
private final Double rating;
private final Integer viewCount;
private final Date publicationDate;
private final ArrayList<String> tags;
private final String category;
// TODO can there be multiple categories?
// "Usually a video will belong to a single category."
// http://www.google.com/support/webmasters/bin/answer.py?answer=80472
private final String familyFriendly;
private final Integer durationInSeconds;
private final String allowEmbed;
/** Specifies a landing page URL, together with a "player" (e.g. SWF)
*
* @param url the landing page URL
* @param playerUrl the URL of the "player" (e.g. SWF file)
* @param allowEmbed when specifying a player, you must specify whether embedding is allowed
*/
public GoogleVideoSitemapUrl(URL url, URL playerUrl, boolean allowEmbed) {
this(new Options(url, playerUrl, allowEmbed));
}
/** Specifies a landing page URL, together with the URL of the underlying video (e.g. FLV)
*
* @param url the landing page URL
* @param contentUrl the URL of the underlying video (e.g. FLV)
*/
public GoogleVideoSitemapUrl(URL url, URL contentUrl) {
this(new Options(url, contentUrl));
}
/** Configures the url with options */
public GoogleVideoSitemapUrl(Options options) {
super(options);
contentUrl = options.contentUrl;
playerUrl = options.playerUrl;
if (playerUrl == null && contentUrl == null) {
throw new RuntimeException("You must specify either contentUrl or playerUrl or both; neither were specified");
}
allowEmbed = convertBooleanToYesOrNo(options.allowEmbed);
if (playerUrl != null && allowEmbed == null) {
throw new RuntimeException("allowEmbed must be specified if playerUrl is specified");
}
category = options.category;
description = options.description;
durationInSeconds = options.durationInSeconds;
familyFriendly = convertBooleanToYesOrNo(options.familyFriendly);
publicationDate = options.publicationDate;
rating = options.rating;
tags = options.tags;
if (tags != null && tags.size() > 32) {
throw new RuntimeException("A maximum of 32 tags is permitted");
}
thumbnailUrl = options.thumbnailUrl;
title = options.title;
viewCount = options.viewCount;
}
private static String convertBooleanToYesOrNo(Boolean value) {
if (value == null) return null;
return value ? "Yes" : "No";
}
/** Retrieves the {@link Options#playerUrl}*/
public URL getPlayerUrl() {
return playerUrl;
}
/** Retrieves the {@link Options#contentUrl}*/
public URL getContentUrl() {
return contentUrl;
}
/**
* Options to configure Google Video URLs
*/
public static class Options extends AbstractSitemapUrlOptions<GoogleVideoSitemapUrl, Options> {
private URL playerUrl;
private URL contentUrl;
private URL thumbnailUrl;
private String title;
private String description;
private Double rating;
private Integer viewCount;
private Date publicationDate;
private ArrayList<String> tags;
private String category;
// TODO can there be multiple categories?
// "Usually a video will belong to a single category."
// http://www.google.com/support/webmasters/bin/answer.py?answer=80472
private Boolean familyFriendly;
private Integer durationInSeconds;
private Boolean allowEmbed;
/** Retrieves the {@link Options#thumbnailUrl}*/
public URL getThumbnailUrl() {
return thumbnailUrl;
}
/**
* Specifies a landing page URL, together with a "player" (e.g. SWF)
*
* @param url the landing page URL
* @param playerUrl the URL of the "player" (e.g. SWF file)
* @param allowEmbed when specifying a player, you must specify whether embedding is allowed
*/
public Options(URL url, URL playerUrl, boolean allowEmbed) {
super(url, GoogleVideoSitemapUrl.class);
this.playerUrl = playerUrl;
this.allowEmbed = allowEmbed;
}
/** Retrieves the {@link Options#title}*/
public String getTitle() {
return title;
}
/**
* Specifies a landing page URL, together with the URL of the underlying video (e.g. FLV)
*
* @param url the landing page URL
* @param contentUrl the URL of the underlying video (e.g. FLV)
*/
public Options(URL url, URL contentUrl) {
super(url, GoogleVideoSitemapUrl.class);
this.contentUrl = contentUrl;
}
/** Retrieves the {@link Options#description}*/
public String getDescription() {
return description;
}
/**
* Specifies a player URL (e.g. SWF)
*
* @param playerUrl the URL of the "player" (e.g. SWF file)
* @param allowEmbed when specifying a player, you must specify whether embedding is allowed
*/
public Options playerUrl(URL playerUrl, boolean allowEmbed) {
this.playerUrl = playerUrl;
this.allowEmbed = allowEmbed;
return this;
}
/** Retrieves the {@link Options#rating}*/
public Double getRating() {
return rating;
}
/**
* Specifies the URL of the underlying video (e.g FLV)
*/
public Options contentUrl(URL contentUrl) {
this.contentUrl = contentUrl;
return this;
}
/** Retrieves the {@link Options#viewCount}*/
public Integer getViewCount() {
return viewCount;
}
/**
* A URL pointing to the URL for the video thumbnail image file. This
* allows you to suggest the thumbnail you want displayed in search
* results. If you provide a {@link #contentUrl(URL)}, Google will attempt
* to generate a set of representative thumbnail images from your actual
* video content. However, we strongly recommended that you provide a
* thumbnail URL to increase the likelihood of your video being included
* in the video index.
*/
public Options thumbnailUrl(URL thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
return this;
}
/** Retrieves the {@link Options#publicationDate}*/
public Date getPublicationDate() {
return publicationDate;
}
/**
* The title of the video. Limited to 100 characters.
*/
public Options title(String title) {
if (title != null) {
if (title.length() > 100) {
throw new RuntimeException("Video title is limited to 100 characters: " + title);
}
}
this.title = title;
return this;
}
/** Retrieves the {@link Options#tags}*/
public ArrayList<String> getTags() {
return tags;
}
/**
* The description of the video. Descriptions longer than 2048 characters will be truncated.
*/
public Options description(String description) {
if (description != null) {
if (description.length() > 2048) {
throw new RuntimeException("Truncate video descriptions to 2048 characters: " + description);
}
}
this.description = description;
return this;
}
/** Retrieves the {@link Options#category}*/
public String getCategory() {
return category;
}
/**
* The rating of the video. The value must be number in the range 0.0-5.0.
*/
public Options rating(Double rating) {
if (rating != null) {
if (rating < 0 || rating > 5.0) {
throw new RuntimeException("Rating must be between 0.0 and 5.0:" + rating);
}
}
this.rating = rating;
return this;
}
/** Retrieves whether the video is {@link Options#familyFriendly}*/
public String getFamilyFriendly() {
return familyFriendly;
}
/**
* The number of times the video has been viewed
*/
public Options viewCount(int viewCount) {
this.viewCount = viewCount;
return this;
}
/** Retrieves the {@link Options#durationInSeconds}*/
public Integer getDurationInSeconds() {
return durationInSeconds;
}
/**
* The date the video was first published, in {@link W3CDateFormat}.
*/
public Options publicationDate(Date publicationDate) {
this.publicationDate = publicationDate;
return this;
}
/** Retrieves whether embedding is allowed */
public String getAllowEmbed() {
return allowEmbed;
}
/**
* Tag associated with the video; tags are generally very short
* descriptions of key concepts associated with a video or piece of
* content. A single video could have several tags, although it might
* belong to only one category. For example, a video about grilling food
* may belong in the Grilling category, but could be tagged "steak",
* "meat", "summer", and "outdoor". Create a new &lt;video:tag&gt; element for
* each tag associated with a video. A maximum of 32 tags is permitted.
*/
public Options tags(ArrayList<String> tags) {
this.tags = tags;
return this;
}
/**
* Tag associated with the video; tags are generally very short
* descriptions of key concepts associated with a video or piece of
* content. A single video could have several tags, although it might
* belong to only one category. For example, a video about grilling food
* may belong in the Grilling category, but could be tagged "steak",
* "meat", "summer", and "outdoor". Create a new &lt;video:tag&gt; element for
* each tag associated with a video. A maximum of 32 tags is permitted.
*/
public Options tags(Iterable<String> tags) {
this.tags = new ArrayList<String>();
for (String tag : tags) {
this.tags.add(tag);
}
return this;
}
/**
* Tag associated with the video; tags are generally very short
* descriptions of key concepts associated with a video or piece of
* content. A single video could have several tags, although it might
* belong to only one category. For example, a video about grilling food
* may belong in the Grilling category, but could be tagged "steak",
* "meat", "summer", and "outdoor". Create a new &lt;video:tag&gt; element for
* each tag associated with a video. A maximum of 32 tags is permitted.
*/
public Options tags(String... tags) {
return tags(Arrays.asList(tags));
}
/**
* The video's category; for example, <code>cooking</code>. The value
* should be a string no longer than 256 characters. In general,
* categories are broad groupings of content by subject. Usually a video
* will belong to a single category. For example, a site about cooking
* could have categories for Broiling, Baking, and Grilling
*/
public Options category(String category) {
if (category != null) {
if (category.length() > 256) {
throw new RuntimeException("Video category is limited to 256 characters: " + title);
}
}
this.category = category;
return this;
}
/**
* Whether the video is suitable for viewing by children
*/
public Options familyFriendly(boolean familyFriendly) {
this.familyFriendly = familyFriendly;
return this;
}
/**
* The duration of the video in seconds; value must be between 0 and 28800 (8 hours).
*/
public Options durationInSeconds(int durationInSeconds) {
if (durationInSeconds < 0 || durationInSeconds > 28800) {
throw new RuntimeException("Duration must be between 0 and 28800 (8 hours):" + durationInSeconds);
}
this.durationInSeconds = durationInSeconds;
return this;
}
}
/**
* Specifies a landing page URL, together with a "player" (e.g. SWF)
*
* @param url the landing page URL
* @param playerUrl the URL of the "player" (e.g. SWF file)
* @param allowEmbed when specifying a player, you must specify whether embedding is allowed
*/
public GoogleVideoSitemapUrl(URL url, URL playerUrl, boolean allowEmbed) {
this(new Options(url, playerUrl, allowEmbed));
}
/**
* Specifies a landing page URL, together with the URL of the underlying video (e.g. FLV)
*
* @param url the landing page URL
* @param contentUrl the URL of the underlying video (e.g. FLV)
*/
public GoogleVideoSitemapUrl(URL url, URL contentUrl) {
this(new Options(url, contentUrl));
}
/**
* Configures the url with options
*/
public GoogleVideoSitemapUrl(Options options) {
super(options);
contentUrl = options.contentUrl;
playerUrl = options.playerUrl;
if (playerUrl == null && contentUrl == null) {
throw new RuntimeException("You must specify either contentUrl or playerUrl or both; neither were specified");
}
allowEmbed = convertBooleanToYesOrNo(options.allowEmbed);
if (playerUrl != null && allowEmbed == null) {
throw new RuntimeException("allowEmbed must be specified if playerUrl is specified");
}
category = options.category;
description = options.description;
durationInSeconds = options.durationInSeconds;
familyFriendly = convertBooleanToYesOrNo(options.familyFriendly);
publicationDate = options.publicationDate;
rating = options.rating;
tags = options.tags;
if (tags != null && tags.size() > 32) {
throw new RuntimeException("A maximum of 32 tags is permitted");
}
thumbnailUrl = options.thumbnailUrl;
title = options.title;
viewCount = options.viewCount;
}
private static String convertBooleanToYesOrNo(Boolean value) {
if (value == null) return null;
return value ? "Yes" : "No";
}
/**
* Retrieves the {@link Options#playerUrl}
*/
public URL getPlayerUrl() {
return playerUrl;
}
/**
* Retrieves the {@link Options#contentUrl}
*/
public URL getContentUrl() {
return contentUrl;
}
/**
* Retrieves the {@link Options#thumbnailUrl}
*/
public URL getThumbnailUrl() {
return thumbnailUrl;
}
/**
* Retrieves the {@link Options#title}
*/
public String getTitle() {
return title;
}
/**
* Retrieves the {@link Options#description}
*/
public String getDescription() {
return description;
}
/**
* Retrieves the {@link Options#rating}
*/
public Double getRating() {
return rating;
}
/**
* Retrieves the {@link Options#viewCount}
*/
public Integer getViewCount() {
return viewCount;
}
/**
* Retrieves the {@link Options#publicationDate}
*/
public Date getPublicationDate() {
return publicationDate;
}
/**
* Retrieves the {@link Options#tags}
*/
public ArrayList<String> getTags() {
return tags;
}
/**
* Retrieves the {@link Options#category}
*/
public String getCategory() {
return category;
}
/**
* Retrieves whether the video is {@link Options#familyFriendly}
*/
public String getFamilyFriendly() {
return familyFriendly;
}
/**
* Retrieves the {@link Options#durationInSeconds}
*/
public Integer getDurationInSeconds() {
return durationInSeconds;
}
/**
* Retrieves whether embedding is allowed
*/
public String getAllowEmbed() {
return allowEmbed;
}
}

View File

@ -12,283 +12,306 @@ import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;
abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGenerator<U,THIS>> {
/** 50000 URLs per sitemap maximum */
public static final int MAX_URLS_PER_SITEMAP = 50000;
private final URL baseUrl;
private final File baseDir;
private final String fileNamePrefix;
private final String fileNameSuffix;
private final boolean allowEmptySitemap;
private final boolean allowMultipleSitemaps;
private final ArrayList<U> urls = new ArrayList<U>();
private final W3CDateFormat dateFormat;
private final int maxUrls;
private final boolean autoValidate;
private final boolean gzip;
private final ISitemapUrlRenderer<U> renderer;
private int mapCount = 0;
private boolean finished = false;
private final ArrayList<File> outFiles = new ArrayList<File>();
public SitemapGenerator(AbstractSitemapGeneratorOptions<?> options, ISitemapUrlRenderer<U> renderer) {
baseDir = options.baseDir;
baseUrl = options.baseUrl;
fileNamePrefix = options.fileNamePrefix;
W3CDateFormat dateFormat = options.dateFormat;
if (dateFormat == null) dateFormat = new W3CDateFormat();
this.dateFormat = dateFormat;
allowEmptySitemap = options.allowEmptySitemap;
allowMultipleSitemaps = options.allowMultipleSitemaps;
maxUrls = options.maxUrls;
autoValidate = options.autoValidate;
gzip = options.gzip;
this.renderer = renderer;
abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGenerator<U, THIS>> {
/**
* 50000 URLs per sitemap maximum
*/
public static final int MAX_URLS_PER_SITEMAP = 50000;
if(options.suffixStringPattern != null && !options.suffixStringPattern.isEmpty()) {
fileNameSuffix = gzip ? options.suffixStringPattern + ".xml.gz" : options.suffixStringPattern + ".xml";
}
else {
fileNameSuffix = gzip ? ".xml.gz" : ".xml";
}
}
private final URL baseUrl;
private final File baseDir;
private final String fileNamePrefix;
private final String fileNameSuffix;
private final boolean allowEmptySitemap;
private final boolean allowMultipleSitemaps;
private final ArrayList<U> urls = new ArrayList<U>();
private final W3CDateFormat dateFormat;
private final int maxUrls;
private final boolean autoValidate;
private final boolean gzip;
private final ISitemapUrlRenderer<U> renderer;
private int mapCount = 0;
private boolean finished = false;
/** Add one URL of the appropriate type to this sitemap.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or else write out one sitemap immediately.
* @param url the URL to add to this sitemap
* @return this
*/
public THIS addUrl(U url) {
if (finished) throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps");
UrlUtils.checkUrl(url.getUrl(), baseUrl);
if (urls.size() == maxUrls) {
if (!allowMultipleSitemaps) throw new RuntimeException("More than " + maxUrls + " urls, but allowMultipleSitemaps is false. Enable allowMultipleSitemaps to split the sitemap into multiple files with a sitemap index.");
if (baseDir != null) {
if (mapCount == 0) mapCount++;
try {
writeSiteMap();
} catch(IOException ex) {
throw new RuntimeException("Closing of stream failed.", ex);
}
mapCount++;
urls.clear();
}
}
urls.add(url);
return getThis();
}
/** Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(Iterable<? extends U> urls) {
for (U url : urls) addUrl(url);
return getThis();
}
/** Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(U... urls) {
for (U url : urls) addUrl(url);
return getThis();
}
/** Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(String... urls) {
for (String url : urls) addUrl(url);
return getThis();
}
/** Add one URL of the appropriate type to this sitemap.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or else write out one sitemap immediately.
* @param url the URL to add to this sitemap
* @return this
*/
public THIS addUrl(String url) {
U sitemapUrl;
try {
sitemapUrl = renderer.getUrlClass().getConstructor(String.class).newInstance(url);
return addUrl(sitemapUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/** Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(URL... urls) {
for (URL url : urls) addUrl(url);
return getThis();
}
/** Add one URL of the appropriate type to this sitemap.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
* @param url the URL to add to this sitemap
* @return this
*/
public THIS addUrl(URL url) {
U sitemapUrl;
try {
sitemapUrl = renderer.getUrlClass().getConstructor(URL.class).newInstance(url);
return addUrl(sitemapUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
THIS getThis() {
return (THIS)this;
}
/** Write out remaining URLs; this method can only be called once. This is necessary so we can keep an accurate count for {@link #writeSitemapsWithIndex()}.
*
* @return a list of files we wrote out to disk
*/
public List<File> write() {
if (finished) throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps");
if (!allowEmptySitemap && urls.isEmpty() && mapCount == 0) throw new RuntimeException("No URLs added, sitemap would be empty; you must add some URLs with addUrls");
try {
writeSiteMap();
} catch (IOException ex) {
throw new RuntimeException("Closing of streams has failed at some point.", ex);
}
finished = true;
return outFiles;
}
/**
* Writes out the sitemaps as a list of strings.
* Each string in the list is a formatted list of URLs.
* We return a list because the URLs may not all fit --
* google specifies a maximum of 50,000 URLs in one sitemap.
* @return a list of XML-formatted strings
*/
public List<String> writeAsStrings() {
List<String> listOfSiteMapStrings = new ArrayList<String>();
for (int start = 0; start < urls.size(); start += maxUrls) {
int end = start + maxUrls;
if (end > urls.size()) {
end = urls.size();
}
StringBuilder sb = new StringBuilder();
writeSiteMapAsString(sb, urls.subList(start, end));
listOfSiteMapStrings.add(sb.toString());
}
return listOfSiteMapStrings;
}
private void writeSiteMapAsString(StringBuilder sb, List<U> urls) {
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" ");
if (renderer.getXmlNamespaces() != null) {
sb.append(renderer.getXmlNamespaces());
sb.append(' ');
}
sb.append(">\n");
for (U url : urls) {
renderer.render(url, sb, dateFormat);
}
sb.append("</urlset>");
}
/**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
* The sitemap index is written to {baseDir}/sitemap_index.xml
*/
public File writeSitemapsWithIndex() {
return writeSitemapsWithIndex(new File(baseDir, "sitemap_index.xml"));
}
private final ArrayList<File> outFiles = new ArrayList<File>();
/**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
*/
public String writeSitemapsWithIndexAsString() {
return prepareSitemapIndexGenerator(null).writeAsString();
}
public SitemapGenerator(AbstractSitemapGeneratorOptions<?> options, ISitemapUrlRenderer<U> renderer) {
baseDir = options.baseDir;
baseUrl = options.baseUrl;
fileNamePrefix = options.fileNamePrefix;
W3CDateFormat dateFormat = options.dateFormat;
if (dateFormat == null) dateFormat = new W3CDateFormat();
this.dateFormat = dateFormat;
allowEmptySitemap = options.allowEmptySitemap;
allowMultipleSitemaps = options.allowMultipleSitemaps;
maxUrls = options.maxUrls;
autoValidate = options.autoValidate;
gzip = options.gzip;
this.renderer = renderer;
/**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
*
* @param outFile the destination file of the sitemap index.
*/
public File writeSitemapsWithIndex(File outFile) {
prepareSitemapIndexGenerator(outFile).write();
return outFile;
}
if (options.suffixStringPattern != null && !options.suffixStringPattern.isEmpty()) {
fileNameSuffix = gzip ? options.suffixStringPattern + ".xml.gz" : options.suffixStringPattern + ".xml";
} else {
fileNameSuffix = gzip ? ".xml.gz" : ".xml";
}
}
private SitemapIndexGenerator prepareSitemapIndexGenerator(File outFile) {
if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first");
SitemapIndexGenerator sig;
sig = new SitemapIndexGenerator.Options(baseUrl, outFile).dateFormat(dateFormat).autoValidate(autoValidate).build();
sig.addUrls(fileNamePrefix, fileNameSuffix, mapCount);
return sig;
}
private void writeSiteMap() throws IOException {
if (baseDir == null) {
throw new NullPointerException("To write to files, baseDir must not be null");
}
if (urls.isEmpty() && (mapCount > 0 || !allowEmptySitemap)) return;
String fileNamePrefix;
if (mapCount > 0) {
fileNamePrefix = this.fileNamePrefix + mapCount;
} else {
fileNamePrefix = this.fileNamePrefix;
}
File outFile = new File(baseDir, fileNamePrefix+fileNameSuffix);
outFiles.add(outFile);
/**
* Add one URL of the appropriate type to this sitemap.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or else write out one sitemap immediately.
*
* @param url the URL to add to this sitemap
* @return this
*/
public THIS addUrl(U url) {
if (finished)
throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps");
UrlUtils.checkUrl(url.getUrl(), baseUrl);
if (urls.size() == maxUrls) {
if (!allowMultipleSitemaps)
throw new RuntimeException("More than " + maxUrls + " urls, but allowMultipleSitemaps is false. Enable allowMultipleSitemaps to split the sitemap into multiple files with a sitemap index.");
if (baseDir != null) {
if (mapCount == 0) mapCount++;
try {
writeSiteMap();
} catch (IOException ex) {
throw new RuntimeException("Closing of stream failed.", ex);
}
mapCount++;
urls.clear();
}
}
urls.add(url);
return getThis();
}
OutputStreamWriter out = null;
try {
if (gzip) {
FileOutputStream fileStream = new FileOutputStream(outFile);
GZIPOutputStream gzipStream = new GZIPOutputStream(fileStream);
out = new OutputStreamWriter(gzipStream, Charset.forName("UTF-8").newEncoder());
} else {
out = new OutputStreamWriter(new FileOutputStream(outFile), Charset.forName("UTF-8").newEncoder());
}
/**
* Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
*
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(Iterable<? extends U> urls) {
for (U url : urls) addUrl(url);
return getThis();
}
writeSiteMap(out);
out.flush();
/**
* Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
*
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(U... urls) {
for (U url : urls) addUrl(url);
return getThis();
}
/**
* Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
*
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(String... urls) {
for (String url : urls) addUrl(url);
return getThis();
}
/**
* Add one URL of the appropriate type to this sitemap.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or else write out one sitemap immediately.
*
* @param url the URL to add to this sitemap
* @return this
*/
public THIS addUrl(String url) {
U sitemapUrl;
try {
sitemapUrl = renderer.getUrlClass().getConstructor(String.class).newInstance(url);
return addUrl(sitemapUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Add multiple URLs of the appropriate type to this sitemap, one at a time.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
*
* @param urls the URLs to add to this sitemap
* @return this
*/
public THIS addUrls(URL... urls) {
for (URL url : urls) addUrl(url);
return getThis();
}
/**
* Add one URL of the appropriate type to this sitemap.
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
* or write out one sitemap immediately.
*
* @param url the URL to add to this sitemap
* @return this
*/
public THIS addUrl(URL url) {
U sitemapUrl;
try {
sitemapUrl = renderer.getUrlClass().getConstructor(URL.class).newInstance(url);
return addUrl(sitemapUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
THIS getThis() {
return (THIS) this;
}
/**
* Write out remaining URLs; this method can only be called once. This is necessary so we can keep an accurate count for {@link #writeSitemapsWithIndex()}.
*
* @return a list of files we wrote out to disk
*/
public List<File> write() {
if (finished)
throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps");
if (!allowEmptySitemap && urls.isEmpty() && mapCount == 0)
throw new RuntimeException("No URLs added, sitemap would be empty; you must add some URLs with addUrls");
try {
writeSiteMap();
} catch (IOException ex) {
throw new RuntimeException("Closing of streams has failed at some point.", ex);
}
finished = true;
return outFiles;
}
/**
* Writes out the sitemaps as a list of strings.
* Each string in the list is a formatted list of URLs.
* We return a list because the URLs may not all fit --
* google specifies a maximum of 50,000 URLs in one sitemap.
*
* @return a list of XML-formatted strings
*/
public List<String> writeAsStrings() {
List<String> listOfSiteMapStrings = new ArrayList<String>();
for (int start = 0; start < urls.size(); start += maxUrls) {
int end = start + maxUrls;
if (end > urls.size()) {
end = urls.size();
}
StringBuilder sb = new StringBuilder();
writeSiteMapAsString(sb, urls.subList(start, end));
listOfSiteMapStrings.add(sb.toString());
}
return listOfSiteMapStrings;
}
private void writeSiteMapAsString(StringBuilder sb, List<U> urls) {
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" ");
if (renderer.getXmlNamespaces() != null) {
sb.append(renderer.getXmlNamespaces());
sb.append(' ');
}
sb.append(">\n");
for (U url : urls) {
renderer.render(url, sb, dateFormat);
}
sb.append("</urlset>");
}
/**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
* The sitemap index is written to {baseDir}/sitemap_index.xml
*/
public File writeSitemapsWithIndex() {
return writeSitemapsWithIndex(new File(baseDir, "sitemap_index.xml"));
}
/**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
*
* @return
*/
public String writeSitemapsWithIndexAsString() {
return prepareSitemapIndexGenerator(null).writeAsString();
}
/**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
*
* @param outFile the destination file of the sitemap index.
*/
public File writeSitemapsWithIndex(File outFile) {
prepareSitemapIndexGenerator(outFile).write();
return outFile;
}
private SitemapIndexGenerator prepareSitemapIndexGenerator(File outFile) {
if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first");
SitemapIndexGenerator sig;
sig = new SitemapIndexGenerator.Options(baseUrl, outFile).dateFormat(dateFormat).autoValidate(autoValidate).build();
sig.addUrls(fileNamePrefix, fileNameSuffix, mapCount);
return sig;
}
private void writeSiteMap() throws IOException {
if (baseDir == null) {
throw new NullPointerException("To write to files, baseDir must not be null");
}
if (urls.isEmpty() && (mapCount > 0 || !allowEmptySitemap)) return;
String fileNamePrefix;
if (mapCount > 0) {
fileNamePrefix = this.fileNamePrefix + mapCount;
} else {
fileNamePrefix = this.fileNamePrefix;
}
File outFile = new File(baseDir, fileNamePrefix + fileNameSuffix);
outFiles.add(outFile);
OutputStreamWriter out = null;
try {
if (gzip) {
FileOutputStream fileStream = new FileOutputStream(outFile);
GZIPOutputStream gzipStream = new GZIPOutputStream(fileStream);
out = new OutputStreamWriter(gzipStream, Charset.forName("UTF-8").newEncoder());
} else {
out = new OutputStreamWriter(new FileOutputStream(outFile), Charset.forName("UTF-8").newEncoder());
}
writeSiteMap(out);
out.flush();
if (autoValidate) SitemapValidator.validateWebSitemap(outFile);
} catch (IOException e) {
throw new RuntimeException("Problem writing sitemap file " + outFile, e);
} catch (SAXException e) {
throw new RuntimeException("Sitemap file failed to validate (bug?)", e);
} finally {
if (out != null) {
out.close();
}
}
}
private void writeSiteMap(OutputStreamWriter out) throws IOException {
StringBuilder sb = new StringBuilder();
writeSiteMapAsString(sb, urls);
out.write(sb.toString());
}
if (autoValidate) SitemapValidator.validateWebSitemap(outFile);
} catch (IOException e) {
throw new RuntimeException("Problem writing sitemap file " + outFile, e);
} catch (SAXException e) {
throw new RuntimeException("Sitemap file failed to validate (bug?)", e);
} finally {
if(out != null) {
out.close();
}
}
}
private void writeSiteMap(OutputStreamWriter out) throws IOException {
StringBuilder sb = new StringBuilder();
writeSiteMapAsString(sb, urls);
out.write(sb.toString());
}
}

View File

@ -32,7 +32,7 @@ import java.util.TimeZone;
* <li>MILLISECOND: YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
* </ol>
*
* Note that W3C timezone designators (TZD) are either the letter "Z" (for GMT) or a pattern like "+00:30" or "-08:00". This is unlike
* <p>Note that W3C timezone designators (TZD) are either the letter "Z" (for GMT) or a pattern like "+00:30" or "-08:00". This is unlike
* RFC 822 timezones generated by SimpleDateFormat, which omit the ":" like this: "+0030" or "-0800".</p>
*
* <p>This class allows you to either specify which format pattern to use, or (by default) to

View File

@ -1,111 +0,0 @@
<html><head><title>How to use SitemapGen4j</title></head>
<body>
<h1>How to use SitemapGen4j</h1>
SitemapGen4j is a library to generate XML sitemaps in Java.
<h2>What's an XML sitemap?</h2>
Quoting from <a href="http://sitemaps.org/index.php">sitemaps.org</a>:
<blockquote><p>Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.</p>
<p>Web crawlers usually discover pages from links within the site and from other sites. Sitemaps supplement this data to allow crawlers that support Sitemaps to pick up all URLs in the Sitemap and learn about those URLs using the associated metadata. Using the Sitemap protocol does not guarantee that web pages are included in search engines, but provides hints for web crawlers to do a better job of crawling your site.</p>
<p>Sitemap 0.90 is offered under the terms of the Attribution-ShareAlike Creative Commons License and has wide adoption, including support from Google, Yahoo!, and Microsoft.</p>
</blockquote>
<h2>Getting started</h2>
<p>The easiest way to get started is to just use the WebSitemapGenerator class, like this:
<pre name="code" class="java">WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", myDir);
wsg.addUrl("http://www.example.com/index.html"); // repeat multiple times
wsg.write();</pre>
<h2>Configuring options</h2>
But there are a lot of nifty options available for URLs and for the generator as a whole. To configure the generator, use a builder:
<pre name="code" class="java">WebSitemapGenerator wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.gzip(true).build(); // enable gzipped output
wsg.addUrl("http://www.example.com/index.html");
wsg.write();</pre>
To configure the URLs, construct a WebSitemapUrl with WebSitemapUrl.Options.
<pre name="code" class="java">WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", myDir);
WebSitemapUrl url = new WebSitemapUrl.Options("http://www.example.com/index.html")
.lastMod(new Date()).priority(1.0).changeFreq(ChangeFreq.HOURLY).build();
// this will configure the URL with lastmod=now, priority=1.0, changefreq=hourly
wsg.addUrl(url);
wsg.write();</pre>
<h2>Configuring the date format</h2>
One important configuration option for the sitemap generator is the date format. The <a href="http://www.w3.org/TR/NOTE-datetime">W3C datetime standard</a> allows you to choose the precision of your datetime (anything from just specifying the year like "1997" to specifying the fraction of the second like "1997-07-16T19:20:30.45+01:00"); if you don't specify one, we'll try to guess which one you want, and we'll use the default timezone of the local machine, which might not be what you prefer.
<pre name="code" class="java">
// Use DAY pattern (2009-02-07), Greenwich Mean Time timezone
W3CDateFormat dateFormat = new W3CDateFormat(Pattern.DAY);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
WebSitemapGenerator wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.dateFormat(dateFormat).build(); // actually use the configured dateFormat
wsg.addUrl("http://www.example.com/index.html");
wsg.write();</pre>
<h2>Lots of URLs: a sitemap index file</h2>
One sitemap can contain a maximum of 50,000 URLs. (Some sitemaps, like Google News sitemaps, can contain only 1,000 URLs.) If you need to put more URLs than that in a sitemap, you'll have to use a sitemap index file. Fortunately, WebSitemapGenerator can manage the whole thing for you.
<pre name="code" class="java">WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", myDir);
for (int i = 0; i &lt; 60000; i++) wsg.addUrl("http://www.example.com/doc"+i+".html");
wsg.write();
wsg.writeSitemapsWithIndex(); // generate the sitemap_index.xml
</pre>
<p>That will generate two sitemaps for 60K URLs: sitemap1.xml (with 50K urls) and sitemap2.xml (with the remaining 10K), and then generate a sitemap_index.xml file describing the two.</p>
<p>It's also possible to carefully organize your sub-sitemaps. For example, it's recommended to group URLs with the same changeFreq together (have one sitemap for changeFreq "daily" and another for changeFreq "yearly"), so you can modify the lastMod of the daily sitemap without modifying the lastMod of the yearly sitemap. To do that, just construct your sitemaps one at a time using the WebSitemapGenerator, then use the SitemapIndexGenerator to create a single index for all of them.</p>
<pre name="code" class="java">WebSitemapGenerator wsg;
// generate foo sitemap
wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.fileNamePrefix("foo").build();
for (int i = 0; i &lt; 5; i++) wsg.addUrl("http://www.example.com/foo"+i+".html");
wsg.write();
// generate bar sitemap
wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.fileNamePrefix("bar").build();
for (int i = 0; i &lt; 5; i++) wsg.addUrl("http://www.example.com/bar"+i+".html");
wsg.write();
// generate sitemap index for foo + bar
SitemapIndexGenerator sig = new SitemapIndexGenerator("http://www.example.com", myFile);
sig.addUrl("http://www.example.com/foo.xml");
sig.addUrl("http://www.example.com/bar.xml");
sig.write();</pre>
<p>You could also use the SitemapIndexGenerator to incorporate sitemaps generated by other tools. For example, you might use Google's official Python sitemap generator to generate some sitemaps, and use WebSitemapGenerator to generate some sitemaps, and use SitemapIndexGenerator to make an index of all of them.</p>
<h2>Validate your sitemaps</h2>
<p>SitemapGen4j can also validate your sitemaps using the official XML Schema Definition (XSD). If you used SitemapGen4j to make the sitemaps, you shouldn't need to do this unless there's a bug in our code. But you can use it to validate sitemaps generated by other tools, and it provides an extra level of safety.</p>
<p>It's easy to configure the WebSitemapGenerator to automatically validate your sitemaps right after you write them (but this does slow things down, naturally).</p>
<pre name="code" class="java">WebSitemapGenerator wsg = WebSitemapGenerator.builder("http://www.example.com", myDir)
.autoValidate(true).build(); // validate the sitemap after writing
wsg.addUrl("http://www.example.com/index.html");
wsg.write();</pre>
<p>You can also use the SitemapValidator directly to manage sitemaps. It has two methods: validateWebSitemap(File f) and validateSitemapIndex(File f).</p>
<h2>Google-specific sitemaps</h2>
<p>Google can understand a wide variety of custom sitemap formats that they made up, including a Mobile sitemaps, Geo sitemaps, Code sitemaps (for Google Code search), Google News sitemaps, and Video sitemaps. SitemapGen4j can generate any/all of these different types of sitemaps.</p>
<p>To generate a special type of sitemap, just use GoogleMobileSitemapGenerator, GoogleGeoSitemapGenerator, GoogleCodeSitemapGenerator, GoogleCodeSitemapGenerator, GoogleNewsSitemapGenerator, or GoogleVideoSitemapGenerator instead of WebSitemapGenerator.</p>
<p>You can't mix-and-match regular URLs with Google-specific sitemaps, so you'll also have to use a GoogleMobileSitemapUrl, GoogleGeoSitemapUrl, GoogleCodeSitemapUrl, GoogleNewsSitemapUrl, or GoogleVideoSitemapUrl instead of a WebSitemapUrl. Each of them has unique configurable options not available to regular web URLs.</p>
</body>
</html>