001 package net.sf.jolene.html; 002 003 import java.util.Collection; 004 import java.util.Set; 005 import java.util.SortedMap; 006 import java.util.TreeMap; 007 008 /** 009 * Class to manage a style string. 010 * 011 * @author Dan Howard 012 * @since Aug 3, 2008 6:09:50 PM 013 */ 014 public final class Styles implements IStyles { 015 016 private SortedMap<String, String> styles; 017 018 019 /** 020 * Default style constructor 021 */ 022 public Styles() { 023 styles = new TreeMap<String, String>(); 024 } 025 026 027 /** 028 * Construct a style object passing in formatted style string. 029 * 030 * @param styles formatted style string. 031 */ 032 public Styles(String styles) { 033 this(); 034 setStyles(styles); 035 } 036 037 /** 038 * Get a style based on a style name. 039 * 040 * @param key case insensitive style name. 041 * @return String value for the specified style. 042 */ 043 public String getStyle(String key) { 044 if (key == null) { 045 return null; 046 } 047 return styles.get(key.toLowerCase()); 048 049 050 } 051 052 /** 053 * Indicates if a style exists. 054 * 055 * @param key case instensitive style key name. 056 * @return true if the style exists in the string. 057 */ 058 public boolean hasStyle(String key) { 059 if (key == null) { 060 return false; 061 } 062 return getStyle(key.toLowerCase()) != null; 063 064 } 065 066 067 /** 068 * Returns a set of style keys from the Style. 069 * 070 * @return set of style keys from the Style. 071 */ 072 public Set<String> keySet() { 073 return styles.keySet(); 074 } 075 076 /** 077 * Removes a style from the Style object. 078 * 079 * @param key Style key name to remove. 080 * @return previous value associated with specified key, or <tt>null</tt> 081 * if there was no mapping for key. 082 */ 083 public String removeStyle(String key) { 084 return styles.remove(key.toLowerCase()); 085 } 086 087 088 /** 089 * Clears the styles. 090 */ 091 public void clear() { 092 styles.clear(); 093 } 094 095 /** 096 * Set a style on the Style object. 097 * 098 * @param key Style key name. 099 * @param value Style value. 100 * @return previous value associated with specified key, or <tt>null</tt> if there was no mapping for key. 101 */ 102 public String setStyle(String key, String value) { 103 104 // Don't allow null values 105 if (value == null) { 106 value = ""; 107 } 108 109 return styles.put(key.toLowerCase(), value); 110 } 111 112 113 /** 114 * Sets styles on the Style object based on a formatted style string. 115 * Expects the string to be in style format. 116 * 117 * @param style string in a format like <tt>azimuth:behind;background:aliceblue;background-color:aquamarine;border-bottom-style:solid;clip:auto;border-top-width:medium;</tt> 118 */ 119 public void setStyles(String style) { 120 String astyles[] = style.split(";"); 121 for (int j = 0; j < astyles.length; j++) { 122 String astyle[] = astyles[j].split(":"); 123 if (astyle.length == 2) { 124 setStyle(astyle[0].trim(), astyle[1].trim()); 125 } 126 } 127 } 128 129 /** 130 * Sets the style based on the formatted style string, clearing all exisiting styles first. 131 * 132 * @param style string in a format like <tt>azimuth:behind;background:aliceblue;background-color:aquamarine;border-bottom-style:solid;clip:auto;border-top-width:medium;</tt> 133 */ 134 public void resetStyles(String style) { 135 clear(); 136 setStyles(style); 137 } 138 139 /** 140 * Returns a Collection of Style values. 141 * 142 * @return Collection of Style values. 143 */ 144 public Collection<String> values() { 145 return styles.values(); 146 } 147 148 149 /** 150 * Renders this Style as a style string. 151 * 152 * @return a string formatted for a style attribute. 153 */ 154 @Override 155 public String toString() { 156 StringBuilder sb = new StringBuilder(); 157 158 Set<String> keys = keySet(); 159 for (String key : keys) { 160 sb.append(key).append(":").append(getStyle(key)).append(";"); 161 } 162 return sb.toString(); 163 } 164 }