001: // /////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: FieldFormatter.java 1688 2007-09-25 12:54:46Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.commons.formatters;
025:
026: import java.util.Dictionary;
027: import java.util.Enumeration;
028: import java.util.Hashtable;
029:
030: /**
031: * Formats a specific field. Subclasses of this class are handling the different kind of fields.
032: * @author Cristian Bogdan
033: * @author Stefan Baebler
034: * @author Frederik Habilis
035: * @author Rudolf Mayer
036: */
037: public class FieldFormatter {
038: static String[] params = { "default", "empty" };
039:
040: static String[][] paramValues = { null, null };
041:
042: public String[] getAcceptedParams() {
043: return params;
044: }
045:
046: public String[][] getAcceptedValue() {
047: return paramValues;
048: }
049:
050: private static final class SingletonHolder {
051: static final FieldFormatter singleton = new FieldFormatter();
052: }
053:
054: public static FieldFormatter getInstance() {
055: return SingletonHolder.singleton;
056: }
057:
058: /** Don't use this, use getInstance() */
059: protected FieldFormatter() {
060: for (int i = 0; i < getAcceptedParams().length; i++) {
061: Hashtable h = new Hashtable(13);
062: if (getAcceptedValue()[i] != null)
063: for (int j = 0; j < getAcceptedValue()[i].length; j++)
064: h.put(getAcceptedValue()[i][j], dummy);
065: validParams.put(getAcceptedParams()[i], h);
066: }
067: }
068:
069: static Object dummy = new Object();
070:
071: public String getExpr(RecordFormatter rf, int fieldIndex) {
072: if (rf.expr[fieldIndex] != null)
073: return rf.expr[fieldIndex];
074: return rf.dd.getFieldDefinition(fieldIndex).getName();
075: }
076:
077: public void initExpr(RecordFormatter rf, int fieldIndex, String s) {
078: rf.expr[fieldIndex] = s;
079: }
080:
081: Hashtable validParams = new Hashtable(13);
082:
083: public void checkParams(RecordFormatter rf, int fieldIndex,
084: Dictionary formatParams) {
085: for (Enumeration e = formatParams.keys(); e.hasMoreElements();) {
086: String s = (String) e.nextElement();
087: if (s.startsWith("org.makumba"))
088: continue;
089: checkParam(rf, fieldIndex, s,
090: ((String) formatParams.get(s)).toLowerCase());
091: }
092: }
093:
094: public void checkParam(RecordFormatter rf, int fieldIndex,
095: String name, String val) {
096: Hashtable h = (Hashtable) validParams.get(name);
097: if (h == null)
098: throw new InvalidValueException(rf.expr[fieldIndex],
099: "invalid format parameter \'" + name + "\'");
100: if (h.size() == 0)
101: return;
102: if (h.get(val) == null)
103: throw new InvalidValueException(rf.expr[fieldIndex],
104: "invalid value for format parameter \'" + name
105: + "\': <" + val + ">");
106: }
107:
108: /**
109: * Format the object to pure text. If text-format is blank, try the "empty" replacer value.
110: *
111: * @param rf
112: * TODO
113: * @param fieldIndex
114: * TODO
115: */
116: public String format(RecordFormatter rf, int fieldIndex, Object o,
117: Dictionary formatParams) {
118: String formatted;
119: if (o == null
120: || o.equals(rf.dd.getFieldDefinition(fieldIndex)
121: .getNull())) {
122: formatted = formatNull(rf, fieldIndex, formatParams);
123: } else {
124: formatted = formatNotNull(rf, fieldIndex, o, formatParams);
125: }
126: if ("".equals(formatted)) {
127: return getEmptyValueFormat(rf, fieldIndex, formatParams);
128: }
129: return formatted;
130: }
131:
132: /**
133: * Format the null-object to pure text. Try the "default" format parameter.
134: *
135: * @param rf
136: * TODO
137: * @param fieldIndex
138: * TODO
139: */
140: public String formatNull(RecordFormatter rf, int fieldIndex,
141: Dictionary formatParams) {
142: return getDefaultValueFormat(rf, fieldIndex, formatParams);
143: }
144:
145: /**
146: * Formats the not-null-object to pure text. To be over-ridden by subclasses.
147: */
148: public String formatNotNull(RecordFormatter rf, int fieldIndex,
149: Object o, Dictionary formatParams) {
150: return o.toString();
151: }
152:
153: public int getIntParam(RecordFormatter rf, int fieldIndex,
154: Dictionary formatParams, String name) {
155: String s = (String) formatParams.get(name);
156: if (s == null)
157: return -1;
158: try {
159: return Integer.parseInt(s);
160: } catch (NumberFormatException e) {
161: throw new InvalidValueException(rf.expr[fieldIndex],
162: "invalid integer for " + name + ": " + s);
163: }
164: }
165:
166: public String getIntParamString(RecordFormatter rf, int fieldIndex,
167: Dictionary formatParams, String name) {
168: int n = getIntParam(rf, fieldIndex, formatParams, name);
169: if (n == -1)
170: return "";
171: return name + "=\"" + n + "\" ";
172: }
173:
174: // FIXME? these 2 might get more complicated, if {default, empty} are OQL
175: // expressions.
176:
177: /**
178: * Gets the formatted default value, used if real value is null. Returns blank if not set.
179: */
180: public String getDefaultValueFormat(RecordFormatter rf,
181: int fieldIndex, Dictionary formatParams) {
182: String s = (String) formatParams.get("default");
183: return (s == null) ? "" : s;
184: }
185:
186: /**
187: * Gets the formatted empty value, used if real value is empty. Returns blank if not set.
188: */
189: public String getEmptyValueFormat(RecordFormatter rf,
190: int fieldIndex, Dictionary formatParams) {
191: String s = (String) formatParams.get("empty");
192: return (s == null) ? "" : s;
193: }
194:
195: /**
196: * Chooses between the real (formatted) value and possible replacements (default, empty).
197: */
198: public String resetValueFormat(RecordFormatter rf, int fieldIndex,
199: String s, Dictionary formatParams) {
200: if (s == null) {
201: s = getDefaultValueFormat(rf, fieldIndex, formatParams);
202: }
203: if ("".equals(s)) {
204: return getEmptyValueFormat(rf, fieldIndex, formatParams);
205: }
206: return s;
207: }
208:
209: public boolean equals(String s1, String s2) {
210: return s1 != null && s1.equals(s2);
211: }
212:
213: }
|