/[schmitzm]/trunk/src/skrueger/Sitemap.java
ViewVC logotype

Annotation of /trunk/src/skrueger/Sitemap.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1319 - (hide annotations)
Wed Dec 1 01:09:52 2010 UTC (14 years, 3 months ago) by alfonx
File MIME type: text/plain
File size: 5431 byte(s)
Added automatic sumbitting of sitemap.xml .. untested
1 alfonx 1317 package skrueger;
2    
3     import java.io.FileWriter;
4 alfonx 1319 import java.io.IOException;
5 alfonx 1317 import java.io.StringWriter;
6     import java.io.Writer;
7 alfonx 1319 import java.net.MalformedURLException;
8     import java.net.URL;
9 alfonx 1317 import java.util.Date;
10    
11     import javax.xml.parsers.DocumentBuilderFactory;
12     import javax.xml.transform.OutputKeys;
13     import javax.xml.transform.Result;
14     import javax.xml.transform.Transformer;
15     import javax.xml.transform.TransformerFactory;
16     import javax.xml.transform.dom.DOMSource;
17     import javax.xml.transform.stream.StreamResult;
18    
19     import org.apache.log4j.Logger;
20     import org.w3c.dom.Document;
21     import org.w3c.dom.Element;
22    
23     /**
24     * This class provides an easy way to list all pages of your site (including
25     * generates ones) ina sitemap.xml. See http://sitemaps.org/protocol.php for the
26     * XML specification.
27     *
28     * <br/>
29     * XML output is based on org.w3c.dom
30     *
31     *
32     * @author Stefan Tzeggai
33     */
34     public class Sitemap {
35    
36 alfonx 1319 static String[] SEARCHENGINES = new String[] { "http://www.sitemapwriter.com/notify.php?crawler=all&url=" };
37 alfonx 1317
38 alfonx 1319 final static Logger log = Logger.getLogger(Sitemap.class);
39    
40 alfonx 1317 private Document document;
41    
42     final private Element urlsetElement;
43    
44     /**
45     * Returns the number of URLs. May not be more than 50,000 by spec.
46     */
47     private int size = 0;
48    
49     final static String NSURL = "http://www.sitemaps.org/schemas/sitemap/0.9";
50    
51     public enum CHANGEFREQ {
52     monthly, daily, weekly
53     }
54    
55     public Sitemap() {
56    
57     try {
58    
59     // Create a DOM builder and parse the fragment
60     final DocumentBuilderFactory factory = DocumentBuilderFactory
61     .newInstance();
62     document = factory.newDocumentBuilder().newDocument();
63    
64     // XML root element
65     urlsetElement = document.createElementNS(NSURL, "urlset");
66     document.appendChild(urlsetElement);
67    
68     // // Linking this XML to the AtlasML Schema
69     // final Attr namespaces = document.createAttributeNS(
70     // "http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
71     // namespaces
72     // .setValue(NSURL+" http://localhost:"
73     // + Webserver.DEFAULTPORT
74     // + "/skrueger/atlas/resource/AtlasML.xsd");
75     // urlset.setAttributeNode(namespaces);
76    
77     } catch (Exception e) {
78     throw new RuntimeException(
79     "Sitemap org.w3c.xml newDocumentBuilder failed:", e);
80     }
81     }
82    
83     /**
84     *
85     * @param locString
86     * @param lastmod
87     * when was the page modified the last time? May be
88     * <code>null</code>.
89     * @param changefreq
90     * How often does the page change. May be <code>null</code>.
91     * @param priority
92     * Priority 0.0 - 1.0. May be <code>null</code>.
93     * @return <code>true</code> if the {@link Sitemap} contains less than 50000
94     * urls and the page was added.
95     */
96     public boolean addUrl(String locString, Date lastmod,
97     CHANGEFREQ changefreq, Double priority) {
98     size++;
99     if (size >= 50000)
100     return false;
101    
102     Element urlElement = document.createElement("url");
103    
104     if (locString == null)
105     throw new IllegalArgumentException("location must be provided");
106     else {
107     Element e = document.createElement("loc");
108     e.appendChild(document.createTextNode(locString));
109     urlElement.appendChild(e);
110     }
111    
112     if (lastmod != null) {
113     Element e = document.createElement("lastmod");
114     e.appendChild(document.createTextNode(lastmod.toString()));
115     urlElement.appendChild(e);
116     }
117    
118     if (changefreq != null) {
119     Element e = document.createElement("changefreq");
120     e.appendChild(document.createTextNode(changefreq.toString()));
121     urlElement.appendChild(e);
122     }
123    
124     if (priority != null) {
125     Element e = document.createElement("priority");
126     e.appendChild(document.createTextNode(priority.toString()));
127     urlElement.appendChild(e);
128     }
129     urlsetElement.appendChild(urlElement);
130     return true;
131     }
132    
133     public Document getDocument() {
134     return document;
135     }
136    
137     public String getXmlString() {
138     StringWriter stringWriter = new StringWriter();
139     outputToWriter(stringWriter);
140     return stringWriter.toString();
141     }
142    
143     /**
144     * Can be used to write to a File with {@link FileWriter}.
145     */
146     public void outputToWriter(Writer stringWriter) {
147     try {
148    
149     try { // close outputStreamWriter.close();
150    
151     // ****************************************************************************
152     // Create the XML
153     // ****************************************************************************
154     final Result result = new StreamResult(stringWriter);
155    
156     // with indenting to make it human-readable
157     final TransformerFactory tf = TransformerFactory.newInstance();
158    
159 alfonx 1319 // TODO Ging mit xerces, geht nicht mehr mit xalan ?!
160 alfonx 1317 // tf.setAttribute("indent-number", new Integer(2));
161    
162     final Transformer xformer = tf.newTransformer();
163     xformer.setOutputProperty(OutputKeys.INDENT, "yes");
164     xformer.setOutputProperty(
165     "{http://xml.apache.org/xalan}indent-amount", "2");
166    
167     // Write the DOM document to the file
168     xformer.transform(new DOMSource(document), result);
169    
170     } finally {
171     stringWriter.close();
172     }
173    
174     } catch (Exception e) {
175     log.error("Failed to create sitemap.XML-String", e);
176     throw new RuntimeException(e);
177     }
178     }
179    
180     public int getSize() {
181     return size;
182     }
183 alfonx 1319
184     public static void submitToSearchEngines(String urlToSitemap) {
185     for (String se : SEARCHENGINES) {
186     log.info("Submitting " + urlToSitemap + " to " + se);
187     try {
188     URL url = new URL(se + urlToSitemap);
189    
190     url.openConnection().connect();
191    
192     } catch (Exception e) {
193     log.error("failed to submit " + urlToSitemap + " to " + se, e);
194     }
195     }
196     }
197 alfonx 1317 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26