Project

General

Profile

Revision 7764

Added by Matt Jones almost 8 years ago

Mofdified Sitemap class to also generate the sitemap index file that is needed when more than one sitemap file is provided.

View differences:

src/edu/ucsb/nceas/metacat/Sitemap.java
25 25
package edu.ucsb.nceas.metacat;
26 26

  
27 27
import java.io.File;
28
import java.io.FileOutputStream;
28 29
import java.io.FileWriter;
29 30
import java.io.IOException;
31
import java.io.OutputStreamWriter;
30 32
import java.io.Writer;
33
import java.nio.charset.Charset;
31 34
import java.sql.PreparedStatement;
32 35
import java.sql.ResultSet;
33 36
import java.sql.SQLException;
......
131 134
                // Loop through all of the documents, and write them to a
132 135
                // sitemap
133 136
                File sitemapFile = null;
134
                FileWriter sitemap = null;
137
                OutputStreamWriter sitemap = null;
135 138
                int counter = 0;
136 139
                int fileNumber = 0;
137 140
                while (rs.next()) {
......
148 151
                        fileNumber++;
149 152
                        sitemapFile = new File(directory, fileRoot + fileNumber
150 153
                                + ".xml");
151
                        sitemap = new FileWriter(sitemapFile);
154
                        sitemap = new OutputStreamWriter(new FileOutputStream(sitemapFile), Charset.forName("UTF-8"));
152 155

  
153 156
                        // Write the sitemap document header for the new file
154 157
                        writeSitemapHeader(sitemap);
......
162 165
                }
163 166
                stmt.close();
164 167
                writeSitemapFooter(sitemap);
168
                writeSitemapIndex(fileNumber);
165 169
            } catch (SQLException e) {
166 170
                logMetacat.warn("Error while writing to the sitemap file: "
167 171
                        + e.getMessage());
......
195 199
        String header = "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n" +
196 200
                "xmlns:sm=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n" +
197 201
                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
198
                "xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";
202
                "xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n";
199 203
        
200 204
        sitemap.write(header);
201 205
        sitemap.flush();
......
259 263
        }
260 264
    }
261 265

  
266
    /**
267
     * Create an index file listing all of the sitemap files that were created.
268
     * @param fileNumber the number of sitemap files that were created.
269
     */
270
    private void writeSitemapIndex(int fileNumber) {
271
        
272
        // Open a new sitemapIndex file for writing
273
        File sitemapIndexFile = null;
274
        OutputStreamWriter sitemapIndex = null;
275
        sitemapIndexFile = new File(directory, indexFilename);
276
        try {
277
            sitemapIndex = new OutputStreamWriter(new FileOutputStream(sitemapIndexFile), Charset.forName("UTF-8"));
278

  
279
            // Write the sitemap index header for the new file
280
            sitemapIndex.write(PROLOG);
281
            String header = "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n"
282
                    + "xmlns:sm=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n" + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
283
                    + "xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd\">\n";
284
            sitemapIndex.write(header);
285
            sitemapIndex.flush();
286

  
287
            // Write out one index entry for each sitemap file
288
            for (int fn = 1; fn <= fileNumber; fn++) {
289
                String filename = fileRoot + fileNumber + ".xml";
290
                writeSitemapIndexEntry(sitemapIndex, filename);
291
            }
292

  
293
            // Write the sitemap index footer content
294
            if (sitemapIndex != null) {
295
                String footer = "</sitemapindex>\n";
296
                sitemapIndex.write(footer);
297
                sitemapIndex.close();
298
            }
299

  
300
            // Close the index file
301
            if (sitemapIndex != null) {
302
                sitemapIndex.close();
303
            }
304

  
305
        } catch (IOException e) {
306
            logMetacat.warn("Could not open or write to the sitemap index file." + e.getMessage());
307
        }
308
    }
309
    
310
    /**
311
     * Write a single line of the sitemap index file containing the URL to a specific sitemap file.
312
     * @param sitemapIndex the writer to which the index information is written
313
     * @param filename the name of the index file to be used
314
     * @throws IOException on error writing to the index file 
315
     */
316
    private void writeSitemapIndexEntry(Writer sitemapIndex, String filename)
317
            throws IOException {
318
        if (sitemapIndex != null && filename != null && urlRoot != null) {
319
            StringBuffer url = new StringBuffer();
320
            url.append(urlRoot);
321
            if (!urlRoot.endsWith("/")) {
322
                url.append("/");
323
            }
324
            url.append(filename);
325
            sitemapIndex.write("<sitemap><loc>");
326
            sitemapIndex.write(url.toString());
327
            sitemapIndex.write("</loc>");
328
            sitemapIndex.write("</sitemap>");
329
            sitemapIndex.write("\n");
330
            sitemapIndex.flush();
331
        }
332
    }
333
    
262 334
    // Member variables
263 335

  
264 336
    /** The directory in which sitemaps are written. */
......
275 347

  
276 348
    /** The root name to be used in naming sitemap files. */
277 349
    static final String fileRoot = "metacat";
350
    
351
    /** The name to give to the sitemap index file */
352
    static final String indexFilename = "metacatSitemapIndex.xml";
278 353

  
279 354
    /** A String constant containing the XML prolog to be written in files. */
280 355
    static final String PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";

Also available in: Unified diff