001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.widget.google.provider;
023:
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.Locale;
027:
028: import org.jboss.portal.common.text.FastURLEncoder;
029: import org.jboss.portal.widget.WidgetQuery;
030:
031: /**
032: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
033: * @version $Revision: 9157 $
034: */
035: public final class GGQuery implements WidgetQuery {
036:
037: /** . */
038: private static final org.jboss.logging.Logger log = org.jboss.logging.Logger
039: .getLogger(GGQuery.class);
040:
041: /** . */
042: private int start;
043:
044: /** Zero or negative means it is not used. */
045: private int num = 24;
046:
047: /** . */
048: private String cat;
049:
050: /** . */
051: private String q;
052:
053: /** . */
054: private int hashCode;
055:
056: /** . */
057: private Locale locale;
058:
059: public GGQuery(int start, String cat, String q, Locale locale) {
060: if (start < 0) {
061: throw new IllegalArgumentException("No negative start");
062: }
063: if (cat != null && cat.length() == 0) {
064: cat = null;
065: }
066: if (q != null && q.length() == 0) {
067: q = null;
068: }
069:
070: //
071: this .start = start;
072: this .num = 10;
073: this .cat = cat;
074: this .q = q;
075: this .locale = locale;
076: this .hashCode = start + 10
077: + (cat == null ? 0 : 1 + cat.hashCode())
078: + (q == null ? 0 : 1 + q.hashCode())
079: + (locale == null ? 0 : 1 + locale.hashCode());
080: }
081:
082: public GGQuery(int start, int num, String cat, String q,
083: Locale locale) {
084: this (start, cat, q, locale);
085:
086: if (num < 1) {
087: throw new IllegalArgumentException(
088: "No zero or negative num.");
089: }
090:
091: this .num = num;
092: this .hashCode = start + num
093: + (cat == null ? 0 : 1 + cat.hashCode())
094: + (q == null ? 0 : 1 + q.hashCode())
095: + (locale == null ? 0 : 1 + locale.hashCode());
096: }
097:
098: public int getStart() {
099: return start;
100: }
101:
102: public int getNum() {
103: return num;
104: }
105:
106: public String getCat() {
107: return cat;
108: }
109:
110: public String getQ() {
111: return q;
112: }
113:
114: public boolean equals(Object obj) {
115: if (obj == this ) {
116: return true;
117: }
118: if (obj instanceof GGQuery) {
119: GGQuery that = (GGQuery) obj;
120: return (this .start == that.start)
121: && (this .num == that.num)
122: && (this .cat == null ? that.cat == null : this .cat
123: .equals(that.cat))
124: && (this .q == null ? that.q == null : this .q
125: .equals(that.q));
126: }
127: return false;
128: }
129:
130: public int hashCode() {
131: return hashCode;
132: }
133:
134: public URL buildQueryURL() throws MalformedURLException {
135: StringBuffer buffer = new StringBuffer(
136: "http://www.google.com/ig/directory?synd=jboss&output=rss");
137: if (start > 0) {
138: buffer.append("&start=").append(start);
139: }
140: if (num > 0) {
141: buffer.append("&num=").append(num);
142: }
143: if (cat != null) {
144: buffer.append("&cat=").append(cat);
145: }
146: if (q != null) {
147: buffer.append("&q=").append(
148: FastURLEncoder.getUTF8Instance().encode(q));
149: }
150: if (locale != null && locale.getLanguage().length() > 0) {
151: buffer.append("&lang=").append(locale.getLanguage());
152: }
153: // if country info is not provided use the lang code
154: // its because Google assumes that country is US
155: if (locale != null && locale.getCountry().length() > 0) {
156: buffer.append("&country=").append(locale.getCountry());
157: } else if (locale != null && locale.getLanguage().length() > 0) {
158: buffer.append("&country=").append(locale.getLanguage());
159: }
160:
161: log.debug("Google Gadgets Directory query URL: "
162: + buffer.toString());
163:
164: return new URL(buffer.toString());
165: }
166: }
|