KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > BundleHeaders


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

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 // A utility class used to store the headers that are specified in a bundle's
35
// jar file.
36
// This class keeps a mapping between bundle header names (which are case insensitive)
37
// and bundle header values.
38
public class BundleHeaders extends Dictionary {
39
40    // Hashtable of headers: headerName (in lower-case) --> header value
41
private Hashtable headers;
42
43    protected BundleHeaders( Hashtable attributes ) {
44       // Create the hashtable of headers.
45
headers = new Hashtable();
46       if ( attributes != null ) {
47          // If the set of attributes is not null, add each attribute to the hashtable of
48
// headers.
49
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    // Implementation of the Dictionary interface's get() method.
59
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    // Implementation of the Dictionary interface's isEmpty() method.
70
public boolean isEmpty() {
71       return headers.isEmpty();
72    }
73
74    // Implementation of the Dictionary interface's elements() method.
75
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    // Implementation of the Dictionary interface's keys() method.
85
//
86
// Return keys in the correct case
87
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    // Implementation of the Dictionary interface's put() method.
97
public Object put( Object key, Object value ) {
98       Object ret = get( key );
99
100       if ( key instanceof String && value instanceof String ) {
101          // Process the attribute key, so that header names are case-insensitive.
102
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    // Implementation of the Dictionary interface's remove() method.
112
public Object remove( Object key ) {
113       Object ret = get( key );
114
115       headers.remove( processKey( key ) );
116
117       return ret;
118    }
119
120    // Implementation of the Dictionary interface's size() method.
121
public int size() {
122       return headers.size();
123    }
124
125    // Process the attribute key, so that header names are case-insensitive.
126
// If the key is not a string, throw an IllegalArgumentException.
127
// Otherwise, convert the key to lower case.
128
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