1 11 12 package org.eclipse.pde.internal.core.util; 13 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 20 24 public class XMLComponentRegistry { 25 26 private static XMLComponentRegistry fPinstance; 27 28 31 public static final String F_NAME = "n"; 33 36 public static final String F_DESCRIPTION = "d"; 38 41 public static final int F_SCHEMA_COMPONENT = 2; 42 43 46 public static final int F_ELEMENT_COMPONENT = 4; 47 48 51 public static final int F_ATTRIBUTE_COMPONENT = 8; 52 53 private static ArrayList fConsumers = new ArrayList (); 54 55 private Map fSchemaComponentMap; 56 57 private Map fAttributeComponentMap; 58 59 private Map fElementComponentMap; 60 61 private XMLComponentRegistry() { 62 fSchemaComponentMap = Collections.synchronizedMap(new HashMap ()); 63 fAttributeComponentMap = Collections.synchronizedMap(new HashMap ()); 64 fElementComponentMap = Collections.synchronizedMap(new HashMap ()); 65 } 66 67 public static XMLComponentRegistry Instance() { 68 if (fPinstance == null) { 69 fPinstance = new XMLComponentRegistry(); 70 } 71 return fPinstance; 72 } 73 74 public void dispose() { 75 if (fSchemaComponentMap != null) { 76 fSchemaComponentMap.clear(); 77 } 78 if (fAttributeComponentMap != null) { 79 fAttributeComponentMap.clear(); 80 } 81 if (fElementComponentMap != null) { 82 fElementComponentMap.clear(); 83 } 84 } 85 86 public void putDescription(String key, String value, int mapType) { 87 putValue(F_DESCRIPTION, key, value, mapType); 88 } 89 90 public void putName(String key, String value, int mapType) { 91 putValue(F_NAME, key, value, mapType); 92 } 93 94 private Map getTargetMap(int mask) { 95 Map targetMap = null; 96 if (mask == F_SCHEMA_COMPONENT) { 97 targetMap = fSchemaComponentMap; 98 } else if (mask == F_ATTRIBUTE_COMPONENT) { 99 targetMap = fAttributeComponentMap; 100 } else if (mask == F_ELEMENT_COMPONENT) { 101 targetMap = fElementComponentMap; 102 } 103 return targetMap; 104 } 105 106 private void putValue(String valueKey, String key, String value, int mapType) { 107 if (key != null) { 108 Map targetMap = getTargetMap(mapType); 109 if (targetMap == null) { 110 return; 111 } 112 HashMap previousValue = (HashMap )targetMap.get(key); 113 if (previousValue == null) { 114 HashMap newValue = new HashMap (); 115 newValue.put(valueKey, value); 116 targetMap.put(key, newValue); 117 } else { 118 previousValue.put(valueKey, value); 119 } 120 } 121 } 122 123 public void put(String key, HashMap value, int mapType) { 124 if (key != null) { 125 Map targetMap = getTargetMap(mapType); 126 if (targetMap == null) { 127 return; 128 } 129 targetMap.put(key, value); 130 } 131 } 132 133 public HashMap get(String key, int mapType) { 134 Map targetMap = getTargetMap(mapType); 135 if (targetMap == null) { 136 return null; 137 } 138 return (HashMap )targetMap.get(key); 139 } 140 141 private String getValue(String valueKey, String key, int mapType) { 142 if (key != null) { 143 HashMap map = get(key, mapType); 144 if (map != null) { 145 return (String )map.get(valueKey); 146 } 147 } 148 return null; 149 } 150 151 public String getDescription(String key, int mapType) { 152 return getValue(F_DESCRIPTION, key, mapType); 153 } 154 155 public String getName(String key, int mapType) { 156 return getValue(F_NAME, key, mapType); 157 } 158 159 public void connect(Object consumer) { 160 if (!fConsumers.contains(consumer)) { 161 fConsumers.add(consumer); 162 } 163 } 164 165 public void disconnect(Object consumer) { 166 fConsumers.remove(consumer); 167 if (fConsumers.size() == 0) { 168 dispose(); 169 } 170 } 171 172 } 173 | Popular Tags |