Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.beans; import java.io.IOException; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.HashMap; import org.apache.commons.lang.StringUtils; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; import org.jdom.output.Format; /** * The Class GadgetBean. * * @author David Harrison 20th July 2007 */ public class GadgetBean implements java.io.Serializable { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The Constant DEFAULT_HEIGHT. */ private static final int DEFAULT_HEIGHT = 300; /** The Constant DEFAULT_WIDTH. */ private static final int DEFAULT_WIDTH = 270; /** The url. */ private String url; /** The title. */ private String title; /** The description. */ private String description; /** The thumbnail url. */ private String thumbnailUrl; /** The screenshot url. */ private String screenshotUrl; /** The author. */ private String author; /** The height. */ private int height; /** The width. */ private int width; /** The scroll. */ private String scroll; /** The category. */ private String category; /** The user preferences. */ private HashMap<String, HashMap<String, Object>> userPreferences; /** The xml. */ private String xml; /** The created. */ private Date created; /** The created by. */ private String createdBy; /** * Instantiates a new gadget bean. */ public GadgetBean() { } /** * Sets the url. * * @param urlVal the new url */ public final void setUrl(final String urlVal) { this.url = urlVal; } /** * Gets the url. * * @return the url */ public final String getUrl() { return this.url; } /** * Sets the title. * * @param titleVal the new title */ public final void setTitle(final String titleVal) { this.title = titleVal; } /** * Gets the title. * * @return the title */ public final String getTitle() { if (this.title == null) { this.title = ""; } return this.title; } /** * Sets the description. * * @param descriptionVal the new description */ public final void setDescription(final String descriptionVal) { this.description = descriptionVal; } /** * Gets the description. * * @return the description */ public final String getDescription() { if (this.description == null) { this.description = ""; } return this.description; } /** * Sets the thumbnail url. * * @param thumbnailUrlVal the new thumbnail url */ public final void setThumbnailUrl(final String thumbnailUrlVal) { this.thumbnailUrl = thumbnailUrlVal; } /** * Gets the thumbnail url. * * @return the thumbnail url */ public final String getThumbnailUrl() { this.thumbnailUrl = checkUrl(this.thumbnailUrl); return this.thumbnailUrl; } /** * Sets the screenshot url. * * @param screenshotUrlVal the new screenshot url */ public final void setScreenshotUrl(final String screenshotUrlVal) { this.screenshotUrl = screenshotUrlVal; } /** * Gets the screenshot url. * * @return the screenshot url */ public final String getScreenshotUrl() { this.screenshotUrl = checkUrl(this.screenshotUrl); return this.screenshotUrl; } /** * Sets the author. * * @param authorVal the new author */ public final void setAuthor(final String authorVal) { this.author = authorVal; } /** * Gets the author. * * @return the author */ public final String getAuthor() { if (this.author == null) { this.author = "Unknown"; } return this.author; } /** * Sets the width. * * @param widthVal the new width */ public final void setWidth(final int widthVal) { this.width = widthVal; } /** * Gets the width. * * @return the width */ public final int getWidth() { if (this.width == 0) { this.width = DEFAULT_WIDTH; } return this.width; } /** * Sets the height. * * @param heightVal the new height */ public final void setHeight(final int heightVal) { this.height = heightVal; } /** * Gets the height. * * @return the height */ public final int getHeight() { if (this.height == 0) { this.height = DEFAULT_HEIGHT; } return this.height; } /** * Sets the scroll. * * @param scrollOption the new scroll */ public final void setScroll(final String scrollOption) { this.scroll = "no"; if (scrollOption != null) { if (scrollOption.compareToIgnoreCase("true") == 0) { this.scroll = "auto"; } } } /** * Gets the scroll. * * @return the scroll */ public final String getScroll() { if (this.scroll == null) { this.scroll = "no"; } return this.scroll; } /** * Sets the category. * * @param categoryVal the new category */ public final void setCategory(final String categoryVal) { this.category = categoryVal; } /** * Gets the category. * * @return the category */ public final String getCategory() { if (this.category == null) { this.category = ""; } return this.category; } /** * Sets the user preferences which is a hashmap for required/optional gadget * preferences. The String key is the name of the preference * The hashmap contains the following options: * - name of the gadget * - display.name: name to display * - default.value: default value of the preference * - datatype: The type of data = "string", "enum" * - required: true/false if option is required * - enumthis.values: ArrayList of String[] (value, display) for enum * * @param userPrefsMap the user preferences */ public final void setUserPreferences(final HashMap<String, HashMap<String, Object>> userPrefsMap) { this.userPreferences = userPrefsMap; } /** * Gets the user preferences which is a hashmap of required/optional gadget * preferences. The String key is the name of the preference. * The hashmap contains the following options: * - name: name of the gadget * - display.name: name to display * - default.value: default value of the preference * - datatype: The type of data = "string", "enum", "bool", "hidden" * - required: true/false if option is required * - enum.values: ArrayList of String[] (value, display) for enum * * @return the user preferences */ public final HashMap<String, HashMap<String, Object>> getUserPreferences() { if (this.userPreferences == null) { this.userPreferences = new HashMap<String, HashMap<String, Object>>(); } return this.userPreferences; } /** * Sets the xml. * * @param xmlVal the new xml */ public final void setXml(final String xmlVal) { this.xml = xmlVal; } /** * Gets the xml. * * @return the xml */ public final String getXml() { if (this.xml == null) { this.xml = ""; } return this.xml; } /** * Sets the created. * * @param createdDate the new created */ public final void setCreated(final Date createdDate) { if (createdDate != null) { this.created = new Date(createdDate.getTime()); } } /** * Gets the created. * * @return the created */ public final Date getCreated() { return this.created; } /** * Sets the created by. * * @param createdByVal the new created by */ public final void setCreatedBy(final String createdByVal) { this.createdBy = createdByVal; } /** * Gets the created by. * * @return the created by */ public final String getCreatedBy() { return this.createdBy; } /** * Embed url. * * @param gadgetPreferences the gadget preferences * * @return the string */ public final String embedUrl(final GadgetPreferencesBean gadgetPreferences) { StringBuffer output = new StringBuffer(); String divider = "&"; output.append("http://gmodules.com/ig/ifr?"); output.append("url="); output.append(this.getUrl()); output.append(divider); if (gadgetPreferences != null) { ArrayList<String> keys = gadgetPreferences.getUserPrefKeys(); if (keys != null) { for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); String value = getValue(key, gadgetPreferences); try { output.append(URLEncoder.encode("up_" + key, "utf-8")); output.append("="); output.append(URLEncoder.encode(value, "utf-8")); output.append(divider); } catch (UnsupportedEncodingException uee) { output.append("Encoding exception: " + uee.getMessage()); } } } } // Default border and closing options output.append("border=%23ffffff%7C3px%2C1px+solid+%23999999"); output.append(divider); output.append("synd=open"); output.append(divider); output.append("output=html"); return output.toString(); } /** * Read data. * * @param urlVal the url * @param categoryVal the category * * @throws JDOMException the JDOM exception * @throws IOException Signals that an I/O exception has occurred. */ public final void readData(final String urlVal, final String categoryVal) throws JDOMException, IOException { if (StringUtils.isNotBlank(urlVal)) { Document document = null; SAXBuilder builder = new SAXBuilder(); document = builder.build(urlVal); loadData(document, urlVal, categoryVal); } } /** * Read data. * * @param xmlString the xml string * @param urlVal the url * @param categoryVal the category * * @throws JDOMException the JDOM exception * @throws IOException Signals that an I/O exception has occurred. */ public final void readData(final String xmlString, final String urlVal, final String categoryVal) throws JDOMException, IOException { if (StringUtils.isNotBlank(urlVal)) { StringReader reader = new StringReader(xmlString); Document document = null; SAXBuilder builder = new SAXBuilder(); document = builder.build(reader); loadData(document, urlVal, categoryVal); } } /** * Checks the supplied url. If a url does not begin with http or https then * assume the url is http://www.google.com (unless it is an empty string). * * @param urlVal the url * * @return the string */ private String checkUrl(final String urlVal) { final StringBuffer returnUrl = new StringBuffer(); if (StringUtils.isNotBlank(urlVal)) { if (urlVal.startsWith("http")) { returnUrl.append(urlVal); } else { // The url does not start with http/https returnUrl.append("http://www.google.com"); if (!urlVal.startsWith("/")) { returnUrl.append("/"); } returnUrl.append(urlVal); } } return returnUrl.toString(); } /** * Get the value. * * @param keyVal the key * @param gadgetPreferences the gadget preferences * * @return the string */ private String getValue(final String keyVal, final GadgetPreferencesBean gadgetPreferences) { String value = ""; if (gadgetPreferences.getUserPref(keyVal) != null) { value = gadgetPreferences.getUserPref(keyVal); } HashMap<String, HashMap<String, Object>> userPrefs = this.getUserPreferences(); if (userPrefs != null) { if (userPrefs.containsKey(keyVal)) { HashMap<String, Object> userPref = userPrefs.get(keyVal); String dataType = (String) userPref.get("datatype"); if (dataType != null) { if (dataType.compareToIgnoreCase("bool") == 0) { if (value.compareToIgnoreCase("true") == 0) { value = "1"; } else { value = "0"; } } } } } return value; } /** * Load the data. * * @param document the document * @param urlVal the url * @param categoryVal the category * * @throws JDOMException the JDOM exception * @throws IOException Signals that an I/O exception has occurred. */ private void loadData(final Document document, final String urlVal, final String categoryVal) throws JDOMException, IOException { Element modulePrefs = document.getRootElement().getChild("ModulePrefs"); if (StringUtils.isBlank(this.getTitle())) { this.setTitle(modulePrefs.getAttributeValue("title")); } this.setCategory(categoryVal); if (StringUtils.isBlank(this.getDescription())) { try { this.setDescription(modulePrefs.getAttributeValue("description")); } catch (Exception e) { // Error getting description this.setDescription(""); } } if (StringUtils.equals(this.getAuthor(), "Unknown")) { try { this.setAuthor(modulePrefs.getAttributeValue("author")); } catch (Exception e) { // Error getting author this.setAuthor(""); } } if (StringUtils.isBlank(this.getThumbnailUrl())) { try { this.setThumbnailUrl(modulePrefs.getAttributeValue("thumbnail")); } catch (Exception e) { // Error getting screenshot this.setThumbnailUrl(""); } } if (StringUtils.isBlank(this.getScreenshotUrl())) { try { this.setScreenshotUrl(modulePrefs.getAttributeValue("screenshot")); } catch (Exception e) { // Error getting screenshot this.setScreenshotUrl(""); } } try { this.setHeight(Integer.parseInt(modulePrefs.getAttributeValue("height"))); } catch (Exception e) { // Error getting height this.setHeight(DEFAULT_HEIGHT); } try { this.setWidth(Integer.parseInt(modulePrefs.getAttributeValue("width"))); } catch (Exception e) { // Error getting width this.setWidth(DEFAULT_WIDTH); } try { this.setScroll(modulePrefs.getAttributeValue("scrolling")); } catch (Exception e) { // Error getting width this.setScroll("false"); } this.setUrl(urlVal); // Load user preferences hash HashMap<String, HashMap<String, Object>> preferences = new HashMap<String, HashMap<String, Object>>(); List<?> userprefs = document.getRootElement().getChildren("UserPref"); if (userprefs != null) { for (int i = 0; i < userprefs.size(); i++) { HashMap<String, Object> preference = new HashMap<String, Object>(); Element userpref = (Element) userprefs.get(i); String key = userpref.getAttributeValue("name"); preference.put("name", key); preference.put("display_name", userpref.getAttributeValue("display_name")); try { preference.put("default_value", userpref.getAttributeValue("default_value")); } catch (Exception e) { // Error parsing default_value preference.put("default_value", ""); } try { if (userpref.getAttributeValue("datatype") != null) { preference.put("datatype", userpref.getAttributeValue("datatype")); } else { preference.put("datatype", "string"); } } catch (Exception e) { // Error parsing user preference preference.put("datatype", "string"); } try { if (userpref.getAttributeValue("required") != null) { preference.put("required", userpref.getAttributeValue("required")); } else { preference.put("required", "false"); } } catch (Exception e) { // Error parsing user preference preference.put("required", "false"); } ArrayList<String[]> enumValues = new ArrayList<String[]>(); try { // Try parsing any EnumValue elements that may exist List<?> enumvalues = userpref.getChildren("EnumValue"); if (enumvalues != null) { for (int x = 0; x < enumvalues.size(); x++) { Element enumvalue = (Element) enumvalues.get(x); String value = enumvalue.getAttributeValue("value"); String display = enumvalue.getAttributeValue("display_value"); if (display != null) { if (display.compareTo("") == 0) { display = value; } } else { display = value; } String[] pair = { value, display }; enumValues.add(pair); } } } catch (Exception e) { // Error parsing these elements or maybe none existed enumValues = new ArrayList<String[]>(); } preference.put("enum_values", enumValues); preferences.put(key, preference); } } this.setUserPreferences(preferences); XMLOutputter output = new XMLOutputter(); output.setFormat(Format.getPrettyFormat()); this.setXml(output.outputString(document)); } }