[Schmitzm-commits] r1317 - in trunk: src/schmitzm/geotools/styling src/schmitzm/mail src/skrueger src_junit/skrueger
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Dec 1 00:37:59 CET 2010
Author: alfonx
Date: 2010-12-01 00:37:57 +0100 (Wed, 01 Dec 2010)
New Revision: 1317
Added:
trunk/src/skrueger/Sitemap.java
trunk/src_junit/skrueger/SitemapUtilTest.java
Modified:
trunk/src/schmitzm/geotools/styling/StylingUtil.java
trunk/src/schmitzm/mail/EmailThread.java
Log:
Added a first version of sitemap.xml support to immodb. Sitemap.java is in schmitzm
Modified: trunk/src/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/styling/StylingUtil.java 2010-11-30 22:03:52 UTC (rev 1316)
+++ trunk/src/schmitzm/geotools/styling/StylingUtil.java 2010-11-30 23:37:57 UTC (rev 1317)
@@ -3118,7 +3118,7 @@
/**
* Converts a {@link StyledLayerDescriptor} or any of it's subcomponents to
- * XML
+ * XML.
*
* @throws TransformerException
*/
Modified: trunk/src/schmitzm/mail/EmailThread.java
===================================================================
--- trunk/src/schmitzm/mail/EmailThread.java 2010-11-30 22:03:52 UTC (rev 1316)
+++ trunk/src/schmitzm/mail/EmailThread.java 2010-11-30 23:37:57 UTC (rev 1317)
@@ -2,6 +2,9 @@
import org.apache.log4j.Logger;
+/**
+ * Send an Email on another {@link Thread}.
+ */
public class EmailThread extends Thread {
static final Logger log = Logger.getLogger(EmailThread.class);
Added: trunk/src/skrueger/Sitemap.java
===================================================================
--- trunk/src/skrueger/Sitemap.java 2010-11-30 22:03:52 UTC (rev 1316)
+++ trunk/src/skrueger/Sitemap.java 2010-11-30 23:37:57 UTC (rev 1317)
@@ -0,0 +1,178 @@
+package skrueger;
+
+import java.io.FileWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Date;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * This class provides an easy way to list all pages of your site (including
+ * generates ones) ina sitemap.xml. See http://sitemaps.org/protocol.php for the
+ * XML specification.
+ *
+ * <br/>
+ * XML output is based on org.w3c.dom
+ *
+ *
+ * @author Stefan Tzeggai
+ */
+public class Sitemap {
+
+ Logger log = Logger.getLogger(Sitemap.class);
+
+ private Document document;
+
+ final private Element urlsetElement;
+
+ /**
+ * Returns the number of URLs. May not be more than 50,000 by spec.
+ */
+ private int size = 0;
+
+ final static String NSURL = "http://www.sitemaps.org/schemas/sitemap/0.9";
+
+ public enum CHANGEFREQ {
+ monthly, daily, weekly
+ }
+
+ public Sitemap() {
+
+ try {
+
+ // Create a DOM builder and parse the fragment
+ final DocumentBuilderFactory factory = DocumentBuilderFactory
+ .newInstance();
+ document = factory.newDocumentBuilder().newDocument();
+
+ // XML root element
+ urlsetElement = document.createElementNS(NSURL, "urlset");
+ document.appendChild(urlsetElement);
+
+ // // Linking this XML to the AtlasML Schema
+ // final Attr namespaces = document.createAttributeNS(
+ // "http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
+ // namespaces
+ // .setValue(NSURL+" http://localhost:"
+ // + Webserver.DEFAULTPORT
+ // + "/skrueger/atlas/resource/AtlasML.xsd");
+ // urlset.setAttributeNode(namespaces);
+
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Sitemap org.w3c.xml newDocumentBuilder failed:", e);
+ }
+ }
+
+ /**
+ *
+ * @param locString
+ * @param lastmod
+ * when was the page modified the last time? May be
+ * <code>null</code>.
+ * @param changefreq
+ * How often does the page change. May be <code>null</code>.
+ * @param priority
+ * Priority 0.0 - 1.0. May be <code>null</code>.
+ * @return <code>true</code> if the {@link Sitemap} contains less than 50000
+ * urls and the page was added.
+ */
+ public boolean addUrl(String locString, Date lastmod,
+ CHANGEFREQ changefreq, Double priority) {
+ size++;
+ if (size >= 50000)
+ return false;
+
+ Element urlElement = document.createElement("url");
+
+ if (locString == null)
+ throw new IllegalArgumentException("location must be provided");
+ else {
+ Element e = document.createElement("loc");
+ e.appendChild(document.createTextNode(locString));
+ urlElement.appendChild(e);
+ }
+
+ if (lastmod != null) {
+ Element e = document.createElement("lastmod");
+ e.appendChild(document.createTextNode(lastmod.toString()));
+ urlElement.appendChild(e);
+ }
+
+ if (changefreq != null) {
+ Element e = document.createElement("changefreq");
+ e.appendChild(document.createTextNode(changefreq.toString()));
+ urlElement.appendChild(e);
+ }
+
+ if (priority != null) {
+ Element e = document.createElement("priority");
+ e.appendChild(document.createTextNode(priority.toString()));
+ urlElement.appendChild(e);
+ }
+ urlsetElement.appendChild(urlElement);
+ return true;
+ }
+
+ public Document getDocument() {
+ return document;
+ }
+
+ public String getXmlString() {
+ StringWriter stringWriter = new StringWriter();
+ outputToWriter(stringWriter);
+ return stringWriter.toString();
+ }
+
+ /**
+ * Can be used to write to a File with {@link FileWriter}.
+ */
+ public void outputToWriter(Writer stringWriter) {
+ try {
+
+ try { // close outputStreamWriter.close();
+
+ // ****************************************************************************
+ // Create the XML
+ // ****************************************************************************
+ final Result result = new StreamResult(stringWriter);
+
+ // with indenting to make it human-readable
+ final TransformerFactory tf = TransformerFactory.newInstance();
+
+ // TODO Ging mit xerces, geht nicht mehr mit xalan ?!
+ // tf.setAttribute("indent-number", new Integer(2));
+
+ final Transformer xformer = tf.newTransformer();
+ xformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ xformer.setOutputProperty(
+ "{http://xml.apache.org/xalan}indent-amount", "2");
+
+ // Write the DOM document to the file
+ xformer.transform(new DOMSource(document), result);
+
+ } finally {
+ stringWriter.close();
+ }
+
+ } catch (Exception e) {
+ log.error("Failed to create sitemap.XML-String", e);
+ throw new RuntimeException(e);
+ }
+ }
+
+ public int getSize() {
+ return size;
+ }
+}
Property changes on: trunk/src/skrueger/Sitemap.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
Added: trunk/src_junit/skrueger/SitemapUtilTest.java
===================================================================
--- trunk/src_junit/skrueger/SitemapUtilTest.java 2010-11-30 22:03:52 UTC (rev 1316)
+++ trunk/src_junit/skrueger/SitemapUtilTest.java 2010-11-30 23:37:57 UTC (rev 1317)
@@ -0,0 +1,20 @@
+package skrueger;
+
+import org.junit.Test;
+
+import schmitzm.junit.TestingClass;
+import skrueger.Sitemap.CHANGEFREQ;
+
+public class SitemapUtilTest extends TestingClass {
+
+
+ @Test
+ public void testGetDocument() {
+ Sitemap s = new Sitemap();
+ s.addUrl("http://www.wikisquare.de/UeberUns", null, CHANGEFREQ.monthly, 0.6);
+ s.addUrl("http://www.wikisquare.de/publications", null, null, null);
+ String xml = s.getXmlString();
+ System.out.println(xml);
+ }
+
+}
Property changes on: trunk/src_junit/skrueger/SitemapUtilTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
More information about the Schmitzm-commits
mailing list