KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > storage > util > StorageReader


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.storage.util;
11
12 import java.util.*;
13
14 import org.w3c.dom.*;
15 import org.xml.sax.InputSource JavaDoc;
16
17 import org.mmbase.storage.*;
18 import org.mmbase.util.xml.DocumentReader;
19 import org.mmbase.util.logging.*;
20
21 /**
22  * @javadoc
23  * @author Pierre van Rooden
24  * @version $Id: StorageReader.java,v 1.10 2005/07/04 21:25:26 michiel Exp $
25  * @since MMBase-1.7
26  */

27 public class StorageReader extends DocumentReader {
28
29     private static final Logger log = Logging.getLoggerInstance(StorageReader.class);
30
31     /** Public ID of the Storage DTD version 1.0 */
32     public static final String JavaDoc PUBLIC_ID_STORAGE_1_0 = "-//MMBase//DTD storage config 1.0//EN";
33     /** DTD resource filename of the Database DTD version 1.0 */
34     public static final String JavaDoc DTD_STORAGE_1_0 = "storage_1_0.dtd";
35
36     /** Public ID of the most recent Database DTD */
37     public static final String JavaDoc PUBLIC_ID_STORAGE = PUBLIC_ID_STORAGE_1_0;
38     /** DTD resource filename of the most Database DTD */
39     public static final String JavaDoc 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     /**
46      * The factory for which the reader reads the document.
47      * The factory is used to verify whether the document is compatible, and is used to instantiate objects
48      * that depend on factory information (such as schemes)
49      */

50     protected StorageManagerFactory factory;
51
52     /**
53      * Constructor.
54      *
55      * @param factory the factory for which to read the storage configuration
56      * @param source to the xml document.
57      * @since MMBase-1.7
58      */

59     public StorageReader(StorageManagerFactory factory, InputSource JavaDoc source) {
60         super(source, DocumentReader.validate(), StorageReader.class);
61         this.factory = factory;
62     }
63
64     /**
65      * Attempt to load a StorageManager class, using the classname as given in the configuration.
66      * The method verifies whether the instantiated class is of the correct version.
67      * @return the storage manager Class, or null if none was configured
68      * @throws StorageConfigurationException if the factory version did not match, or the class configured is invalid
69      */

70     public Class JavaDoc getStorageManagerClass() throws StorageConfigurationException {
71         Element root = document.getDocumentElement();
72         if (factory != null) {
73             // verify if the storagemanagerfactory is of the correct class, and
74
// of the correct version
75
NodeList factoryTagList = root.getElementsByTagName("storagemanagerfactory");
76             if (factoryTagList.getLength()>0) {
77                 Element factoryTag = (Element)factoryTagList.item(0);
78                 try {
79                     // obtain and check class
80
String JavaDoc factoryClassName = factoryTag.getAttribute("classname");
81                     Class JavaDoc factoryClass = Class.forName(factoryClassName);
82                     if (!factoryClass.isInstance(factory)) {
83                         throw new StorageConfigurationException("StorageManager Configuration requires factory class '"+factoryClassName+"'.");
84                     }
85                     // obtain and check version
86
String JavaDoc 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 JavaDoc pe) {
94                     throw new StorageConfigurationException(pe); // version is not an integer
95
} catch (ClassNotFoundException JavaDoc cnfe) {
96                     throw new StorageConfigurationException(cnfe); // factory class not found
97
}
98             }
99         }
100         NodeList managerTagList = root.getElementsByTagName("storagemanager");
101         if (managerTagList.getLength()>0) {
102             Element managerTag = (Element)managerTagList.item(0);
103             String JavaDoc managerClassName = managerTag.getAttribute("classname");
104             // intantiate storage manager and check version
105
try {
106                 Class JavaDoc managerClass = Class.forName(managerClassName);
107                 StorageManager manager = (StorageManager)managerClass.newInstance();
108                 // obtain and check version
109
String JavaDoc 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 JavaDoc pe) {
118                 throw new StorageConfigurationException(pe); // version is not an integer
119
} catch (ClassNotFoundException JavaDoc cnfe) {
120                 throw new StorageConfigurationException(cnfe);
121             } catch (IllegalAccessException JavaDoc iae) {
122                 throw new StorageConfigurationException(iae);
123             } catch (InstantiationException JavaDoc ie) {
124                 throw new StorageConfigurationException(ie);
125             }
126         } else {
127             return null;
128         }
129     }
130
131     /**
132      * Attempt to obtain a list of SearchQueryHandler classes, using the classname as given in the configuration.
133      *
134      *
135      * @return A List of Class objects, each being the SearchQueryHandler class, or an empty list if none was configured
136      * @throws StorageConfigurationException if the class configured is invalid
137      */

138     public List getSearchQueryHandlerClasses() throws StorageConfigurationException {
139         // override if otherwise specified
140
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 JavaDoc queryHandlerClassName = handlerTag.getAttribute("classname");
146             // get class
147
try {
148                 classes.add(Class.forName(queryHandlerClassName));
149             } catch (ClassNotFoundException JavaDoc cnfe) {
150                 throw new StorageConfigurationException(cnfe);
151             }
152         }
153         return classes;
154     }
155
156     /**
157      * Reads all attributes from the reader and returns them as a map.
158      * This include options, as well as the following special attributes:
159      * <ul>
160      * <li>option-disallowed-fields-case-sensitive : has the Boolean value TRUE if disallowed fields are case sensitive (default FALSE)</li>
161      * </ul>
162      * @return attributes as a map
163      */

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 JavaDoc attributeName = attributeTag.getAttribute("name");
174                 // require an attribute name.
175
// if not given, skip the option.
176
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                 // require an option name.
184
// if not given, skip the option.
185
String JavaDoc optionName = optionTag.getAttribute("name");
186                 if (optionName != null) {
187                     String JavaDoc optionValue = optionTag.getAttribute("value");
188                     Boolean JavaDoc 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 JavaDoc schemeName = schemeTag.getAttribute("name");
199                 // require a scheme name
200
// if not given, skip the option
201
if (schemeName != null) {
202                     String JavaDoc 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         // system attributes
212
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     /**
222      * Returns all disallowed fields and their possible alternate values.
223      * The fields are returned as name-value pairs, where the disallowedfieldname is the key, and
224      * the alternate name is the value (null if no name is given).
225      * @return disallowed fields as a map
226      */

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 JavaDoc fieldName = fieldTag.getAttribute("name");
238                 // require a field name.
239
// if not given, skip the option.
240
if (fieldName != null) {
241                     if (!casesensitive) fieldName = fieldName.toLowerCase();
242                     String JavaDoc replacement = fieldTag.getAttribute("replacement");
243                     disallowedFields.put(fieldName,replacement);
244                 }
245             }
246         }
247         return disallowedFields;
248     }
249
250
251     /**
252      * Returns all type mappings.
253      * The mappings are returned in the order that they were given in the reader.
254      * Calling code should sort this list if they want to use TypoMapping fuzzy matching.
255      * @return a List of TypeMapping objects
256      */

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                 // require a type-mapping name (a MMBase type)
269
// if not given, skip the option.
270
if (typeMapping.name != null) {
271                     // obtain min/max values for sizes
272
try {
273                         typeMapping.minSize = Integer.parseInt(typeMappingTag.getAttribute("min-size"));
274                     } catch (NumberFormatException JavaDoc nfe) {}
275                     try {
276                         typeMapping.maxSize = Integer.parseInt(typeMappingTag.getAttribute("max-size"));
277                     } catch (NumberFormatException JavaDoc nfe) {}
278                     // get the type to convert to
279
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