Author: chris.laprun(a)jboss.com
Date: 2008-06-13 11:05:14 -0400 (Fri, 13 Jun 2008)
New Revision: 11043
Modified:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java
Log:
- Now force US location and English language for query.
- More robust handling of Google markup. Should output a message if the query couldn't
be parsed.
Modified:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java 2008-06-13
13:55:11 UTC (rev 11042)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleClippingPortlet.java 2008-06-13
15:05:14 UTC (rev 11043)
@@ -52,7 +52,11 @@
private static final String A_TARGET_BLANK = "<a
target='_blank'";
private static final String ZIP = "zipcode";
private static final String SAN_FRAN = "94102";
- private static final String GOOGLE = "http://www.google.com/search?q=";
+ /**
+ * gl=US forces use of US google site, hl=en forces results to be in English so that
regardless of location the query
+ * should result in the expected result.
+ */
+ private static final String GOOGLE =
"http://www.google.com/search?gl=US&hl=en&q=";
@Override
protected void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
@@ -71,22 +75,43 @@
in.close();
String beg = "<div id=\"res\"><div";
+ String alternateBeg = "<div id=res><div";
String end = "</table>";
int begIndex = html.indexOf(beg);
if (begIndex != -1)
{
- // extract table containing specific first result
- int endIndex = html.indexOf(end, begIndex);
- html = html.substring(begIndex + beg.length(), endIndex + end.length());
+ html = process(renderResponse, html, beg, end, begIndex);
+ }
+ else
+ {
+ begIndex = html.indexOf(alternateBeg);
+ if (begIndex != -1)
+ {
+ html = process(renderResponse, html, alternateBeg, end, begIndex);
+ }
+ else
+ {
+ html = "<font class='portlet-font'>Couldn't retrieve
result from Google. Sorry! :(</font>";
+ }
+ }
- html = html.substring(html.indexOf(">") + 1);
+ renderResponse.setContentType("text/html");
+ PrintWriter printWriter = renderResponse.getWriter();
+ printWriter.print(html);
+ }
- html = postProcessHTML(html);
+ private String process(RenderResponse renderResponse, String html, String beg, String
end, int begIndex)
+ throws IOException
+ {
+ // extract table containing specific first result
+ int endIndex = html.indexOf(end, begIndex);
+ html = html.substring(begIndex + beg.length(), endIndex + end.length());
- renderResponse.setContentType("text/html");
- PrintWriter printWriter = renderResponse.getWriter();
- printWriter.print(html);
- }
+ html = html.substring(html.indexOf(">") + 1);
+
+ html = postProcessHTML(html);
+
+ return html;
}
protected String getZipCode(RenderRequest renderRequest)
@@ -104,17 +129,17 @@
{
renderResponse.setContentType("text/html");
renderResponse.getWriter().print(
- "<div align='center'>\n" +
- " <br/>\n" +
- " <font class='portlet-font'>Change
Location:</font>\n" +
- "\n" +
- " <form method='post' action='" +
renderResponse.createActionURL() + "'\n" +
- " <font class='portlet-font'>Zip
Code:</font><br/>\n" +
- " <input class='portlet-form-input-field'
type='text' value='' size='12' name='" + ZIP +
"'/>\n" +
- " <br/>\n" +
- " <input class='portlet-form-input-field'
type='submit' name='submit' value='submit'>\n" +
- " </form>\n" +
- "</div>");
+ "<div align='center'>\n" +
+ " <br/>\n" +
+ " <font class='portlet-font'>Change
Location:</font>\n" +
+ "\n" +
+ " <form method='post' action='" +
renderResponse.createActionURL() + "'\n" +
+ " <font class='portlet-font'>Zip
Code:</font><br/>\n" +
+ " <input class='portlet-form-input-field'
type='text' value='' size='12' name='" + ZIP +
"'/>\n" +
+ " <br/>\n" +
+ " <input class='portlet-form-input-field'
type='submit' name='submit' value='submit'>\n" +
+ " </form>\n" +
+ "</div>");
}
@Override
Modified:
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java
===================================================================
---
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java 2008-06-13
13:55:11 UTC (rev 11042)
+++
modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/google/GoogleWeatherClippingPortlet.java 2008-06-13
15:05:14 UTC (rev 11043)
@@ -29,7 +29,7 @@
*/
public class GoogleWeatherClippingPortlet extends GoogleClippingPortlet
{
- private final static String WEATHER_URL =
"http://www.google.com/search?q=weather+";
+ private final static String WEATHER_URL =
"http://www.google.com/search?gl=US&hl=en&q=weather+";
private static final String A_END = "</a>";
protected String getQueryString(String zip)
Show replies by date