1 10 package org.mmbase.storage.util; 11 12 import java.util.*; 13 14 import org.w3c.dom.*; 15 import org.xml.sax.InputSource ; 16 17 import org.mmbase.storage.*; 18 import org.mmbase.util.xml.DocumentReader; 19 import org.mmbase.util.logging.*; 20 21 27 public class StorageReader extends DocumentReader { 28 29 private static final Logger log = Logging.getLoggerInstance(StorageReader.class); 30 31 32 public static final String PUBLIC_ID_STORAGE_1_0 = "-//MMBase//DTD storage config 1.0//EN"; 33 34 public static final String DTD_STORAGE_1_0 = "storage_1_0.dtd"; 35 36 37 public static final String PUBLIC_ID_STORAGE = PUBLIC_ID_STORAGE_1_0; 38 39 public static final String DTD_STORAGE = DTD_STORAGE_1_0; 40 41 static { 42 org.mmbase.util.XMLEntityResolver.registerPublicID(PUBLIC_ID_STORAGE_1_0, DTD_STORAGE_1_0, StorageReader.class); 43 } 44 45 50 protected StorageManagerFactory factory; 51 52 59 public StorageReader(StorageManagerFactory factory, InputSource source) { 60 super(source, DocumentReader.validate(), StorageReader.class); 61 this.factory = factory; 62 } 63 64 70 public Class getStorageManagerClass() throws StorageConfigurationException { 71 Element root = document.getDocumentElement(); 72 if (factory != null) { 73 NodeList factoryTagList = root.getElementsByTagName("storagemanagerfactory"); 76 if (factoryTagList.getLength()>0) { 77 Element factoryTag = (Element)factoryTagList.item(0); 78 try { 79 String factoryClassName = factoryTag.getAttribute("classname"); 81 Class factoryClass = Class.forName(factoryClassName); 82 if (!factoryClass.isInstance(factory)) { 83 throw new StorageConfigurationException("StorageManager Configuration requires factory class '"+factoryClassName+"'."); 84 } 85 String storageManagerFactoryVersion = factoryTag.getAttribute("version"); 87 if (storageManagerFactoryVersion != null) { 88 double version = Double.parseDouble(storageManagerFactoryVersion); 89 if (version > factory.getVersion()) { 90 throw new StorageConfigurationException("StorageManager Configuration requires factory version '"+version+", found "+factory.getVersion()+"."); 91 } 92 } 93 } catch (NumberFormatException pe) { 94 throw new StorageConfigurationException(pe); } catch (ClassNotFoundException cnfe) { 96 throw new StorageConfigurationException(cnfe); } 98 } 99 } 100 NodeList managerTagList = root.getElementsByTagName("storagemanager"); 101 if (managerTagList.getLength()>0) { 102 Element managerTag = (Element)managerTagList.item(0); 103 String managerClassName = managerTag.getAttribute("classname"); 104 try { 106 Class managerClass = Class.forName(managerClassName); 107 StorageManager manager = (StorageManager)managerClass.newInstance(); 108 String storageManagerVersion = managerTag.getAttribute("version"); 110 if (storageManagerVersion != null) { 111 double version = Double.parseDouble(storageManagerVersion); 112 if (version > manager.getVersion()) { 113 throw new StorageConfigurationException("StorageManager Configuration requires storage manager version '"+version+", found "+manager.getVersion()+"."); 114 } 115 } 116 return managerClass; 117 } catch (NumberFormatException pe) { 118 throw new StorageConfigurationException(pe); } catch (ClassNotFoundException cnfe) { 120 throw new StorageConfigurationException(cnfe); 121 } catch (IllegalAccessException iae) { 122 throw new StorageConfigurationException(iae); 123 } catch (InstantiationException ie) { 124 throw new StorageConfigurationException(ie); 125 } 126 } else { 127 return null; 128 } 129 } 130 131 138 public List getSearchQueryHandlerClasses() throws StorageConfigurationException { 139 List classes = new ArrayList(); 141 Element root = document.getDocumentElement(); 142 NodeList handlerTagList = root.getElementsByTagName("searchqueryhandler"); 143 for(int i=0; i<handlerTagList.getLength(); i++) { 144 Element handlerTag = (Element)handlerTagList.item(i); 145 String queryHandlerClassName = handlerTag.getAttribute("classname"); 146 try { 148 classes.add(Class.forName(queryHandlerClassName)); 149 } catch (ClassNotFoundException cnfe) { 150 throw new StorageConfigurationException(cnfe); 151 } 152 } 153 return classes; 154 } 155 156 164 public Map getAttributes() { 165 Map attributes = new HashMap(); 166 Element root = document.getDocumentElement(); 167 NodeList attributesTagList = root.getElementsByTagName("attributes"); 168 if (attributesTagList.getLength()>0) { 169 Element attributesTag = (Element)attributesTagList.item(0); 170 NodeList attributeTagList = attributesTag.getElementsByTagName("attribute"); 171 for (int i=0; i<attributeTagList.getLength(); i++) { 172 Element attributeTag = (Element)attributeTagList.item(i); 173 String attributeName = attributeTag.getAttribute("name"); 174 if (attributeName != null) { 177 attributes.put(attributeName,getNodeTextValue(attributeTag)); 178 } 179 } 180 NodeList optionTagList = attributesTag.getElementsByTagName("option"); 181 for (int i=0; i<optionTagList.getLength(); i++) { 182 Element optionTag = (Element)optionTagList.item(i); 183 String optionName = optionTag.getAttribute("name"); 186 if (optionName != null) { 187 String optionValue = optionTag.getAttribute("value"); 188 Boolean value = Boolean.TRUE; 189 if (optionValue != null && !optionValue.equals("")) { 190 value = Boolean.valueOf(optionValue); 191 } 192 attributes.put(optionName,value); 193 } 194 } 195 NodeList schemeTagList = attributesTag.getElementsByTagName("scheme"); 196 for (int i=0; i<schemeTagList.getLength(); i++) { 197 Element schemeTag = (Element)schemeTagList.item(i); 198 String schemeName = schemeTag.getAttribute("name"); 199 if (schemeName != null) { 202 String pattern = getNodeTextValue(schemeTag); 203 if (pattern==null || pattern.equals("")) { 204 attributes.put(schemeName, null); 205 } else { 206 attributes.put(schemeName, new Scheme(factory,getNodeTextValue(schemeTag))); 207 } 208 } 209 } 210 } 211 NodeList disallowedFieldsList = root.getElementsByTagName("disallowed-fields"); 213 if (disallowedFieldsList.getLength()>0) { 214 Element disallowedFieldsTag = (Element)disallowedFieldsList.item(0); 215 attributes.put(Attributes.DISALLOWED_FIELD_CASE_SENSITIVE, Boolean.valueOf(disallowedFieldsTag.getAttribute("case-sensitive"))); 216 attributes.put(Attributes.ENFORCE_DISALLOWED_FIELDS, Boolean.valueOf(disallowedFieldsTag.getAttribute("enforce"))); 217 } 218 return attributes; 219 } 220 221 227 public Map getDisallowedFields() { 228 Map disallowedFields = new HashMap(); 229 Element root = document.getDocumentElement(); 230 NodeList disallowedFieldsList = root.getElementsByTagName("disallowed-fields"); 231 if (disallowedFieldsList.getLength() > 0) { 232 Element disallowedFieldsTag = (Element)disallowedFieldsList.item(0); 233 boolean casesensitive = Boolean.valueOf(disallowedFieldsTag.getAttribute("case-sensitive")).booleanValue(); 234 NodeList fieldTagList = disallowedFieldsTag.getElementsByTagName("disallowed-field"); 235 for (int i = 0; i < fieldTagList.getLength(); i++) { 236 Element fieldTag = (Element)fieldTagList.item(i); 237 String fieldName = fieldTag.getAttribute("name"); 238 if (fieldName != null) { 241 if (!casesensitive) fieldName = fieldName.toLowerCase(); 242 String replacement = fieldTag.getAttribute("replacement"); 243 disallowedFields.put(fieldName,replacement); 244 } 245 } 246 } 247 return disallowedFields; 248 } 249 250 251 257 public List getTypeMappings() { 258 List typeMappings = new ArrayList(); 259 Element root = document.getDocumentElement(); 260 NodeList typeMappingsTagList = root.getElementsByTagName("type-mappings"); 261 if (typeMappingsTagList.getLength()>0) { 262 Element typeMappingsTag = (Element)typeMappingsTagList.item(0); 263 NodeList typeMappingTagList = typeMappingsTag.getElementsByTagName("type-mapping"); 264 for (int i=0; i<typeMappingTagList.getLength(); i++) { 265 Element typeMappingTag = (Element)typeMappingTagList.item(i); 266 TypeMapping typeMapping = new TypeMapping(); 267 typeMapping.name = typeMappingTag.getAttribute("name"); 268 if (typeMapping.name != null) { 271 try { 273 typeMapping.minSize = Integer.parseInt(typeMappingTag.getAttribute("min-size")); 274 } catch (NumberFormatException nfe) {} 275 try { 276 typeMapping.maxSize = Integer.parseInt(typeMappingTag.getAttribute("max-size")); 277 } catch (NumberFormatException nfe) {} 278 typeMapping.type = typeMappingTag.getAttribute("type"); 280 typeMappings.add(typeMapping); 281 if (typeMapping.name.equals("BYTE")) { 282 log.warn("In " + this + " deprecated mapping for 'BYTE' is specified. This must be changed to 'BINARY'"); 283 typeMapping.name = "BINARY"; 284 } 285 } 286 } 287 } 288 return typeMappings; 289 } 290 } 291 | Popular Tags |