01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.storage.util;
11:
12: import java.text.MessageFormat;
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.6 2006/09/25 12:07:17 michiel Exp $
24: * @since MMBase-1.7
25: */
26: public final class Scheme extends MessageFormat {
27:
28: /**
29: * The factory this scheme belongs to.
30: */
31: private final StorageManagerFactory factory;
32:
33: private final String 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 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 resolveParameter(Object param)
64: throws StorageException {
65: if (param == null || param instanceof String
66: || param instanceof Number) {
67: return param;
68: } else if (param instanceof Storable) {
69: return ((Storable) param).getStorageIdentifier();
70: } else {
71: return factory.getStorageIdentifier(param);
72: }
73: }
74:
75: /**
76: * Applies the parameters to the scheme's pattern.
77: * @param params an array of parameters to apply to the pattern
78: * @return the result scheme as a String
79: * @throws StorageException if one of the passed parameters cannot be resolved
80: */
81: public String format(Object... params) throws StorageException {
82: for (int i = 0; i < params.length; i++) {
83: params[i] = resolveParameter(params[i]);
84: }
85: return super .format(params); // I think it is a bit obfuscating that there is no method super.format(Object[]).
86: }
87:
88: public String toString() {
89: return orgpattern;
90: }
91:
92: }
|