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;
023:
024: import org.jboss.portal.common.i18n.LocalizedString;
025: import org.jboss.portal.common.text.FastURLEncoder;
026: import org.jboss.portal.common.util.UUIDGenerator;
027: import org.jboss.portal.widget.Widget;
028: import org.jboss.portal.widget.google.info.GGPreferenceInfo;
029: import org.jboss.portal.widget.google.info.GGPreferencesInfo;
030: import org.jboss.portal.widget.google.info.GGWidgetInfo;
031:
032: import java.net.URL;
033: import java.util.Iterator;
034: import java.util.Locale;
035: import java.util.Map;
036:
037: /**
038: * A Google Gadget widget. For more details please see the <a href="http://www.google.com/apis/gadgets/reference.html">API
039: * Developer Guide</a>.
040: *
041: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
042: * @version $Revision: 8953 $
043: * @todo - Resource bundles implementation
044: */
045: public class GGWidget implements Widget {
046:
047: /** . */
048: private static final UUIDGenerator generator = new UUIDGenerator();
049:
050: /** . */
051: private final URL url;
052:
053: /** . */
054: private final String id;
055:
056: /** . */
057: private final GGWidgetInfo info;
058:
059: public GGWidget(URL url, GGWidgetInfo info) {
060: this .id = url.toString();
061: this .url = url;
062: this .info = info;
063: }
064:
065: public String getId() {
066: return id;
067: }
068:
069: public GGWidgetInfo getInfo() {
070: return info;
071: }
072:
073: public LocalizedString getTitle() {
074: return getInfo().getTitle();
075: }
076:
077: public LocalizedString getDirectoryTitle() {
078: return getInfo().getDirectoryTitle();
079: }
080:
081: public LocalizedString getDescription() {
082: return getInfo().getDescription();
083: }
084:
085: public String render(Map parameters) {
086: return render(parameters, null);
087: }
088:
089: public String render(Map parameters, Locale locale) {
090: String id = generator.generateKey();
091: StringBuffer tmp = new StringBuffer(
092: "http://gmodules.com/ig/ifr?url=").append(url);
093:
094: //
095: GGWidgetInfo info = getInfo();
096:
097: //
098: if (info != null) {
099: // Generate base params
100: tmp.append("&synd=jboss");
101: tmp.append("&w=").append(info.getWidth());
102: tmp.append("&h=").append(info.getHeight());
103: //don's let to override the title param - proper one will be picked up with i18n support
104: //tmp.append("&title=").append(FastURLEncoder.DEFAULT_ENCODER.encode(info.getTitle().getDefaultString()));
105: tmp
106: .append("&border=%23ffffff%7C3px%2C1px+none+%23999999"); // "&border=%23ffffff%7C3px%2C1px+solid+%23999999"
107: tmp.append("&output=js");
108: //pass locale information
109: if (locale != null) {
110: tmp.append("&lang=").append(locale.getLanguage());
111: tmp.append("&country=").append(locale.getCountry());
112: }
113:
114: //
115: GGPreferencesInfo prefsInfo = info.getPreferences();
116:
117: for (Iterator i = prefsInfo.getPreferences().iterator(); i
118: .hasNext();) {
119: GGPreferenceInfo prefInfo = (GGPreferenceInfo) i.next();
120: String prefName = prefInfo.getName();
121: String defaultValue = prefInfo.getDefaultValue() != null ? prefInfo
122: .getDefaultValue()
123: : "";
124: String[] values = (String[]) parameters.get(prefName);
125: String value = values != null ? values[0]
126: : defaultValue;
127: // if value is present and not a default parameter
128: if (value != null && !defaultValue.equals(value)) {
129: FastURLEncoder encoder = FastURLEncoder
130: .getUTF8Instance();
131: tmp.append("&up_").append(
132: encoder.encode(prefName)).append("=")
133: .append(encoder.encode(value));
134: }
135: }
136:
137: //
138: String clipper = "" + "(function(){\n"
139: + "var a = document.getElementById('" + id
140: + "');\n" + "var b = a.childNodes.item(2);\n"
141: + // table
142: "var c = b.childNodes.item(0);\n"
143: + // tbody
144: "var d = c.childNodes.item(0);\n"
145: + // tr
146: "var e = c.childNodes.item(1);\n"
147: + // tr
148: "var f = c.childNodes.item(3);\n"
149: + // tr
150: "c.removeChild(d);\n" + "c.removeChild(e);\n"
151: + "c.removeChild(f);\n" + "})();";
152:
153: return "<div id=\"" + id + "\">\n" + "<script src=\"" + tmp
154: + "\"></script>\n" + "<script>" + clipper
155: + "</script>\n" + "</div>\n";
156: } else {
157: return "Error";
158: }
159: }
160: }
|