001    /* Format.java -- Abstract superclass for formatting/parsing strings.
002       Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010     
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.text;
040    
041    import gnu.java.text.FormatCharacterIterator;
042    
043    import java.io.Serializable;
044    
045    /**
046     * This class is the abstract superclass of classes that format and parse
047     * data to/from <code>Strings</code>.  It is guaranteed that any 
048     * <code>String</code> produced by a concrete subclass of <code>Format</code>
049     * will be parseable by that same subclass.
050     * <p>
051     * In addition to implementing the abstract methods in this class, subclasses
052     * should provide static factory methods of the form 
053     * <code>getInstance()</code> and <code>getInstance(Locale)</code> if the
054     * subclass loads different formatting/parsing schemes based on locale.
055     * These subclasses should also implement a static method called
056     * <code>getAvailableLocales()</code> which returns an array of 
057     * available locales in the current runtime environment.
058     *
059     * @author Aaron M. Renn (arenn@urbanophile.com)
060     * @author Per Bothner (bothner@cygnus.com)
061     */
062    public abstract class Format implements Serializable, Cloneable
063    {
064      /**
065       * For compatability with Sun's JDK 1.4.2 rev. 5
066       */
067      static final long serialVersionUID = -299282585814624189L;
068    
069      public static class Field extends AttributedCharacterIterator.Attribute
070      {
071        static final long serialVersionUID = 276966692217360283L;
072       
073        protected Field(String name)
074        {
075          super(name);
076        }
077      }
078      
079      /**
080       * This method initializes a new instance of <code>Format</code>.
081       * It performs no actions, but acts as a default constructor for
082       * subclasses.
083       */
084      public Format ()
085      {
086      }
087    
088      /**
089       * This method formats an <code>Object</code> into a <code>String</code>.
090       * 
091       * @param obj The <code>Object</code> to format.
092       *
093       * @return The formatted <code>String</code>.
094       *
095       * @exception IllegalArgumentException If the <code>Object</code>
096       * cannot be formatted. 
097       */
098      public final String format(Object obj) throws IllegalArgumentException
099      {
100        StringBuffer sb = new StringBuffer ();
101        format (obj, sb, new FieldPosition (0));
102        return sb.toString ();
103      }
104    
105      /**
106       * This method formats an <code>Object</code> into a <code>String</code> and
107       * appends the <code>String</code> to a <code>StringBuffer</code>.
108       *
109       * @param obj The <code>Object</code> to format.
110       * @param sb The <code>StringBuffer</code> to append to.
111       * @param pos The desired <code>FieldPosition</code>, which is also
112       *            updated by this call. 
113       *
114       * @return The updated <code>StringBuffer</code>.
115       *
116       * @exception IllegalArgumentException If the <code>Object</code>
117       * cannot be formatted. 
118       */
119      public abstract StringBuffer format (Object obj, StringBuffer sb,
120                                           FieldPosition pos)
121        throws IllegalArgumentException;
122    
123      /**
124       * This method parses a <code>String</code> and converts the parsed 
125       * contents into an <code>Object</code>.
126       *
127       * @param str The <code>String</code> to parse.
128       *
129       * @return The resulting <code>Object</code>.
130       *
131       * @exception ParseException If the <code>String</code> cannot be parsed.
132       */
133      public Object parseObject (String str) throws ParseException
134      {
135        ParsePosition pos = new ParsePosition(0);
136        Object result = parseObject (str, pos);
137        if (result == null)
138          {
139            int index = pos.getErrorIndex();
140            if (index < 0)
141              index = pos.getIndex();
142            throw new ParseException("parseObject failed", index);
143          }
144        return result;
145      }
146    
147      /**
148       * This method parses a <code>String</code> and converts the parsed
149       * contents into an <code>Object</code>. 
150       *
151       * @param str The <code>String</code> to parse.
152       * @param pos The starting parse index on input, the ending parse
153       *            index on output. 
154       *
155       * @return The parsed <code>Object</code>, or <code>null</code> in
156       *         case of error.
157       */
158      public abstract Object parseObject (String str, ParsePosition pos);
159    
160      public AttributedCharacterIterator formatToCharacterIterator(Object obj)
161      {
162        return new FormatCharacterIterator(format(obj), null, null);
163      }
164    
165      /**
166       * Creates a copy of this object.
167       *
168       * @return The copied <code>Object</code>.
169       */
170      public Object clone ()
171      {
172        try
173          {
174            return super.clone ();
175          }
176        catch (CloneNotSupportedException e)
177          {
178            return null;
179          }
180      }
181    }