From 191381492e041aaf145c110ef84d490ecfd5883c Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 13 Jan 2004 00:23:43 +0000 Subject: [PATCH] Initial import. --- google-taglib/rss/.cvsignore | 3 + google-taglib/rss/GoogleRSS.iml | 44 ++ google-taglib/rss/GoogleRSS.ipr | 93 +++ google-taglib/rss/GoogleRSS.iws | 747 ++++++++++++++++++ google-taglib/rss/LICENSE.txt | 30 + google-taglib/rss/README.txt | 71 ++ google-taglib/rss/build.properties | 27 + google-taglib/rss/build.xml | 112 +++ .../rss/google-rss-examples/README.txt | 66 ++ .../rss/google-rss-examples/WEB-INF/web.xml | 42 + .../rss/google-rss-examples/index.html | 71 ++ .../rss/google-rss-examples/key.html | 16 + google-taglib/rss/google-rss-examples/key.jsp | 21 + google-taglib/rss/google-rss-examples/rss.jsp | 1 + .../rss/google-rss-examples/search.html | 16 + .../clevercactus/google/AtomGenerator.java | 222 ++++++ .../google/FeedGenerationException.java | 68 ++ .../clevercactus/google/FeedGenerator.java | 172 ++++ .../google/FeedGeneratorFactory.java | 84 ++ .../google/GoogleResultsWrapper.java | 174 ++++ .../google/GoogleResultsWrapperItem.java | 82 ++ .../com/clevercactus/google/RSSGenerator.java | 198 +++++ .../google/taglibs/SearchResults.java | 132 ++++ google-taglib/rss/tlds/google-rss.tld | 22 + 24 files changed, 2514 insertions(+) create mode 100644 google-taglib/rss/.cvsignore create mode 100644 google-taglib/rss/GoogleRSS.iml create mode 100644 google-taglib/rss/GoogleRSS.ipr create mode 100644 google-taglib/rss/GoogleRSS.iws create mode 100644 google-taglib/rss/LICENSE.txt create mode 100644 google-taglib/rss/README.txt create mode 100644 google-taglib/rss/build.properties create mode 100644 google-taglib/rss/build.xml create mode 100644 google-taglib/rss/google-rss-examples/README.txt create mode 100644 google-taglib/rss/google-rss-examples/WEB-INF/web.xml create mode 100644 google-taglib/rss/google-rss-examples/index.html create mode 100644 google-taglib/rss/google-rss-examples/key.html create mode 100644 google-taglib/rss/google-rss-examples/key.jsp create mode 100644 google-taglib/rss/google-rss-examples/rss.jsp create mode 100644 google-taglib/rss/google-rss-examples/search.html create mode 100644 google-taglib/rss/src/com/clevercactus/google/AtomGenerator.java create mode 100644 google-taglib/rss/src/com/clevercactus/google/FeedGenerationException.java create mode 100644 google-taglib/rss/src/com/clevercactus/google/FeedGenerator.java create mode 100644 google-taglib/rss/src/com/clevercactus/google/FeedGeneratorFactory.java create mode 100644 google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapper.java create mode 100644 google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapperItem.java create mode 100644 google-taglib/rss/src/com/clevercactus/google/RSSGenerator.java create mode 100644 google-taglib/rss/src/com/clevercactus/google/taglibs/SearchResults.java create mode 100644 google-taglib/rss/tlds/google-rss.tld diff --git a/google-taglib/rss/.cvsignore b/google-taglib/rss/.cvsignore new file mode 100644 index 0000000..3586368 --- /dev/null +++ b/google-taglib/rss/.cvsignore @@ -0,0 +1,3 @@ +build +dist +*.bat \ No newline at end of file diff --git a/google-taglib/rss/GoogleRSS.iml b/google-taglib/rss/GoogleRSS.iml new file mode 100644 index 0000000..36999e8 --- /dev/null +++ b/google-taglib/rss/GoogleRSS.iml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/google-taglib/rss/GoogleRSS.ipr b/google-taglib/rss/GoogleRSS.ipr new file mode 100644 index 0000000..f2bee3d --- /dev/null +++ b/google-taglib/rss/GoogleRSS.ipr @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/google-taglib/rss/GoogleRSS.iws b/google-taglib/rss/GoogleRSS.iws new file mode 100644 index 0000000..81df5c0 --- /dev/null +++ b/google-taglib/rss/GoogleRSS.iws @@ -0,0 +1,747 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/google-taglib/rss/LICENSE.txt b/google-taglib/rss/LICENSE.txt new file mode 100644 index 0000000..001be53 --- /dev/null +++ b/google-taglib/rss/LICENSE.txt @@ -0,0 +1,30 @@ +Google RSS Tag Library License + +Copyright (c) 2003-2004, clevercactus ltd. +Copyright (c) 2003-2004, Erik C. Thauvin (erik@thauvin.net) +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +Neither the name of the author nor the names of its contributors may be used to +endorse or promote products derived from this software without specific prior +written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/google-taglib/rss/README.txt b/google-taglib/rss/README.txt new file mode 100644 index 0000000..65a8552 --- /dev/null +++ b/google-taglib/rss/README.txt @@ -0,0 +1,71 @@ +Google RSS Tag Library README +============================= + + +Overview +======== + +This README file describes the Google RSS Tag Library distribution. + +The Google RSS Tag Library is an extension to, and thus requires, +the Google Tag Library, which can be found at: + + + + +See the "LICENSE.txt" file for licensing details. + +The Google RSS Tag Library uses the Google Web APIs, but is not associated +with or sponsored by Google, Inc. + + +Installation +============ + +The Google RSS Tag Library requires a JSP (1.1 or higher) container, such +as Tomcat. + +To use the Google RSS Tag Library, simply copy the library's JAR file +(google-rss.jar) to your application's WEB-INF/lib directory. The Tag Library +Descriptor (google-rss.tld) should be placed in your application's WEB-INF +directory. + +You should also modify your web application deployment descriptor (web.xml) +file to map the Google RSS Tag Library TLD URI to its location: + + + /google-rss-taglib + /WEB-INF/google-rss.tld + + + +TagLib Directive +================ + +Using the Google RSS Tag Library is easy; you simply need to import it into +your JSP pages using the taglib directive. For instance, you would include +the following line at the top of your JSP page: + + <%@taglib uri="/google-rss-taglib" prefix="rss"%> + + +SearchResults Tag +================= + +The tag is used to display the search results returned +by a Google search (performed using the Google Tag Library) in RSS format. + +For example: + + <%@ page contentType="text/xml" %> + <%@ taglib uri="/google-taglib" prefix="google" %> + <%@ taglib uri="/google-rss-taglib" prefix="rss" %> + my search query + + +Please refer to the Google Tag Library documentation for detailed information +on performing searches. + + +=========================================================================== +$Id$ \ No newline at end of file diff --git a/google-taglib/rss/build.properties b/google-taglib/rss/build.properties new file mode 100644 index 0000000..e89ace1 --- /dev/null +++ b/google-taglib/rss/build.properties @@ -0,0 +1,27 @@ +# Version +version=1.0 + +# Project +project.name=google-rss +release.name=${project.name}-taglib-${version} + +# Locations +src.dir=src +build.dir=build +dist.dir=dist +lib.dir=../lib +tlds.dir=tlds + +# Google +google.jar=../dist/google.jar +google.tld=../dist/google.tld + +# Examples +ex.dir=${project.name}-examples + +# Source +src.excludes=${lib.dir}/googleapi.jar + +# Docs +doc.license=LICENSE.txt +doc.readme=README.txt \ No newline at end of file diff --git a/google-taglib/rss/build.xml b/google-taglib/rss/build.xml new file mode 100644 index 0000000..71db1bb --- /dev/null +++ b/google-taglib/rss/build.xml @@ -0,0 +1,112 @@ + + + Google RSS Tag Library + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/google-taglib/rss/google-rss-examples/README.txt b/google-taglib/rss/google-rss-examples/README.txt new file mode 100644 index 0000000..9bdbd8b --- /dev/null +++ b/google-taglib/rss/google-rss-examples/README.txt @@ -0,0 +1,66 @@ +Google RSS Tag Library Examples README +====================================== + + +Overview +======== + +This README file describes the Google RSS Tag Library Examples web +application. + +This web application contains a collection of examples demonstrating the +usage of the Google JSP RSS Tag Library. For more information about using +the custom tag library in your own web applications, please refer to the +Google RSS Tag Library documentation. + +The Google RSS Tag Library is an extension to the Google Tag Library, which +can be found at: + + + +See the "LICENSE.txt" file for licensing details. + +The Google RSS Tag Library uses the Google Web APIs, but is not associated +with or sponsored by Google, Inc. + + +Google Web APIs +=============== + +The Google Web APIs Java library is required in order to use the Examples web +application. The library's JAR file (googleapi.jar) should be placed into the +application's WEB-INF/lib directory. + + http://www.google.com/apis/download.html + +You must also register with Google (free) in order to obtain a license key. +The license key is required to access Google's Web Services. + + https://www.google.com/accounts/NewAccount + + +Google License Key +================== + +The license key can be specified as a context parameter in the web +application deployment descriptor (web.xml) as follows: + + + google_key + 000000000000000000000000 + + +The license key can also be temporarily specified from the index page of +the web application. + + +Examples +======== + +The following example is included: + + - Perform a simple Google RSS search + + +=========================================================================== +$Id$ \ No newline at end of file diff --git a/google-taglib/rss/google-rss-examples/WEB-INF/web.xml b/google-taglib/rss/google-rss-examples/WEB-INF/web.xml new file mode 100644 index 0000000..2d63d72 --- /dev/null +++ b/google-taglib/rss/google-rss-examples/WEB-INF/web.xml @@ -0,0 +1,42 @@ + + + + google-rss-examples + Google RSS Examples Web Application + + google_key + + The Google Web APIs client authorization key. + + + google_proxy_host + + The host to use as an HTTP proxy. + + + google_proxy_port + + The port to use as an HTTP proxy. + + + google_proxy_username + + The username required for the HTTP proxy. + + + google_proxy_password + + The password required for the HTTP proxy. + + + index.html + + + /google-taglib + /WEB-INF/google.tld + + + /google-rss-taglib + /WEB-INF/google-rss.tld + + diff --git a/google-taglib/rss/google-rss-examples/index.html b/google-taglib/rss/google-rss-examples/index.html new file mode 100644 index 0000000..9639482 --- /dev/null +++ b/google-taglib/rss/google-rss-examples/index.html @@ -0,0 +1,71 @@ + + + +Google RSS Tag Library Examples + + + + + +Google RSS Tag Library Examples +

+This is a collection of examples demonstrating the usage of the Google RSS JSP Tag Library. +

+

+The Google RSS Tag Library is an extension to the Google Tag Library. +

+

+The Google Web APIs Java library is required in order to use the Google RSS Tag +Library. The library's JAR file (googleapi.jar) should be placed +into the application's WEB-INF/lib directory. +

+ +

+You must also register with Google (free) in order to obtain a license key. The +license key is required to access Google's Web Services. +

+ +

+The license key can be specified as a context parameter in the web application +deployment descriptor (web.xml) as follows: +

+
+
<context-param>
<param-name>google_key</param-name>
<param-value>000000000000000000000000</param-value>
</context-param>
+
+

+The license key can also be temporarily specified using the following: +

+ +

+The included examples will allow you to: +

+ +

 

+

+The Google RSS Tag Library uses the Google Web APIs, but is not associated with or sponsored by Google, Inc.
+Click here for licensing information.
+

+ + diff --git a/google-taglib/rss/google-rss-examples/key.html b/google-taglib/rss/google-rss-examples/key.html new file mode 100644 index 0000000..4fa97a3 --- /dev/null +++ b/google-taglib/rss/google-rss-examples/key.html @@ -0,0 +1,16 @@ + + + +Set Google Key + + + + +
+   +
+ + diff --git a/google-taglib/rss/google-rss-examples/key.jsp b/google-taglib/rss/google-rss-examples/key.jsp new file mode 100644 index 0000000..cf30658 --- /dev/null +++ b/google-taglib/rss/google-rss-examples/key.jsp @@ -0,0 +1,21 @@ +<% + // Copyright (c) 2002-2004, Erik C. Thauvin + // All rights reserved. + + String key = request.getParameter("key"); + + if ((key != null)&& (key.trim().length() > 0)) + { + pageContext.setAttribute("google_key", key, PageContext.APPLICATION_SCOPE); +%> + <%@include file="search.html"%> +<% + } + else + { +%> + <%@include file="key.html"%> +<% + } +%> + diff --git a/google-taglib/rss/google-rss-examples/rss.jsp b/google-taglib/rss/google-rss-examples/rss.jsp new file mode 100644 index 0000000..0c7d8b5 --- /dev/null +++ b/google-taglib/rss/google-rss-examples/rss.jsp @@ -0,0 +1 @@ +<%@ page contentType="text/xml" %><%@ taglib uri="/google-taglib" prefix="google" %><%@ taglib uri="/google-rss-taglib" prefix="rss" %> diff --git a/google-taglib/rss/google-rss-examples/search.html b/google-taglib/rss/google-rss-examples/search.html new file mode 100644 index 0000000..9367268 --- /dev/null +++ b/google-taglib/rss/google-rss-examples/search.html @@ -0,0 +1,16 @@ + + + +Search Google + + + + +
+   +
+ + diff --git a/google-taglib/rss/src/com/clevercactus/google/AtomGenerator.java b/google-taglib/rss/src/com/clevercactus/google/AtomGenerator.java new file mode 100644 index 0000000..292111c --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/AtomGenerator.java @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2003-2004 clevercactus ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +package com.clevercactus.google; + +import org.xml.sax.SAXException; + +import java.io.PrintWriter; +import java.util.Date; + +/** + * A class that uses JAXP and SAX to print an SearchResults 2.0 feed to a PrintWriter. + * Output of the SearchResults happens as data is added. See the main() method for a + * simple usage example. + * + * @version 1.0, 29/08/2003 + * @author Diego Doval + */ +public +class AtomGenerator + extends FeedGenerator +{ + /** + * The one and only constructor. + */ + public + AtomGenerator() + { + } + + /** + * Prints a valid SearchResults 2.0 header based on the information passed. + * + * @param title feed title + * @param link feed link + * @param description feed description + * @param author feed author (must be an email address) + * @param lastBuildDate the date the feed was built + * @throws FeedGenerationException + */ + public + void + printHeader(String title, String link, String description, String author, Date lastBuildDate) + throws FeedGenerationException + { + try { + String language = "en-us"; + String generatorName = "clevercactus atom generator 1.0"; + String generatorURL = "http://www.clevercactus.com/google/"; + String copyright = "Copyright (c) 2003-2004 clevercactus ltd."; + String authorName = "Diego Doval"; + + feedAttributes_d.clear(); + feedAttributes_d.addAttribute("", "", "version", "", "0.2"); + feedAttributes_d.addAttribute("", "", "xmlns", "", "http://purl.org/atom/ns#"); + feedAttributes_d.addAttribute("", "", "xml:lang", "", "en"); + transformerHandler_d.startElement("","","feed",feedAttributes_d); + + feedAttributes_d.clear(); + transformerHandler_d.startElement("","","author",feedAttributes_d); + transformerHandler_d.startElement("","","name",feedAttributes_d); + transformerHandler_d.characters(authorName.toCharArray(),0,authorName.length()); + transformerHandler_d.endElement("","","name"); + transformerHandler_d.endElement("","","author"); + + feedAttributes_d.clear(); + transformerHandler_d.startElement("","","title",feedAttributes_d); + transformerHandler_d.characters(title.toCharArray(),0,title.length()); + transformerHandler_d.endElement("","","title"); + transformerHandler_d.startElement("","","link",feedAttributes_d); + transformerHandler_d.characters(link.toCharArray(),0,link.length()); + transformerHandler_d.endElement("","","link"); + transformerHandler_d.startElement("","","tagline", feedAttributes_d); + transformerHandler_d.characters(description.toCharArray(),0,description.length()); + transformerHandler_d.endElement("","","tagline"); + transformerHandler_d.startElement("","","modified",elementAttributes_d); + String dtString = getAsISO8601String(lastBuildDate); + transformerHandler_d.characters(dtString.toCharArray(),0,dtString.length()); + transformerHandler_d.endElement("","","modified"); + feedAttributes_d.addAttribute("", "", "name", "", generatorName); + transformerHandler_d.startElement("","","generator", feedAttributes_d); + transformerHandler_d.characters(generatorURL.toCharArray(),0, generatorURL.length()); + transformerHandler_d.endElement("","","generator"); + feedAttributes_d.clear(); + transformerHandler_d.startElement("","","copyright", feedAttributes_d); + transformerHandler_d.characters(copyright.toCharArray(),0,copyright.length()); + transformerHandler_d.endElement("","","copyright"); + } + catch (SAXException e) { + throw new FeedGenerationException(e); + } + } + + /** + * Prints a valid SearchResults 2.0 item based on the information passed. + * + * @param title the entry title + * @param description the entry body/description + * @param link the entry's GUID + * @param date the entry publication date + * @throws FeedGenerationException + */ + public + void + printItem(String title, String description, String link, Date date) + throws FeedGenerationException + { + try { + elementAttributes_d.clear(); + + transformerHandler_d.startElement("","","entry",elementAttributes_d); + transformerHandler_d.startElement("","","title",elementAttributes_d); + transformerHandler_d.characters(title.toCharArray(),0,title.length()); + transformerHandler_d.endElement("","","title"); + + transformerHandler_d.startElement("","","issued",elementAttributes_d); + String dtString = getAsISO8601String(date); + transformerHandler_d.characters(dtString.toCharArray(),0,dtString.length()); + transformerHandler_d.endElement("","","issued"); + + transformerHandler_d.startElement("","","modified",elementAttributes_d); + transformerHandler_d.characters(dtString.toCharArray(),0,dtString.length()); + transformerHandler_d.endElement("","","modified"); + + transformerHandler_d.startElement("","","created",elementAttributes_d); + transformerHandler_d.characters(dtString.toCharArray(),0,dtString.length()); + transformerHandler_d.endElement("","","created"); + + transformerHandler_d.startElement("","","link",elementAttributes_d); + transformerHandler_d.characters(link.toCharArray(),0,link.length()); + transformerHandler_d.endElement("","","link"); + + transformerHandler_d.startElement("","","id",elementAttributes_d); + transformerHandler_d.characters(link.toCharArray(),0,link.length()); + transformerHandler_d.endElement("","","id"); + + elementAttributes_d.clear(); + elementAttributes_d.addAttribute("", "", "type", "", "text/html"); + elementAttributes_d.addAttribute("", "", "mode", "", "escaped"); + elementAttributes_d.addAttribute("", "", "xml:lang", "", "en"); + + transformerHandler_d.startElement("","","content",elementAttributes_d); + transformerHandler_d.startCDATA(); + transformerHandler_d.characters(description.toCharArray(),0,description.length()); + transformerHandler_d.endCDATA(); + transformerHandler_d.endElement("","","content"); + + transformerHandler_d.endElement("","","entry"); + } + catch (SAXException e) { + throw new FeedGenerationException(e); + } + + } + + /** + * Closes the feed's main elements (i.e., "rss" and "channel"). + * @throws FeedGenerationException + */ + public + void + close() + throws FeedGenerationException + { + try { + transformerHandler_d.endElement("","","feed"); + transformerHandler_d.endDocument(); + } + catch (SAXException e) { + throw new FeedGenerationException(e); + } + } + + /** + * Simple test to generate a 2.0 feed. + */ + public static + void + main(String[] args) + { + try { + AtomGenerator gen = new AtomGenerator(); + gen.init(new PrintWriter(System.out)); + gen.printHeader("Test feed", "http://something/", "test feed desc", "test@test.com", new Date()); + gen.printItem("Item title 1", "Item desc 1", "http://guid/1234", new Date()); + gen.printItem("Item title 2", "Item desc 2", "http://guid/12342", new Date()); + gen.close(); + } + catch (FeedGenerationException e) { + e.printStackTrace(); + } + } +} diff --git a/google-taglib/rss/src/com/clevercactus/google/FeedGenerationException.java b/google-taglib/rss/src/com/clevercactus/google/FeedGenerationException.java new file mode 100644 index 0000000..fb33e0f --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/FeedGenerationException.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2003-2004 clevercactus ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +package com.clevercactus.google; + +/** + * Used to simplify exception handling in the RSSGenerator class. Internal + * Exceptions in that class will be wrapped in an FeedGenerationException. + * + * @version 1.0, 29/08/2003 + * @author Diego Doval + */ +public +class FeedGenerationException + extends Exception +{ + public + FeedGenerationException() + { + } + + public + FeedGenerationException(Throwable cause) + { + super(cause); + } + + public + FeedGenerationException(String message) + { + super(message); + } + + public + FeedGenerationException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/google-taglib/rss/src/com/clevercactus/google/FeedGenerator.java b/google-taglib/rss/src/com/clevercactus/google/FeedGenerator.java new file mode 100644 index 0000000..5879e6e --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/FeedGenerator.java @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2003-2004 clevercactus ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +package com.clevercactus.google; + +import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.SAXException; + +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.Transformer; +import javax.xml.transform.OutputKeys; +import java.io.Writer; +import java.util.Date; +import java.util.Locale; +import java.text.SimpleDateFormat; + +/** + * A class that uses JAXP and SAX to print an feed to a PrintWriter. + * Output of the actual happens as data is added on the subclass. See the main() method for a + * simple usage example. + * + * @version 1.0, 29/08/2003 + * @author Diego Doval + */ +public abstract +class FeedGenerator +{ + protected Writer output_d; + protected StreamResult streamResult_d; + protected TransformerHandler transformerHandler_d; + protected SAXTransformerFactory saxTransformerFactory_d; + protected AttributesImpl elementAttributes_d; + protected AttributesImpl feedAttributes_d; + + public static SimpleDateFormat iso8601DateFormat_sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); + public static SimpleDateFormat rfc822DateFormat_sd = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yy' 'HH:mm:ss' 'z", Locale.US); + + /** + * The one and only constructor. + */ + public + FeedGenerator() + { + } + + /** + * @param output writer to use for outputting the SearchResults + * @throws FeedGenerationException + */ + public + void + init(Writer output) + throws FeedGenerationException + { + output_d = output; + streamResult_d = new StreamResult(output_d); + feedAttributes_d = new AttributesImpl(); + elementAttributes_d = new AttributesImpl(); + try { + saxTransformerFactory_d = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); + transformerHandler_d = saxTransformerFactory_d.newTransformerHandler(); + Transformer trans = transformerHandler_d.getTransformer(); + trans.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); + trans.setOutputProperty(OutputKeys.INDENT,"yes"); + transformerHandler_d.setResult(streamResult_d); + transformerHandler_d.startDocument(); + } + catch (Exception e) { + throw new FeedGenerationException(e); + } + } + + /** + * Prints a valid header based on the information passed. + * + * @param title feed title + * @param link feed link + * @param description feed description + * @param author feed author (must be an email address) + * @param lastBuildDate the date the feed was built + * @throws FeedGenerationException + */ + public abstract + void + printHeader(String title, String link, String description, String author, Date lastBuildDate) + throws FeedGenerationException; + + /** + * Prints a valid SearchResults 2.0 item based on the information passed. + * + * @param title the entry title + * @param description the entry body/description + * @param link the entry's GUID + * @param date the entry publication date + * @throws FeedGenerationException + */ + public abstract + void + printItem(String title, String description, String link, Date date) + throws FeedGenerationException; + + /** + * Closes the feed's main elements + * @throws FeedGenerationException + */ + public abstract + void + close() + throws FeedGenerationException; + + /** + * Utility method to obtain a date in a string in RFC 822 date format + * + * @param date the date to + * @return a string with the date in RFC 822 format + */ + public final + String + getAsISO8601String(Date date) + { + String result = iso8601DateFormat_sd.format(date); + //convert 20030509T00:32:35+0100 returned by Java into 20030509T00:32:35+01:00 + //from the spec date/time 19980717T14:08:55 + result = result.substring(0, result.length()-2) + ":" + result.substring(result.length()-2); + return result; + } + + + /** + * Utility method to obtain a date in a string in RFC 822 date format + * + * @param date the date to + * @return a string with the date in RFC 822 format + */ + public final + String + getAsRFC822String(Date date) + { + return rfc822DateFormat_sd.format(date); + } +} diff --git a/google-taglib/rss/src/com/clevercactus/google/FeedGeneratorFactory.java b/google-taglib/rss/src/com/clevercactus/google/FeedGeneratorFactory.java new file mode 100644 index 0000000..a07fe81 --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/FeedGeneratorFactory.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2003-2004 clevercactus ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +package com.clevercactus.google; + +import java.util.HashMap; + +/** + * Date: 05-Sep-2003 + * Time: 20:41:19 + */ +public +class FeedGeneratorFactory +{ + public static HashMap feedTypes_sd = new HashMap(); + + public static final String FEED_TYPE_RSS = "rss"; + public static final String FEED_TYPE_ATOM = "atom"; + + static { + addGenerator(FEED_TYPE_RSS, RSSGenerator.class); + addGenerator(FEED_TYPE_ATOM, AtomGenerator.class); + } + + public static + boolean + supportsFormat(String type) + { + return type != null && (type.equals(FEED_TYPE_RSS) || type.equals(FEED_TYPE_ATOM)); + } + + public static + void + addGenerator(String type, Class cls) + { + feedTypes_sd.put(type, cls); + } + + public static + FeedGenerator + getGeneratorForType(String type) + { + Class cls = (Class) feedTypes_sd.get(type); + try { + return (FeedGenerator) cls.newInstance(); + } + catch (InstantiationException e) { + e.printStackTrace(); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapper.java b/google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapper.java new file mode 100644 index 0000000..128ad70 --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapper.java @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2003-2004 clevercactus ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +package com.clevercactus.google; + +import com.google.soap.search.GoogleSearchResultElement; + +import java.io.Writer; +import java.io.IOException; +import java.util.Iterator; +import java.util.Date; +import java.util.ArrayList; + +/** + * Date: 30-Oct-2003 + * Time: 13:41:41 + * @author Diego Doval + * @author Erik C. Thauvin + */ +public +class GoogleResultsWrapper +{ + ArrayList items_d = new ArrayList(); + + + private + GoogleResultsWrapper() + { + } + + private + Iterator + iterator() + { + return items_d.iterator(); + } + + private + void + addItem(GoogleResultsWrapperItem it) + { + items_d.add(it); + } + + private static + GoogleResultsWrapper + createWrapper(GoogleSearchResultElement[] elements) + { + if (elements == null || elements.length == 0) { + return null; + } + GoogleResultsWrapper wrapper = new GoogleResultsWrapper(); + for (int i = 0; i < elements.length; i++) { + GoogleSearchResultElement element = elements[i]; + wrapper.addItem(new GoogleResultsWrapperItem(element.getTitle(), element.getURL(), element.getSnippet())); + } + return wrapper; + } + + /** + * Output the results to a writer. + * + * @param out the writer to use + * @param elements the search result elements + * @param query the search query + * @param feedType the type of feed to use (options are "rss" and "atom") + */ + public static + void + outputResults(Writer out, GoogleSearchResultElement[] elements, String query, String feedType) + { + outputResults(out, createWrapper(elements), query, feedType); + } + + /** + * Output the results to a writer. + * + * @param out the writer to use + * @param wrapper the results object obtained from the parsing of results + * @param query the search query + * @param feedType the type of feed to use (options are "rss" and "atom") + */ + private static + void + outputResults(Writer out, GoogleResultsWrapper wrapper, String query, String feedType) + { + try { + if (out == null || feedType == null) { + try { + out.write("Null parameter in output results.\n"); + } + catch (IOException e1) { + } + return; + } + if (!feedType.equals(FeedGeneratorFactory.FEED_TYPE_RSS) + && !feedType.equals(FeedGeneratorFactory.FEED_TYPE_ATOM)) { + try { + out.write("Invalid feed type ("+feedType+"). Supported types are 'rss' and 'atom'.\n"); + } + catch (IOException e1) { + } + return; + } + try { + FeedGenerator gen = FeedGeneratorFactory.getGeneratorForType(feedType); + gen.init(out); + Date date = new Date(); + + String description; + if ((query == null) || (query.trim().length() == 0)) { + description = "Google Tag Library"; + } else { + description = "Search results for: \"" + query + '"'; + } + + gen.printHeader("Google Search Feed Output", "http://google-taglib.sourceforge.net/", description, "check@page.for.info", date); + if (wrapper != null) { + Iterator items = wrapper.iterator(); + while (items.hasNext()) { + GoogleResultsWrapperItem item = (GoogleResultsWrapperItem) items.next(); + gen.printItem(item.getTitle(), item.getSnippet(), item.getURL(), date); + } + } + gen.close(); + } + catch (FeedGenerationException e) { + try { + out.write("Error.\n"); + out.write(e.getMessage()); + } + catch (IOException e1) { + } + } + } + finally { + try { + out.close(); + } + catch (IOException e) { + } + } + } + +} \ No newline at end of file diff --git a/google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapperItem.java b/google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapperItem.java new file mode 100644 index 0000000..848c216 --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/GoogleResultsWrapperItem.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2003-2004 clevercactus ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +package com.clevercactus.google; + +/** + * Date: 30-Oct-2003 + * Time: 13:51:43 + * @author Diego Doval + */ +public +class GoogleResultsWrapperItem +{ + String title_d; + String text_d; + String url_d; + + public + GoogleResultsWrapperItem(String title, String url, String text) + { + title_d = title; + url_d = url; + text_d = text; + } + + public + String + getTitle() + { + return title_d; + } + + public + String + getSnippet() + { + return text_d; + } + + public + String + getURL() + { + return url_d; + } + + public + String + toString() + { + return "Title: "+title_d+"\nURL:"+url_d+"\nText:"+text_d+"\n"; + } +} diff --git a/google-taglib/rss/src/com/clevercactus/google/RSSGenerator.java b/google-taglib/rss/src/com/clevercactus/google/RSSGenerator.java new file mode 100644 index 0000000..e6a4b25 --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/RSSGenerator.java @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2003-2004 clevercactus ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +package com.clevercactus.google; + +import org.xml.sax.SAXException; + +import java.io.PrintWriter; +import java.util.Date; + +/** + * A class that uses JAXP and SAX to print an SearchResults 2.0 feed to a PrintWriter. + * Output of the SearchResults happens as data is added. See the main() method for a + * simple usage example. + * + * @version 1.0, 29/08/2003 + * @author Diego Doval + */ +public +class RSSGenerator + extends FeedGenerator +{ + + /** + * The one and only constructor. + */ + public + RSSGenerator() + { + } + + /** + * Prints a valid SearchResults 2.0 header based on the information passed. + * + * @param title feed title + * @param link feed link + * @param description feed description + * @param author feed author (must be an email address) + * @param lastBuildDate the date the feed was built + * @throws FeedGenerationException + */ + public + void + printHeader(String title, String link, String description, String author, Date lastBuildDate) + throws FeedGenerationException + { + try { + String language = "en-us"; + String generator = "clevercactus rss generator 1.0"; + + feedAttributes_d.clear(); + feedAttributes_d.addAttribute("", "", "version", "", "2.0"); + transformerHandler_d.startElement("","","rss",feedAttributes_d); + feedAttributes_d.clear(); + transformerHandler_d.startElement("","","channel",feedAttributes_d); + transformerHandler_d.startElement("","","title",feedAttributes_d); + transformerHandler_d.characters(title.toCharArray(),0,title.length()); + transformerHandler_d.endElement("","","title"); + transformerHandler_d.startElement("","","link",feedAttributes_d); + transformerHandler_d.characters(link.toCharArray(),0,link.length()); + transformerHandler_d.endElement("","","link"); + transformerHandler_d.startElement("","","description", feedAttributes_d); + transformerHandler_d.characters(description.toCharArray(),0,description.length()); + transformerHandler_d.endElement("","","description"); + transformerHandler_d.startElement("","","language", feedAttributes_d); + transformerHandler_d.characters(language.toCharArray(),0,language.length()); + transformerHandler_d.endElement("","","language"); + transformerHandler_d.startElement("","","lastBuildDate",elementAttributes_d); + String dtString = getAsRFC822String(lastBuildDate); + transformerHandler_d.characters(dtString.toCharArray(),0,dtString.length()); + transformerHandler_d.endElement("","","lastBuildDate"); + transformerHandler_d.startElement("","","generator", feedAttributes_d); + transformerHandler_d.characters(generator.toCharArray(),0,generator.length()); + transformerHandler_d.endElement("","","generator"); + transformerHandler_d.startElement("","","managingEditor", feedAttributes_d); + transformerHandler_d.characters(author.toCharArray(),0,author.length()); + transformerHandler_d.endElement("","","managingEditor"); + transformerHandler_d.startElement("","","webMaster", feedAttributes_d); + transformerHandler_d.characters(author.toCharArray(),0,author.length()); + transformerHandler_d.endElement("","","webMaster"); + } + catch (SAXException e) { + throw new FeedGenerationException(e); + } + } + + /** + * Prints a valid SearchResults 2.0 item based on the information passed. + * + * @param title the entry title + * @param description the entry body/description + * @param link the entry's GUID + * @param date the entry publication date + * @throws FeedGenerationException + */ + public + void + printItem(String title, String description, String link, Date date) + throws FeedGenerationException + { + try { + elementAttributes_d.clear(); + transformerHandler_d.startElement("","","item",elementAttributes_d); + elementAttributes_d.clear(); + transformerHandler_d.startElement("","","title",elementAttributes_d); + transformerHandler_d.characters(title.toCharArray(),0,title.length()); + transformerHandler_d.endElement("","","title"); + elementAttributes_d.clear(); + transformerHandler_d.startElement("","","description",elementAttributes_d); + transformerHandler_d.characters(description.toCharArray(),0,description.length()); + transformerHandler_d.endElement("","","description"); + elementAttributes_d.clear(); + transformerHandler_d.startElement("","","pubDate",elementAttributes_d); + String dtString = getAsRFC822String(date); + transformerHandler_d.characters(dtString.toCharArray(),0,dtString.length()); + transformerHandler_d.endElement("","","pubDate"); + elementAttributes_d.clear(); + transformerHandler_d.startElement("","","link",elementAttributes_d); + transformerHandler_d.characters(link.toCharArray(),0,link.length()); + transformerHandler_d.endElement("","","link"); + + transformerHandler_d.endElement("","","item"); + } + catch (SAXException e) { + throw new FeedGenerationException(e); + } + + } + + /** + * Closes the feed's main elements (i.e., "rss" and "channel"). + * @throws FeedGenerationException + */ + public + void + close() + throws FeedGenerationException + { + try { + transformerHandler_d.endElement("","","channel"); + transformerHandler_d.endElement("","","rss"); + transformerHandler_d.endDocument(); + } + catch (SAXException e) { + throw new FeedGenerationException(e); + } + } + + + /** + * Simple test to generate a 2.0 feed. + */ + public static + void + main(String[] args) + { + try { + RSSGenerator gen = new RSSGenerator(); + gen.init(new PrintWriter(System.out)); + gen.printHeader("Test feed", "http://something/", "test feed desc", "test@test.com", new Date()); + gen.printItem("Item title 1", "Item desc 1", "guid", new Date()); + gen.printItem("Item title 2", "Item desc 2", "guid", new Date()); + gen.close(); + } + catch (FeedGenerationException e) { + e.printStackTrace(); + } + } +} diff --git a/google-taglib/rss/src/com/clevercactus/google/taglibs/SearchResults.java b/google-taglib/rss/src/com/clevercactus/google/taglibs/SearchResults.java new file mode 100644 index 0000000..61f44f0 --- /dev/null +++ b/google-taglib/rss/src/com/clevercactus/google/taglibs/SearchResults.java @@ -0,0 +1,132 @@ +/* + * @(#)SearchResults.java + * + * Copyright (c) 2003-2004, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $Id$ + * + */ +package com.clevercactus.google.taglibs; + +import com.clevercactus.google.GoogleResultsWrapper; + +import net.thauvin.google.GoogleSearchBean; +import net.thauvin.google.TagUtility; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.tagext.BodyTagSupport; + + +/** + * A custom tag used to display Google search results in RSS format. + * + * @author Erik C. Thauvin + * @version $Revision$, $Date$ + * + * @created Oct 30, 2003 + * @since 1.0 + */ +public class SearchResults extends BodyTagSupport +{ + /** + * The Google search bean. + */ + protected GoogleSearchBean bean = null; + + /** + * doEndTag method. + * + * @return EVAL_PAGE + * + * @throws JspException + */ + public int doEndTag() throws JspException + { + try + { + // Is the result valid? + if (bean != null) + { + // Output the feed + GoogleResultsWrapper.outputResults(pageContext.getOut(), + bean.getResultElements(), + bean.getResultProperty("searchKeywords"), + "rss"); + } + } + catch (Exception e) + { + throw TagUtility.outputError("rss", e); + } + + // Reset the values + reset(); + + return EVAL_PAGE; + } + + /** + * doStartTag method. + * + * @return SKIP_BODY. + * + * @throws JspException + */ + public int doStartTag() throws JspException + { + // Get the Google bean + bean = TagUtility.getGoogleSearchBean(pageContext); + + return SKIP_BODY; + } + + /** + * Release method. + */ + public void release() + { + super.release(); + + // Reset the bean + bean = null; + + // Reset the values + reset(); + } + + /** + * Reset the values. + */ + protected void reset() + { + // super.reset(); + } +} diff --git a/google-taglib/rss/tlds/google-rss.tld b/google-taglib/rss/tlds/google-rss.tld new file mode 100644 index 0000000..06ee349 --- /dev/null +++ b/google-taglib/rss/tlds/google-rss.tld @@ -0,0 +1,22 @@ + + + + + + @version@ + + 1.1 + + google-rss + + http://www.thauvin.net/taglibs/google-rss-taglib-@version@ + + The Google RSS Tag Library. + + + searchResults + com.clevercactus.google.taglibs.SearchResults + empty + Displays Google search results in RSS format. + +