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
23
24
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
45
46
47
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 }