Coverage Report - net.sf.jolene.html.Styles
 
Classes in this File Line Coverage Branch Coverage Complexity
Styles
88%
30/34
71%
10/14
0
 
 1  
 package net.sf.jolene.html;
 2  
 
 3  
 import java.util.Collection;
 4  
 import java.util.Set;
 5  
 import java.util.SortedMap;
 6  
 import java.util.TreeMap;
 7  
 
 8  
 /**
 9  
  * Class to manage a style string.
 10  
  *
 11  
  * @author Dan Howard
 12  
  * @since Aug 3, 2008 6:09:50 PM
 13  
  */
 14  
 public final class Styles implements IStyles {
 15  
 
 16  
     private SortedMap<String, String> styles;
 17  
 
 18  
 
 19  
     /**
 20  
      * Default style constructor
 21  
      */
 22  674
     public Styles() {
 23  674
         styles = new TreeMap<String, String>();
 24  674
     }
 25  
 
 26  
 
 27  
     /**
 28  
      * Construct a style object passing in formatted style string.
 29  
      *
 30  
      * @param styles formatted style string.
 31  
      */
 32  
     public Styles(String styles) {
 33  2
         this();
 34  2
         setStyles(styles);
 35  2
     }
 36  
 
 37  
     /**
 38  
      * Get a style based on a style name.
 39  
      *
 40  
      * @param key case insensitive style name.
 41  
      * @return String value for the specified style.
 42  
      */
 43  
     public String getStyle(String key) {
 44  79
         if (key == null) {
 45  0
             return null;
 46  
         }
 47  79
         return styles.get(key.toLowerCase());
 48  
 
 49  
 
 50  
     }
 51  
 
 52  
     /**
 53  
      * Indicates if a style exists.
 54  
      *
 55  
      * @param key case instensitive style key name.
 56  
      * @return true if the style exists in the string.
 57  
      */
 58  
     public boolean hasStyle(String key) {
 59  2
         if (key == null) {
 60  0
             return false;
 61  
         }
 62  2
         return getStyle(key.toLowerCase()) != null;
 63  
 
 64  
     }
 65  
 
 66  
 
 67  
     /**
 68  
      * Returns a set of style keys from the Style.
 69  
      *
 70  
      * @return set of style keys from the Style.
 71  
      */
 72  
     public Set<String> keySet() {
 73  30
         return styles.keySet();
 74  
     }
 75  
 
 76  
     /**
 77  
      * Removes a style from the Style object.
 78  
      *
 79  
      * @param key Style key name to remove.
 80  
      * @return previous value associated with specified key, or <tt>null</tt>
 81  
      *         if there was no mapping for key.
 82  
      */
 83  
     public String removeStyle(String key) {
 84  1
         return styles.remove(key.toLowerCase());
 85  
     }
 86  
 
 87  
 
 88  
     /**
 89  
      * Clears the styles.
 90  
      */
 91  
     public void clear() {
 92  3
         styles.clear();
 93  3
     }
 94  
 
 95  
     /**
 96  
      * Set a style on the Style object.
 97  
      *
 98  
      * @param key   Style key name.
 99  
      * @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  47
         if (value == null) {
 106  0
             value = "";
 107  
         }
 108  
 
 109  47
         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  18
         String astyles[] = style.split(";");
 121  58
         for (int j = 0; j < astyles.length; j++) {
 122  40
             String astyle[] = astyles[j].split(":");
 123  40
             if (astyle.length == 2) {
 124  40
                 setStyle(astyle[0].trim(), astyle[1].trim());
 125  
             }
 126  
         }
 127  18
     }
 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  3
         clear();
 136  3
         setStyles(style);
 137  3
     }
 138  
 
 139  
     /**
 140  
      * Returns a Collection of Style values.
 141  
      *
 142  
      * @return Collection of Style values.
 143  
      */
 144  
     public Collection<String> values() {
 145  0
         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  25
         StringBuilder sb = new StringBuilder();
 157  
 
 158  25
         Set<String> keys = keySet();
 159  25
         for (String key : keys) {
 160  74
             sb.append(key).append(":").append(getStyle(key)).append(";");
 161  
         }
 162  25
         return sb.toString();
 163  
     }
 164  
 }