1 26 27 package com.opensugar.cube; 28 29 import java.util.Dictionary; 30 import java.util.Enumeration; 31 import java.util.Hashtable; 32 import java.util.Vector; 33 34 public class BundleHeaders extends Dictionary { 39 40 private Hashtable headers; 42 43 protected BundleHeaders( Hashtable attributes ) { 44 headers = new Hashtable(); 46 if ( attributes != null ) { 47 Enumeration enum = attributes.keys(); 50 String key; 51 while ( enum.hasMoreElements() ) { 52 key = enum.nextElement().toString(); 53 put( key, attributes.get( key ) ); 54 } 55 } 56 } 57 58 public Object get( Object key ) { 60 Header header = (Header)headers.get( processKey( key ) ); 61 if ( header == null ) { 62 return null; 63 } 64 else { 65 return header.getValue(); 66 } 67 } 68 69 public boolean isEmpty() { 71 return headers.isEmpty(); 72 } 73 74 public Enumeration elements() { 76 Enumeration enum = headers.elements(); 77 Vector vec = new Vector(); 78 while ( enum.hasMoreElements() ) { 79 vec.addElement( ( (Header)enum.nextElement() ).getValue() ); 80 } 81 return vec.elements(); 82 } 83 84 public Enumeration keys() { 88 Enumeration enum = headers.elements(); 89 Vector vec = new Vector(); 90 while ( enum.hasMoreElements() ) { 91 vec.addElement( ( (Header)enum.nextElement() ).getKey() ); 92 } 93 return vec.elements(); 94 } 95 96 public Object put( Object key, Object value ) { 98 Object ret = get( key ); 99 100 if ( key instanceof String && value instanceof String ) { 101 headers.put( processKey( key ), new Header( (String)key, (String)value ) ); 103 104 return ret; 105 } 106 else { 107 throw new IllegalArgumentException( "Key and value must be of type String" ); 108 } 109 } 110 111 public Object remove( Object key ) { 113 Object ret = get( key ); 114 115 headers.remove( processKey( key ) ); 116 117 return ret; 118 } 119 120 public int size() { 122 return headers.size(); 123 } 124 125 private String processKey( Object key ) { 129 if ( key instanceof String ) { 130 return ( (String)key ).toLowerCase(); 131 } 132 throw new IllegalArgumentException( "Key must be of type String but is of type: " + key.getClass().getName() ); 133 } 134 135 private class Header { 136 private String key; 137 private String value; 138 public Header( String key, String value ) { 139 this.key = key; 140 this.value = value; 141 } 142 public String getKey() { 143 return key; 144 } 145 public String getValue() { 146 return value; 147 } 148 } 149 150 } | Popular Tags |