001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.lang.text;
018
019 import java.text.FieldPosition;
020 import java.text.Format;
021 import java.text.ParseException;
022 import java.text.ParsePosition;
023
024 /**
025 * Formats using one formatter and parses using a different formatter. An
026 * example of use for this would be a webapp where data is taken in one way and
027 * stored in a database another way.
028 *
029 * @author Archimedes Trajano
030 * @version $Id: CompositeFormat.java 598710 2007-11-27 17:34:34Z mbenson $
031 */
032 public class CompositeFormat extends Format {
033
034 /**
035 * Required for serialization support.
036 *
037 * @see java.io.Serializable
038 */
039 private static final long serialVersionUID = -4329119827877627683L;
040
041 /** The parser to use. */
042 private final Format parser;
043 /** The formatter to use. */
044 private final Format formatter;
045
046 /**
047 * Create a format that points its parseObject method to one implementation
048 * and its format method to another.
049 *
050 * @param parser implementation
051 * @param formatter implementation
052 */
053 public CompositeFormat(Format parser, Format formatter) {
054 this.parser = parser;
055 this.formatter = formatter;
056 }
057
058 /**
059 * Uses the formatter Format instance.
060 *
061 * @param obj the object to format
062 * @param toAppendTo the {@link StringBuffer} to append to
063 * @param pos the FieldPosition to use (or ignore).
064 * @return <code>toAppendTo</code>
065 * @see Format#format(Object, StringBuffer, FieldPosition)
066 */
067 public StringBuffer format(Object obj, StringBuffer toAppendTo,
068 FieldPosition pos) {
069 return formatter.format(obj, toAppendTo, pos);
070 }
071
072 /**
073 * Uses the parser Format instance.
074 *
075 * @param source the String source
076 * @param pos the ParsePosition containing the position to parse from, will
077 * be updated according to parsing success (index) or failure
078 * (error index)
079 * @return the parsed Object
080 * @see Format#parseObject(String, ParsePosition)
081 */
082 public Object parseObject(String source, ParsePosition pos) {
083 return parser.parseObject(source, pos);
084 }
085
086 /**
087 * Provides access to the parser Format implementation.
088 *
089 * @return parser Format implementation
090 */
091 public Format getParser() {
092 return this.parser;
093 }
094
095 /**
096 * Provides access to the parser Format implementation.
097 *
098 * @return formatter Format implementation
099 */
100 public Format getFormatter() {
101 return this.formatter;
102 }
103
104 /**
105 * Utility method to parse and then reformat a String.
106 *
107 * @param input String to reformat
108 * @return A reformatted String
109 * @throws ParseException thrown by parseObject(String) call
110 */
111 public String reformat(String input) throws ParseException {
112 return format(parseObject(input));
113 }
114
115 }