Format.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Format.java -- Abstract superclass for formatting/parsing strings.
  2. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. import gnu.java.text.FormatCharacterIterator;
  33. import java.io.Serializable;
  34. /**
  35. * This class is the abstract superclass of classes that format and parse
  36. * data to/from <code>Strings</code>. It is guaranteed that any
  37. * <code>String</code> produced by a concrete subclass of <code>Format</code>
  38. * will be parseable by that same subclass.
  39. * <p>
  40. * In addition to implementing the abstract methods in this class, subclasses
  41. * should provide static factory methods of the form
  42. * <code>getInstance()</code> and <code>getInstance(Locale)</code> if the
  43. * subclass loads different formatting/parsing schemes based on locale.
  44. * These subclasses should also implement a static method called
  45. * <code>getAvailableLocales()</code> which returns an array of
  46. * available locales in the current runtime environment.
  47. *
  48. * @author Aaron M. Renn (arenn@urbanophile.com)
  49. * @author Per Bothner (bothner@cygnus.com)
  50. */
  51. public abstract class Format implements Serializable, Cloneable
  52. {
  53. /**
  54. * For compatability with Sun's JDK 1.4.2 rev. 5
  55. */
  56. static final long serialVersionUID = -299282585814624189L;
  57. public static class Field extends AttributedCharacterIterator.Attribute
  58. {
  59. static final long serialVersionUID = 276966692217360283L;
  60. protected Field(String name)
  61. {
  62. super(name);
  63. }
  64. }
  65. /**
  66. * This method initializes a new instance of <code>Format</code>.
  67. * It performs no actions, but acts as a default constructor for
  68. * subclasses.
  69. */
  70. protected Format ()
  71. {
  72. }
  73. /**
  74. * This method formats an <code>Object</code> into a <code>String</code>.
  75. *
  76. * @param obj The <code>Object</code> to format.
  77. *
  78. * @return The formatted <code>String</code>.
  79. *
  80. * @exception IllegalArgumentException If the <code>Object</code>
  81. * cannot be formatted.
  82. */
  83. public final String format(Object obj) throws IllegalArgumentException
  84. {
  85. StringBuffer sb = new StringBuffer ();
  86. format (obj, sb, new FieldPosition (0));
  87. return sb.toString ();
  88. }
  89. /**
  90. * This method formats an <code>Object</code> into a <code>String</code> and
  91. * appends the <code>String</code> to a <code>StringBuffer</code>.
  92. *
  93. * @param obj The <code>Object</code> to format.
  94. * @param sb The <code>StringBuffer</code> to append to.
  95. * @param pos The desired <code>FieldPosition</code>, which is also
  96. * updated by this call.
  97. *
  98. * @return The updated <code>StringBuffer</code>.
  99. *
  100. * @exception IllegalArgumentException If the <code>Object</code>
  101. * cannot be formatted.
  102. */
  103. public abstract StringBuffer format (Object obj, StringBuffer sb,
  104. FieldPosition pos)
  105. throws IllegalArgumentException;
  106. /**
  107. * This method parses a <code>String</code> and converts the parsed
  108. * contents into an <code>Object</code>.
  109. *
  110. * @param str The <code>String</code> to parse.
  111. *
  112. * @return The resulting <code>Object</code>.
  113. *
  114. * @exception ParseException If the <code>String</code> cannot be parsed.
  115. */
  116. public Object parseObject (String str) throws ParseException
  117. {
  118. ParsePosition pos = new ParsePosition(0);
  119. Object result = parseObject (str, pos);
  120. if (result == null)
  121. {
  122. int index = pos.getErrorIndex();
  123. if (index < 0)
  124. index = pos.getIndex();
  125. throw new ParseException("parseObject failed", index);
  126. }
  127. return result;
  128. }
  129. /**
  130. * This method parses a <code>String</code> and converts the parsed
  131. * contents into an <code>Object</code>.
  132. *
  133. * @param str The <code>String</code> to parse.
  134. * @param pos The starting parse index on input, the ending parse
  135. * index on output.
  136. *
  137. * @return The parsed <code>Object</code>, or <code>null</code> in
  138. * case of error.
  139. */
  140. public abstract Object parseObject (String str, ParsePosition pos);
  141. public AttributedCharacterIterator formatToCharacterIterator(Object obj)
  142. {
  143. return new FormatCharacterIterator(format(obj), null, null);
  144. }
  145. /**
  146. * Creates a copy of this object.
  147. *
  148. * @return The copied <code>Object</code>.
  149. */
  150. public Object clone ()
  151. {
  152. try
  153. {
  154. return super.clone ();
  155. }
  156. catch (CloneNotSupportedException e)
  157. {
  158. return null;
  159. }
  160. }
  161. }