1 //======================================================================== 2 //$Id: AttributesMap.java,v 1.3 2005/11/14 17:45:52 gregwilkins Exp $ 3 //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd. 4 //------------------------------------------------------------------------ 5 //Licensed under the Apache License, Version 2.0 (the "License"); 6 //you may not use this file except in compliance with the License. 7 //You may obtain a copy of the License at 8 //http://www.apache.org/licenses/LICENSE-2.0 9 //Unless required by applicable law or agreed to in writing, software 10 //distributed under the License is distributed on an "AS IS" BASIS, 11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 //See the License for the specific language governing permissions and 13 //limitations under the License. 14 //======================================================================== 15 16 package org.mortbay.util; 17 18 import java.util.ArrayList; 19 import java.util.Collections; 20 import java.util.Enumeration; 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /* ------------------------------------------------------------ */ 25 /** AttributesMap. 26 * @author gregw 27 * 28 */ 29 public class AttributesMap implements Attributes 30 { 31 Map _map; 32 33 /* ------------------------------------------------------------ */ 34 public AttributesMap() 35 { 36 _map=new HashMap(); 37 } 38 39 /* ------------------------------------------------------------ */ 40 public AttributesMap(Map map) 41 { 42 _map=map; 43 } 44 45 /* ------------------------------------------------------------ */ 46 /* 47 * @see org.mortbay.util.Attributes#removeAttribute(java.lang.String) 48 */ 49 public void removeAttribute(String name) 50 { 51 _map.remove(name); 52 } 53 54 /* ------------------------------------------------------------ */ 55 /* 56 * @see org.mortbay.util.Attributes#setAttribute(java.lang.String, java.lang.Object) 57 */ 58 public void setAttribute(String name, Object attribute) 59 { 60 if (attribute==null) 61 _map.remove(name); 62 else 63 _map.put(name, attribute); 64 } 65 66 /* ------------------------------------------------------------ */ 67 /* 68 * @see org.mortbay.util.Attributes#getAttribute(java.lang.String) 69 */ 70 public Object getAttribute(String name) 71 { 72 return _map.get(name); 73 } 74 75 /* ------------------------------------------------------------ */ 76 /* 77 * @see org.mortbay.util.Attributes#getAttributeNames() 78 */ 79 public Enumeration getAttributeNames() 80 { 81 return Collections.enumeration(_map.keySet()); 82 } 83 84 /* ------------------------------------------------------------ */ 85 /* 86 * @see org.mortbay.util.Attributes#getAttributeNames() 87 */ 88 public static Enumeration getAttributeNamesCopy(Attributes attrs) 89 { 90 if (attrs instanceof AttributesMap) 91 return Collections.enumeration(((AttributesMap)attrs)._map.keySet()); 92 ArrayList names = new ArrayList(); 93 Enumeration e = attrs.getAttributeNames(); 94 while (e.hasMoreElements()) 95 names.add(e.nextElement()); 96 return Collections.enumeration(names); 97 } 98 99 /* ------------------------------------------------------------ */ 100 /* 101 * @see org.mortbay.util.Attributes#clear() 102 */ 103 public void clearAttributes() 104 { 105 _map.clear(); 106 } 107 108 /* ------------------------------------------------------------ */ 109 public String toString() 110 { 111 return _map.toString(); 112 } 113 114 115 }