| 1 10 11 package org.mmbase.datatypes; 12 13 import java.util.*; 14 import org.mmbase.datatypes.util.xml.DataTypeDefinition; 15 import org.mmbase.util.logging.*; 16 17 30 31 public final class DataTypeCollector { 32 33 private static final Logger log = Logging.getLoggerInstance(DataTypeCollector.class); 34 35 private Map dataTypes = new HashMap(); private Map specializations = new HashMap(); private Set roots = new HashSet(); 40 private Object signature = null; 42 43 private List collectors = new ArrayList(); 45 46 private static DataTypeCollector systemDataTypeCollector; 48 49 53 static DataTypeCollector createSystemDataTypeCollector() { 54 if (systemDataTypeCollector == null) { 55 Object signature = new String ( "SYSTEM_" + System.currentTimeMillis()); 56 systemDataTypeCollector = new DataTypeCollector(signature); 57 return systemDataTypeCollector; 58 } else { 59 throw new SecurityException ("System datatype collector already defined, may not be created twice."); 60 } 61 } 62 63 67 public DataTypeCollector(Object signature) { 68 this.signature = signature; 69 } 70 71 public DataTypeDefinition getDataTypeDefinition() { 72 return new DataTypeDefinition(this); 73 } 74 75 82 public void addCollector(DataTypeCollector collector) { 83 collectors.add(collector); 84 } 85 86 90 public void setDataTypes(Map dataTypes) { 91 this.dataTypes = dataTypes; 92 if (log.isDebugEnabled()) log.debug("DataTypes for collector with signature " + signature + ":" +dataTypes); 93 } 94 95 98 public Map getDataTypes() { 99 return dataTypes; 100 } 101 102 108 public BasicDataType getDataType(String name) { 109 return getDataType(name, false); 110 } 111 112 120 public BasicDataType addDataType(BasicDataType dataType) { 121 String name = dataType.getName(); 122 if (name == null || "".equals(name)) { 123 return null; 125 } else { 126 DataType origin = dataType.getOrigin(); 127 if (origin != null) { 128 if (origin.equals(getDataType(origin.getName()))) { Set spec = (Set) specializations.get(origin.getName()); 130 if (spec == null) { 133 spec = new HashSet(); 134 specializations.put(origin.getName(), spec); 135 } 136 spec.add(dataType); 137 } else { 138 roots.add(dataType); 139 } 140 } else { 141 roots.add(dataType); 142 } 143 BasicDataType old = (BasicDataType) dataTypes.put(name, dataType); 144 if (old != null && old != dataType) { 145 log.warn("Replaced " + name + " " + old + " with " + dataType); 146 } 147 return old; 148 } 149 } 150 151 154 public Collection getSpecializations(String name) { 155 Set set = (Set) specializations.get(name); 157 return set == null ? Collections.EMPTY_SET : Collections.unmodifiableSet(set); 158 } 159 160 163 public Iterator getAllSpecializations(String name) { 164 final Iterator i = getSpecializations(name).iterator(); 165 return new Iterator() { 166 DataType next = i.hasNext() ? (DataType) i.next() : null; 167 Iterator subIterator = null; 168 public boolean hasNext() { 169 return next != null || subIterator != null; 170 } 171 public Object next() { 172 if (subIterator != null) { 173 Object n = subIterator.next(); 174 if (! subIterator.hasNext()) subIterator = null; 175 return n; 176 } 177 if (next != null) { 178 subIterator = getAllSpecializations(next.getName()); 179 if (! subIterator.hasNext()) subIterator = null; 180 Object n = next; 181 if (i.hasNext()) { 182 next = (DataType) i.next(); 183 } else { 184 next = null; 185 } 186 return n; 187 } 188 throw new NoSuchElementException(); 189 } 190 public void remove() { 191 throw new UnsupportedOperationException (); 192 } 193 }; 194 } 195 198 public Set getRoots() { 199 return Collections.unmodifiableSet(roots); 201 } 202 203 213 public BasicDataType getDataType(String name, boolean recursive) { 214 BasicDataType dataType = (BasicDataType) dataTypes.get(name); 215 if (this != systemDataTypeCollector && dataType == null && recursive) { 216 for (Iterator i = collectors.iterator(); dataType == null && i.hasNext();) { 217 DataTypeCollector collector = (DataTypeCollector) i.next(); 218 dataType = collector.getDataType(name, true); 219 } 220 if (dataType == null) { 221 dataType = systemDataTypeCollector.getDataType(name, false); 222 } 223 } 224 return dataType; 225 } 226 227 238 public BasicDataType getDataTypeInstance(String name, BasicDataType baseDataType) { 239 BasicDataType dataType = getDataType(name, true); 240 if (dataType == null && baseDataType == null) { 241 return null; 242 } else if (dataType == null) { 243 return (BasicDataType)baseDataType.clone(name); 244 } else { 245 return (BasicDataType)dataType.clone(); 246 } 247 } 248 249 252 public boolean contains(String name) { 253 return dataTypes.containsKey(name); 254 } 255 256 259 public boolean contains(DataType dataType) { 260 return dataTypes.containsValue(dataType); 261 } 262 263 267 public void rewrite(DataType dataType) { 268 dataType.rewrite(signature); 269 } 270 271 275 public void finish(DataType dataType) { 276 dataType.finish(signature); 277 } 278 279 public String toString() { 280 return signature + ": " + dataTypes.values() + " " + collectors; 281 } 282 283 } 284 285 | Popular Tags |