KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > metadata > CollectionInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * CollectionConfig.java
25  *
26  * Created on 20 juillet 2000, 14:24
27  */

28  
29 package org.xquark.mapper.metadata;
30
31 import java.io.StringWriter JavaDoc;
32
33 import org.xquark.mapper.RepositoryConnection;
34 import org.xquark.mapper.util.Tabulator;
35 import org.xquark.xml.xdbc.*;
36
37 /** Data structure that is used to set features when creating an XMLCollection.
38  * <p>This object can be recycled because every XMLCollection keeps a copy
39  * of its configuration.</p>
40  */

41
42 public class CollectionInfo implements Cloneable JavaDoc, Configurable
43 {
44     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
45     private static final String JavaDoc RCSName = "$Name: $";
46     
47     /////////////////////////
48
// PROPERTIES
49
/////////////////////////
50
public static final String JavaDoc TYPE_PROPERTY
51         = "http://www.xquark.org/repository/collection/properties/type";
52     public static final String JavaDoc BUCKET_ID_SIZE_PROPERTY
53         = "http://www.xquark.org/repository/collection/properties/bucketIDSizeInBits";
54     
55     /* "public" properties */
56     private static final String JavaDoc[] SUPPORTED_PROPERTIES
57         = new String JavaDoc[] {
58             RepositoryConnection.TEXT_LENGTH_PROPERTY,
59             RepositoryConnection.EXTRA_DATA_LENGTH_PROPERTY,
60             RepositoryConnection.DESC_PROPERTY,
61             RepositoryConnection.DOC_OID_SIZE_PROPERTY,
62             RepositoryConnection.DOC_OID_PREALLOCATION_SIZE,
63             RepositoryConnection.MAPPING_ID_PROPERTY};
64         
65     // TO DO : define which properties are read-only (all for the moment)
66

67     /////////////////////////
68
// FEATURES
69
/////////////////////////
70

71     /* "public" features */
72     private static final String JavaDoc[] SUPPORTED_FEATURES
73         = new String JavaDoc[] {RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE};
74     
75     /** System repository using Generic storage. These repositories are
76      * processed in special way by the repository. For instance, they
77      * are deleted at last. They are used to store, for instance,
78      * schemas or mappings.
79      *<p><B>This constants has been set here to be hidden from API users.</B></p>
80      */

81     public static final byte SYSTEM = 0x00;
82    
83     /** The default collection type
84      */

85     public static final byte USER = 0x01;
86     
87     /////////////////////////
88
// DATA
89
/////////////////////////
90

91     /** Storage method type. By extension, type of the collection itself. At the present
92      * time, only one kind of collection is available : USER.
93      */

94     protected byte collectionType = USER;
95
96     /** A user-free string.
97      */

98     protected String JavaDoc description;
99     
100     protected byte docOIDSize = RepositoryConstants.DID_SIZE_DEFAULT_VALUE;
101     protected short docOIDPrealloc = 1;
102 // protected byte bucketIDSize = 24;
103

104     /** Maximum length of text values. Default is 255 bytes */
105     protected int maxTextLength = RepositoryConstants.DEFAULT_DATA_LENGTH;
106
107     protected int maxExtraDataLength = RepositoryConstants.DEFAULT_DATA_LENGTH;
108
109     /** The public ID for mapping file used for identification in the system collection.
110      * A null value means that the default mapping method must be used.
111      */

112     protected String JavaDoc mappingURI = "";
113     
114     /** Option used to discard prefix mapping information storage for documents.
115      * The prefix mappings of the schemas associated are used at reconstruction
116      * time. If false, document info is discarded and schema one is used.
117      */

118     protected boolean useSchemaPrefixes = false;
119     
120     private boolean writeProtected = true;
121     
122     /** Creates new CollectionConfig with default features.
123      * <p>Default is 512 for text length.</p>
124      */

125     public CollectionInfo() {}
126     
127     public CollectionInfo(byte type, int textLength)
128     {
129         collectionType = type;
130         maxTextLength = maxExtraDataLength = textLength;
131         
132     }
133
134     public CollectionInfo(boolean writeProtected)
135     {
136         setWriteProtected(writeProtected);
137     }
138
139     /** "Clone" constructor.
140      * @param config base CollectionConfig object.
141      */

142     public CollectionInfo(Configurable config)
143     {
144         try {
145             collectionType = Byte.parseByte((String JavaDoc)config.getProperty(TYPE_PROPERTY));
146             maxTextLength = Integer.parseInt((String JavaDoc)config.getProperty(RepositoryConnection.TEXT_LENGTH_PROPERTY));
147             maxExtraDataLength = Integer.parseInt((String JavaDoc)config.getProperty(RepositoryConnection.EXTRA_DATA_LENGTH_PROPERTY));
148             mappingURI = (String JavaDoc)config.getProperty(RepositoryConnection.MAPPING_ID_PROPERTY);
149             description = (String JavaDoc)config.getProperty(RepositoryConnection.DESC_PROPERTY);
150             setDocOIDSize(Byte.parseByte((String JavaDoc)config.getProperty(RepositoryConnection.DOC_OID_SIZE_PROPERTY)));
151             docOIDPrealloc = Short.parseShort((String JavaDoc)config.getProperty(RepositoryConnection.DOC_OID_PREALLOCATION_SIZE));
152 // bucketIDSize = Byte.parseByte((String)config.getProperty(BUCKET_ID_SIZE_PROPERTY));
153
useSchemaPrefixes = config.getFeature(RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE);
154         }
155         catch (XMLDBCException e) {
156             // no op : we know what is supported !!!
157
}
158     }
159     
160     public void setWriteProtected(boolean mode)
161     {
162         writeProtected = mode;
163     }
164     
165     /** Builds a String in order to displays the information of this configuration.
166      * @return the string representing the repository features.
167      */

168     public String JavaDoc toString()
169     {
170         StringWriter JavaDoc out = new StringWriter JavaDoc();
171         Tabulator tabulator = new Tabulator
172                                     (
173                                     out,
174                                     79,
175                                     new int[] {0, 40}
176                                     );
177                                     
178         if (description != null && description.length() > 0)
179         {
180             tabulator.addItem("Description:");
181             tabulator.addItem(description);
182         }
183         if (mappingURI.length() != 0)
184         {
185             tabulator.addItem("Mapping file:");
186             tabulator.addItem(mappingURI);
187             tabulator.addItem("Use Schema prefixes :");
188             tabulator.addItem(String.valueOf(useSchemaPrefixes));
189         }
190         tabulator.addItem("Text length:");
191         tabulator.addItem(String.valueOf(maxTextLength));
192         tabulator.addItem("Extra data length:");
193         tabulator.addItem(String.valueOf(maxExtraDataLength));
194         tabulator.addItem("Document ID size in bits:");
195         tabulator.addItem(String.valueOf(docOIDSize));
196         tabulator.addItem("Document OID preallocation size:");
197         tabulator.addItem(String.valueOf(docOIDSize));
198 // tabulator.addItem("\nBucket ID size in bits:\t");
199
// tabulator.addItem(bucketIDSize);
200

201         return out.toString();
202     }
203     
204     /** Builds a copy of this object.
205      * @return a CollectionConfig object.
206      */

207     public Object JavaDoc clone()
208     {
209         Object JavaDoc copy = null;
210         try {
211             copy = super.clone();
212         }
213         catch(CloneNotSupportedException JavaDoc e) {
214         }
215         return copy;
216     }
217     
218     public void setProperty(String JavaDoc propertyId, Object JavaDoc value) throws XMLDBCNotRecognizedException, XMLDBCNotSupportedException
219     {
220         // TO REFINE FOR EVERY PARAMETER
221
if (writeProtected)
222             throw new XMLDBCNotSupportedException("This object is read-only.");
223         
224         if (propertyId.equals(TYPE_PROPERTY))
225         {
226             if (value instanceof String JavaDoc)
227                 collectionType = Byte.parseByte((String JavaDoc)value);
228             else if (value instanceof Number JavaDoc)
229                 collectionType = ((Number JavaDoc)value).byteValue();
230             else
231                 throw new XMLDBCNotSupportedException("Property value type is not supported.");
232         }
233         else if (propertyId.equals(RepositoryConnection.TEXT_LENGTH_PROPERTY))
234         {
235             if (value instanceof String JavaDoc)
236                 maxTextLength = Integer.parseInt((String JavaDoc)value);
237             else if (value instanceof Number JavaDoc)
238                 maxTextLength = ((Number JavaDoc)value).intValue();
239             else
240                 throw new XMLDBCNotSupportedException("Property value type is not supported.");
241         }
242         else if (propertyId.equals(RepositoryConnection.EXTRA_DATA_LENGTH_PROPERTY))
243         {
244             if (value instanceof String JavaDoc)
245                 maxExtraDataLength = Integer.parseInt((String JavaDoc)value);
246             else if (value instanceof Number JavaDoc)
247                 maxExtraDataLength = ((Number JavaDoc)value).intValue();
248             else
249                 throw new XMLDBCNotSupportedException("Property value type is not supported.");
250         }
251         else if (propertyId.equals(RepositoryConnection.DOC_OID_SIZE_PROPERTY))
252         {
253             if (value instanceof String JavaDoc)
254                 setDocOIDSize(Byte.parseByte((String JavaDoc)value));
255             else if (value instanceof Number JavaDoc)
256                 setDocOIDSize(((Number JavaDoc)value).byteValue());
257             else
258                 throw new XMLDBCNotSupportedException("Property value type is not supported.");
259         }
260         else if (propertyId.equals(RepositoryConnection.DOC_OID_PREALLOCATION_SIZE))
261         {
262             if (value instanceof String JavaDoc)
263                 docOIDPrealloc = Short.parseShort((String JavaDoc)value);
264             else if (value instanceof Number JavaDoc)
265                 docOIDPrealloc = ((Number JavaDoc)value).shortValue();
266             else
267                 throw new XMLDBCNotSupportedException("Property value type is not supported.");
268         }
269 // else if (propertyId.equals(BUCKET_ID_SIZE_PROPERTY))
270
// {
271
// if (value instanceof String)
272
// bucketIDSize = Byte.parseByte((String)value);
273
// else if (value instanceof Number)
274
// bucketIDSize = ((Number)value).byteValue();
275
// else
276
// throw new XMLDBCNotSupportedException("Property value type is not supported.");
277
// }
278
else if (propertyId.equals(RepositoryConnection.MAPPING_ID_PROPERTY))
279         {
280             if (value instanceof String JavaDoc)
281                 mappingURI = (String JavaDoc)value;
282             else
283                 throw new XMLDBCNotSupportedException("Property value type is not supported.");
284         }
285          else if (propertyId.equals(RepositoryConnection.DESC_PROPERTY))
286         {
287             if (value instanceof String JavaDoc)
288                 description = (String JavaDoc)value;
289             else
290                 throw new XMLDBCNotSupportedException("Property value type is not supported.");
291         }
292         else
293             throw new XMLDBCNotRecognizedException("Unkown property " + propertyId);
294     }
295     
296     public Object JavaDoc getProperty(String JavaDoc propertyId) throws XMLDBCNotRecognizedException
297     {
298         if (propertyId.equals(TYPE_PROPERTY))
299             return Byte.toString(collectionType);
300         else if (propertyId.equals(RepositoryConnection.TEXT_LENGTH_PROPERTY))
301             return Integer.toString(maxTextLength);
302         else if (propertyId.equals(RepositoryConnection.EXTRA_DATA_LENGTH_PROPERTY))
303             return Integer.toString(maxExtraDataLength);
304         else if (propertyId.equals(RepositoryConnection.DOC_OID_SIZE_PROPERTY))
305             return Byte.toString(docOIDSize);
306         else if (propertyId.equals(RepositoryConnection.DOC_OID_PREALLOCATION_SIZE))
307             return Integer.toString(docOIDPrealloc);
308 // else if (propertyId.equals(BUCKET_ID_SIZE_PROPERTY))
309
// return Byte.toString(bucketIDSize);
310
else if (propertyId.equals(RepositoryConnection.MAPPING_ID_PROPERTY))
311             return mappingURI;
312         else if (propertyId.equals(RepositoryConnection.DESC_PROPERTY))
313             return description;
314         else
315             throw new XMLDBCNotRecognizedException("Unkown property " + propertyId);
316     }
317     
318     public boolean getFeature(String JavaDoc name) throws XMLDBCNotRecognizedException
319     {
320         if (name.equals(RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE))
321             return useSchemaPrefixes;
322         else
323             throw new XMLDBCNotRecognizedException("Unkown feature " + name);
324     }
325     
326     public void setFeature(String JavaDoc name, boolean state) throws XMLDBCNotRecognizedException, XMLDBCNotSupportedException
327     {
328         // TO REFINE FOR EVERY PARAMETER
329
if (writeProtected)
330             throw new XMLDBCNotSupportedException("This object is read-only.");
331         
332         if (name.equals(RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE))
333             useSchemaPrefixes = state;
334         else
335             throw new XMLDBCNotRecognizedException("Unkown feature " + name);
336     }
337     
338     public String JavaDoc[] getFeatureList()
339     {
340         return SUPPORTED_FEATURES;
341     }
342     
343     public String JavaDoc[] getPropertyList()
344     {
345         return SUPPORTED_PROPERTIES;
346     }
347     
348     public String JavaDoc getMappingID()
349     {
350         return mappingURI;
351     }
352     
353     public String JavaDoc getDescription()
354     {
355         return description;
356     }
357     
358     public int getMaxTextLength()
359     {
360         return maxTextLength;
361     }
362     
363     public int getMaxExtraDataLength()
364     {
365         return maxExtraDataLength;
366     }
367     
368     public byte getType()
369     {
370         return collectionType;
371     }
372     
373     public byte getDocOIDSize()
374     {
375         return docOIDSize;
376     }
377     
378     public short getDocOIDPreallocationSize()
379     {
380         return docOIDPrealloc;
381     }
382     
383 // public byte getBucketIDSize()
384
// {
385
// return bucketIDSize;
386
// }
387

388     public void setMappingID(String JavaDoc uri)
389     {
390         mappingURI = uri;
391     }
392     
393     public void setDescription(String JavaDoc description)
394     {
395         this.description = description;
396     }
397     
398     public void setMaxTextLength(int maxLength)
399     {
400         maxTextLength = maxLength;
401     }
402     
403     public void setMaxExtraDataLength(int maxLength)
404     {
405         maxExtraDataLength = maxLength;
406     }
407     
408     public void setType(byte type)
409     {
410         collectionType = type;
411     }
412     
413     public void setDocOIDSize(byte docSize)
414     {
415         if (docSize < RepositoryConstants.DID_SIZE_MIN_VALUE)
416             docOIDSize = RepositoryConstants.DID_SIZE_MIN_VALUE;
417         else if (docSize > RepositoryConstants.DID_SIZE_MAX_VALUE)
418             docOIDSize = RepositoryConstants.DID_SIZE_MAX_VALUE;
419         else
420             docOIDSize = docSize;
421     }
422     
423     public void setDocOIDPreallocationSize(short size)
424     {
425         docOIDPrealloc = size;
426     }
427     
428 // public void setBucketIDSize(byte docSize)
429
// {
430
// bucketIDSize = docSize;
431
// }
432
}
433
Popular Tags