BrightSide Workbench Full Report + Source Code
SitemapGenerator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.sitemap;
20 
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.apache.commons.io.output.FileWriterWithEncoding;
26 import org.apache.commons.text.StringEscapeUtils;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.impl.util.Files;
29 
34 public class SitemapGenerator {
35 
36  private final FileWriterWithEncoding writer;
37 
38  public SitemapGenerator() throws IOException {
39  this.writer = new FileWriterWithEncoding(ElephantContext.getRealPath("/sitemap.xml"), "UTF-8");
40  }
41 
42  public static void generateSitemap() {
43  try {
44  new SitemapGenerator().generate();
45  } catch (IOException ex) {
46  Logger.getLogger(SitemapGenerator.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
47  }
48  }
49 
50  public void generate() throws IOException {
51  Collection<Sitemaps.SitemapLink> links = Sitemaps.getSitemapLinks();
52  writeHeader();
53  for(Sitemaps.SitemapLink link : links) {
54  writeEntry(link.getUrl(), link.getImages());
55  }
56  writeFooter();
57  writer.close();
58  }
59 
60  private void writeEntry(String url, Collection<String> imgs) throws IOException {
61  url = StringEscapeUtils.escapeHtml4(url);
62  writer.write(
63  " <url> \n" +
64  " <loc>" + ElephantContext.getServerBase("http") + url + "</loc> \n");
65  if(imgs != null) for(String img : imgs) {
66  img = StringEscapeUtils.escapeHtml4(img);
67  writer.write(
68  " <image:image>\n" +
69  " <image:loc>" + ElephantContext.getServerBase("http") + img + "</image:loc>\n" +
70  " <image:caption>" + Files.file(img).getName() + "</image:caption>\n" +
71  " </image:image>\n");
72  }
73  writer.write(" </url>\n");
74  }
75 
76  private void writeHeader() throws IOException {
77  writer.write(
78  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
79  "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" \n" +
80  " xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" \n" +
81  " xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\">\n");
82  }
83 
84  private void writeFooter() throws IOException {
85  writer.write("</urlset>");
86  }
87 
88 }
static String getServerBase(String scheme)
static File file(IConstructor constructor, String file)
Definition: Files.java:71
static Collection< SitemapLink > getSitemapLinks()
Definition: Sitemaps.java:60