1. /*
  2. * @(#)Format.java 1.25 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * @(#)Format.java 1.25 01/11/29
  9. *
  10. * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  11. * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  12. *
  13. * Portions copyright (c) 1996-1998 Sun Microsystems, Inc.
  14. * All Rights Reserved.
  15. *
  16. * The original version of this source code and documentation is copyrighted
  17. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  18. * materials are provided under terms of a License Agreement between Taligent
  19. * and Sun. This technology is protected by multiple US and International
  20. * patents. This notice and attribution to Taligent may not be removed.
  21. * Taligent is a registered trademark of Taligent, Inc.
  22. *
  23. * Permission to use, copy, modify, and distribute this software
  24. * and its documentation for NON-COMMERCIAL purposes and without
  25. * fee is hereby granted provided that this copyright notice
  26. * appears in all copies. Please refer to the file "copyright.html"
  27. * for further important copyright and licensing information.
  28. *
  29. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  30. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  31. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  32. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  33. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  34. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  35. *
  36. */
  37. package java.text;
  38. import java.io.Serializable;
  39. /**
  40. * <code>Format</code> is an abstract base class for formatting locale-sensitive
  41. * information such as dates, messages, and numbers.
  42. *
  43. * <p>
  44. * <code>Format</code> defines the programming interface for formatting
  45. * locale-sensitive objects into <code>String</code>s (the
  46. * <code>format</code> method) and for parsing <code>String</code>s back
  47. * into objects (the <code>parseObject</code> method). Any <code>String</code>
  48. * formatted by <code>format</code> is guaranteed to be parseable by
  49. * <code>parseObject</code>.
  50. *
  51. * <p>
  52. * If formatting is unsuccessful because the <code>Format</code> object
  53. * cannot format the type of object specified, <code>format</code> throws an
  54. * <code>IllegalArgumentException</code>. Otherwise, if there is something
  55. * illformed about the object, <code>format</code> returns the Unicode
  56. * replacement character <code>\\uFFFD</code>.
  57. *
  58. * <p>
  59. * If there is no match when parsing,
  60. * <code>parseObject(String)</code> throws a <code>ParseException</code>,
  61. * and <code>parseObject(String, ParsePosition)</code> leaves the
  62. * <code>ParsePosition</code> <code>index</code> member unchanged and
  63. * returns <code>null</code>.
  64. *
  65. * <p>
  66. * <STRONG>Subclassing:</STRONG>
  67. * The JDK provides three concrete subclasses of <code>Format</code>--
  68. * <code>DateFormat</code>, <code>MessageFormat</code>, and
  69. * <code>NumberFormat</code>--for formatting dates, messages, and numbers,
  70. * respectively.
  71. * <p>
  72. * Concrete subclasses <em>must</em> implement these two methods:
  73. * <ol>
  74. * <li> <code>format(Object obj, StringBuffer toAppendTo, FieldPosition pos)</code>
  75. * <li> <code>parseObject (String source, ParsePosition pos)</code>
  76. * </ol>
  77. *
  78. * <p>
  79. * Most subclasses will also implement the following two methods:
  80. * <ol>
  81. * <li>
  82. * <code>getInstance</code> for getting a useful format object appropriate
  83. * for the current locale
  84. * <li>
  85. * <code>getInstance(Locale)</code> for getting a useful format
  86. * object appropriate for the specified locale
  87. * </ol>
  88. * In addition, some subclasses may also choose to implement other
  89. * <code>getXxxxInstance</code> methods for more specialized control. For
  90. * example, the <code>NumberFormat</code> class provides
  91. * <code>getPercentInstance</code> and <code>getCurrencyInstance</code>
  92. * methods for getting specialized number formatters.
  93. *
  94. * <p>
  95. * Subclasses of <code>Format</code> that allow programmers to create objects
  96. * for locales (with <code>getInstance(Locale)</code> for example)
  97. * must also implement the following class method:
  98. * <blockquote>
  99. * <pre>
  100. * public static Locale[] getAvailableLocales()
  101. * </pre>
  102. * </blockquote>
  103. *
  104. * <p>
  105. * And finally subclasses may define a set of constants to identify the various
  106. * fields in the formatted output. These constants are used to create a FieldPosition
  107. * object which identifies what information is contained in the field and its
  108. * position in the formatted result. These constants should be named
  109. * <code><em>item</em>_FIELD</code> where <code><em>item</em></code> identifies
  110. * the field. For examples of these constants, see <code>ERA_FIELD</code> and its
  111. * friends in {@link DateFormat}.
  112. *
  113. * @see java.text.ParsePosition
  114. * @see java.text.FieldPosition
  115. * @see java.text.NumberFormat
  116. * @see java.text.DateFormat
  117. * @see java.text.MessageFormat
  118. * @version 1.25 11/29/01
  119. * @author Mark Davis
  120. */
  121. public abstract class Format implements Serializable, Cloneable {
  122. /**
  123. * Formats an object to produce a string.
  124. * <p>Subclasses will override the StringBuffer version of format.
  125. * @param obj The object to format
  126. * @return Formatted string.
  127. * @exception IllegalArgumentException when the Format cannot format the
  128. * type of object.
  129. * @see MessageFormat
  130. * @see java.text.Format#format(Object, StringBuffer, FieldPosition)
  131. */
  132. public final String format (Object obj) {
  133. return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
  134. }
  135. /**
  136. * Formats an object to produce a string.
  137. * Subclasses will implement for particular object, such as:
  138. * <pre>
  139. * StringBuffer format (Number obj, StringBuffer toAppendTo)
  140. * Number parse (String str)
  141. * </pre>
  142. * These general routines allow polymorphic parsing and
  143. * formatting for objects such as the MessageFormat.
  144. * @param obj The object to format
  145. * @param toAppendTo where the text is to be appended
  146. * @param pos On input: an alignment field, if desired.
  147. * On output: the offsets of the alignment field.
  148. * @return the value passed in as toAppendTo (this allows chaining,
  149. * as with StringBuffer.append())
  150. * @exception IllegalArgumentException when the Format cannot format the
  151. * given object.
  152. * @see MessageFormat
  153. * @see java.text.FieldPosition
  154. */
  155. public abstract StringBuffer format(Object obj,
  156. StringBuffer toAppendTo,
  157. FieldPosition pos);
  158. /**
  159. * Parses a string to produce an object.
  160. * Subclasses will typically implement for particular object, such as:
  161. * <pre>
  162. * String format (Number obj);
  163. * String format (long obj);
  164. * String format (double obj);
  165. * Number parse (String str);
  166. * </pre>
  167. * @param status Input-Output parameter.
  168. * <p>Before calling, set status.index to the offset you want to start
  169. * parsing at in the source.
  170. * After calling, status.index is the end of the text you parsed.
  171. * If error occurs, index is unchanged.
  172. * <p>When parsing, leading whitespace is discarded
  173. * (with successful parse),
  174. * while trailing whitespace is left as is.
  175. * <p>Example:
  176. * Parsing "_12_xy" (where _ represents a space) for a number,
  177. * with index == 0 will result in
  178. * the number 12, with status.index updated to 3
  179. * (just before the second space).
  180. * Parsing a second time will result in a ParseException
  181. * since "xy" is not a number, and leave index at 3.
  182. * <p>Subclasses will typically supply specific parse methods that
  183. * return different types of values. Since methods can't overload on
  184. * return types, these will typically be named "parse", while this
  185. * polymorphic method will always be called parseObject.
  186. * Any parse method that does not take a status should
  187. * throw ParseException when no text in the required format is at
  188. * the start position.
  189. * @return Object parsed from string. In case of error, returns null.
  190. * @see java.text.ParsePosition
  191. */
  192. public abstract Object parseObject (String source, ParsePosition status);
  193. /**
  194. * Parses a string to produce an object.
  195. *
  196. * @exception ParseException if the specified string is invalid.
  197. */
  198. public Object parseObject(String source) throws ParseException {
  199. ParsePosition status = new ParsePosition(0);
  200. Object result = parseObject(source, status);
  201. if (status.index == 0) {
  202. throw new ParseException("Format.parseObject(String) failed",
  203. status.errorIndex);
  204. }
  205. return result;
  206. }
  207. public Object clone() {
  208. try {
  209. Format other = (Format) super.clone();
  210. return other;
  211. } catch (CloneNotSupportedException e) {
  212. // will never happen
  213. return null;
  214. }
  215. }
  216. }