Coverage Report - net.sf.jolene.html.Attributes
 
Classes in this File Line Coverage Branch Coverage Complexity
Attributes
83%
20/24
90%
9/10
0
 
 1  
 /*
 2  
  * Created on Dec 26, 2003
 3  
 */
 4  
 package net.sf.jolene.html;
 5  
 
 6  
 import java.util.*;
 7  
 
 8  
 
 9  
 /**
 10  
  * Map wrapper to handle html attributes. It's a case insensitive sorted map.
 11  
  *
 12  
  * @author Dan Howard
 13  
  */
 14  
 public class Attributes implements IAttributes {
 15  
 
 16  
     private SortedMap<String, String> attributes;
 17  
 
 18  
     /**
 19  
      *
 20  
      */
 21  2618
     public Attributes() {
 22  2618
         attributes = new TreeMap<String, String>();
 23  2618
     }
 24  
 
 25  
     /**
 26  
      * Sets an attribute.
 27  
      *
 28  
      * @param key   - attribute name
 29  
      * @param value - attribute value
 30  
      * @return String
 31  
      */
 32  
     public String setAttribute(String key, String value) {
 33  
         // Don't allow null values
 34  5567
         if (value == null) {
 35  1
             value = "";
 36  
         }
 37  5567
         return attributes.put(key.toLowerCase(), value);
 38  
     }
 39  
 
 40  
     /**
 41  
      * Gets an attribute from the attribute map or NULL if not found.
 42  
      *
 43  
      * @param key - attribute name
 44  
      * @return String or NULL of key is null or not found
 45  
      */
 46  
     public String getAttribute(String key) {
 47  6767
         if (key == null) {
 48  0
             return null;
 49  
         }
 50  6767
         return attributes.get(key.toLowerCase());
 51  
     }
 52  
 
 53  
     /**
 54  
      * Remove an attribute.
 55  
      *
 56  
      * @param key - attribute name
 57  
      * @return String
 58  
      */
 59  
     public String removeAttribute(String key) {
 60  2
         return attributes.remove(key.toLowerCase());
 61  
     }
 62  
 
 63  
     /**
 64  
      * Indicates whether or not an attribute exists.
 65  
      *
 66  
      * @param key - attribute name
 67  
      * @return boolean
 68  
      */
 69  
     public boolean hasAttribute(String key) {
 70  2165
         if (key == null) {
 71  2
             return false;
 72  
         }
 73  2163
         return getAttribute(key.toLowerCase()) != null;
 74  
     }
 75  
 
 76  
     /**
 77  
      * Returns the attributes as a Set.
 78  
      *
 79  
      * @return Set
 80  
      */
 81  
     public Set<String> keySet() {
 82  1227
         return attributes.keySet();
 83  
     }
 84  
 
 85  
     /**
 86  
      * Returns the attribute values as a Collection.
 87  
      *
 88  
      * @return Collection
 89  
      */
 90  
     public Collection<String> values() {
 91  0
         return attributes.values();
 92  
     }
 93  
 
 94  
 
 95  
     /**
 96  
      * Clears the attributes.
 97  
      */
 98  
     public void clear() {
 99  0
         attributes.clear();
 100  0
     }
 101  
 
 102  
     @Override
 103  
     public String toString() {
 104  1
         Iterator<String> it = keySet().iterator();
 105  1
         StringBuilder s = new StringBuilder("");
 106  4
         while (it.hasNext()) {
 107  3
             String key = it.next();
 108  3
             s.append(key).append('=').append(getAttribute(key)).append(' ');
 109  3
         }
 110  1
         return s.toString();
 111  
     }
 112  
 }