View Javadoc

1   package uk.ac.ebi.intenz.tools.export;
2   
3   import java.io.OutputStream;
4   import java.math.BigDecimal;
5   import java.text.SimpleDateFormat;
6   import java.util.Collection;
7   import java.util.Date;
8   
9   import javax.xml.XMLConstants;
10  import javax.xml.bind.JAXBContext;
11  import javax.xml.bind.JAXBException;
12  import javax.xml.bind.Marshaller;
13  import javax.xml.validation.Schema;
14  import javax.xml.validation.SchemaFactory;
15  
16  import org.sitemaps.ObjectFactory;
17  import org.sitemaps.Url;
18  import org.sitemaps.Urlset;
19  import org.xml.sax.SAXException;
20  
21  /**
22   * Exports IntEnz as a
23   * <a href="http://www.google.com/webmasters/sitemaps">sitemap XML file</a>
24   * @author rafalcan
25   *
26   */
27  public class SitemapExporter {
28  
29  	private Marshaller marshaller;
30      private ObjectFactory of;
31      private final String INTENZ_URL = "http://www.ebi.ac.uk/intenz/";
32  
33      public SitemapExporter() throws JAXBException, SAXException{
34          JAXBContext context = JAXBContext.newInstance("org.sitemaps");
35          marshaller = context.createMarshaller();
36          marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
37  		Schema sitemapXsd = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
38  			.newSchema(SitemapExporter.class.getClassLoader().getResource("sitemap.xsd"));
39  		marshaller.setSchema(sitemapXsd);
40          of = new ObjectFactory();
41  	}
42  
43      /**
44       * Dumps a list of URLs as sitemap (see {@link http://www.sitemaps.org})
45       * @param urls List of indexed URLs as Strings.
46       * @param os
47       * @throws JAXBException in case of marshalling error
48       */
49  	public void export(Collection<String> urls, OutputStream os) throws JAXBException {
50  		final String CHANGE_FREQ = "weekly";
51  		Urlset urlset = of.createUrlset();
52  		Url intenzUrl = of.createUrl();
53  		intenzUrl.setLoc(INTENZ_URL);
54  		intenzUrl.setLastmod(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
55  		intenzUrl.setChangefreq(CHANGE_FREQ);
56  		intenzUrl.setPriority(new BigDecimal(0.8).setScale(1,BigDecimal.ROUND_DOWN));
57  		urlset.getUrl().add(intenzUrl);
58  		for (String url : urls) {
59  			Url pageUrl = of.createUrl();
60  			pageUrl.setLoc(url);
61  			pageUrl.setChangefreq(CHANGE_FREQ);
62  			urlset.getUrl().add(pageUrl);
63  		}
64  		marshaller.marshal(urlset, os);
65  	}
66  
67  }