001: /* $Id: FormatFactory.java 857 2007-02-09 07:57:53Z hengels $ */
002: package org.conform.format;
003:
004: import org.conform.*;
005:
006: import java.util.*;
007: import java.math.*;
008:
009: /**
010: * @version $Revision: 857 $
011: */
012: public abstract class FormatFactory {
013: public static final Format NO_FORMAT = new NoFormat();
014: private static FormatFactory INSTANCE;
015: private ResourceProvider resourceProvider;
016:
017: protected FormatFactory() {
018: INSTANCE = this ;
019: }
020:
021: public Format getFormat(Class type) {
022: if (java.sql.Date.class.isAssignableFrom(type))
023: return new DateFormat();
024: if (java.sql.Time.class.isAssignableFrom(type))
025: return new TimeFormat();
026: if (java.sql.Timestamp.class.isAssignableFrom(type))
027: return new DateTimeFormat();
028: if (java.util.Date.class.isAssignableFrom(type))
029: return new DateTimeFormat();
030: if (Float.class.isAssignableFrom(type))
031: return new NumberFormat(Float.class);
032: if (Double.class.isAssignableFrom(type))
033: return new NumberFormat(Double.class);
034: if (Long.class.isAssignableFrom(type))
035: return new NumberFormat(Long.class);
036: if (Integer.class.isAssignableFrom(type))
037: return new NumberFormat(Integer.class);
038: if (Short.class.isAssignableFrom(type))
039: return new NumberFormat(Short.class);
040: if (Byte.class.isAssignableFrom(type))
041: return new NumberFormat(Byte.class);
042: if (BigDecimal.class.isAssignableFrom(type))
043: return new NumberFormat(BigDecimal.class);
044: if (BigInteger.class.isAssignableFrom(type))
045: return new NumberFormat(BigInteger.class);
046: if (float.class.isAssignableFrom(type))
047: return new NumberFormat(Float.class);
048: if (double.class.isAssignableFrom(type))
049: return new NumberFormat(Double.class);
050: if (long.class.isAssignableFrom(type))
051: return new NumberFormat(Long.class);
052: if (int.class.isAssignableFrom(type))
053: return new NumberFormat(Integer.class);
054: if (short.class.isAssignableFrom(type))
055: return new NumberFormat(Short.class);
056: if (byte.class.isAssignableFrom(type))
057: return new NumberFormat(Byte.class);
058: if (Class.class.isAssignableFrom(type))
059: return new TemplateFormat("$name");
060:
061: return null;
062: }
063:
064: public Format getFormat(PropertyMeta propertyMeta) {
065: Format format = propertyMeta.getFormat();
066: if (format == null) {
067: if (propertyMeta.getRelationBean() != null)
068: format = propertyMeta.getRelationBean().getFormat();
069: else
070: format = getFormat(propertyMeta.getType());
071: }
072: if (format != null) {
073: Object pattern = propertyMeta
074: .getAttribute(PropertyMeta.ATTRIBUTE_PATTERN);
075: if (pattern instanceof String)
076: format.setPattern((String) pattern);
077:
078: Integer scale = (Integer) propertyMeta
079: .getAttribute(PropertyMeta.ATTRIBUTE_SCALE);
080: if (scale != null) {
081: NumberFormat numberFormat = (NumberFormat) format;
082: numberFormat.setScale(scale.intValue());
083: }
084: Integer precision = (Integer) propertyMeta
085: .getAttribute(PropertyMeta.ATTRIBUTE_PRECISION);
086: if (precision != null) {
087: NumberFormat numberFormat = (NumberFormat) format;
088: numberFormat.setPrecision(precision.intValue());
089: }
090: FormatType formatType = (FormatType) propertyMeta
091: .getAttribute("formatType");
092: if (formatType == FormatType.PERCENTAGE) {
093: NumberFormat numberFormat = (NumberFormat) format;
094: numberFormat.setPercentage(true);
095: }
096: }
097: return format;
098: }
099:
100: public abstract void setLocale(Locale locale);
101:
102: public abstract Locale getLocale();
103:
104: public ResourceProvider getResourceProvider() {
105: return resourceProvider;
106: }
107:
108: public void setResourceProvider(ResourceProvider resourceProvider) {
109: this .resourceProvider = resourceProvider;
110: }
111:
112: public static final FormatFactory getInstance() {
113: return INSTANCE;
114: }
115: }
|