KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.MessageFormat JavaDoc;
13
14 import org.mmbase.storage.*;
15
16 /**
17  * This is a specialised version of the MessageFormat class, with some awareness of
18  * MMBase objects. You can pass MMBase objects to Scheme when formatting a pattern.
19  * The Scheme automatically resolves the object to a value it can use in the pattern.
20  * Schemes are used by the storage to create configurable storage instructions (specifically database SQL code).
21  *
22  * @author Pierre van Rooden
23  * @version $Id: Scheme.java,v 1.5 2005/06/28 14:01:41 pierre Exp $
24  * @since MMBase-1.7
25  */

26 public final class Scheme extends MessageFormat JavaDoc {
27
28     /**
29      * The factory this scheme belongs to.
30      */

31     private StorageManagerFactory factory;
32
33     private String JavaDoc orgpattern;
34
35     /**
36      * Instantiate the Scheme
37      * @param factory The factory this scheme belongs to.
38      * @param pattern The pattern to use for the scheme
39      */

40     public Scheme (StorageManagerFactory factory, String JavaDoc pattern) {
41         super(pattern);
42         orgpattern = pattern;
43         this.factory = factory;
44     }
45
46     /**
47      * Resolves an object (passed as a parameter) to a value that can be applied in a pattern.
48      * It returns:
49      * <ul>
50      * <li>For MMBase: the object storage element identifier as a String (fully expanded table name)</li>
51      * <li>For MMObjectBuilder: the builder storage element identifier as a String (fully expanded table name)</li>
52      * <li>For MMObjectNode: the object number as an Integer</li>
53      * <li>For CoreField: a storage-compatible field name as a String (if no such name exists a StorageException is thrown)</li>
54      * </ul>
55      * Other object types are returned as is, leaving them to be handled by MessageFormat's formatting code.
56      *
57      * @todo MMBase, MMObjectNode, and MMObjectBuilder should be enriched with a Storable interface, which
58      * can be used instead
59      * @param param the object to resolve
60      * @return the resolved value
61      * @throws StorageException if the object cannot be resolved
62      */

63     protected Object JavaDoc resolveParameter(Object JavaDoc param) throws StorageException {
64         if (param == null || param instanceof String JavaDoc || param instanceof Number JavaDoc) {
65             return param;
66         } else if (param instanceof Storable) {
67             return ((Storable)param).getStorageIdentifier();
68         } else {
69             return factory.getStorageIdentifier(param);
70         }
71     }
72
73     /**
74      * Applies the parameters to the scheme's pattern.
75      * @param params an array of parameters to apply to the pattern
76      * @return the result scheme as a String
77      * @throws StorageException if one of the passed parameters cannot be resolved
78      */

79     public String JavaDoc format(Object JavaDoc[] params) throws StorageException {
80         for (int i = 0; i < params.length; i++) {
81             params[i] = resolveParameter(params[i]);
82         }
83         return super.format(params);
84     }
85
86     public String JavaDoc toString() {
87         return orgpattern;
88     }
89
90 }
91
92
Popular Tags