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.
026: * An example of use for this would be a webapp where data is taken in one way
027: * and stored in a database another way.
028: *
029: * @author Archimedes Trajano
030: * @version $Id: $
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: * @see Format#format(Object, StringBuffer, FieldPosition)
062: */
063: public StringBuffer format(Object obj, StringBuffer toAppendTo,
064: FieldPosition pos) {
065: return formatter.format(obj, toAppendTo, pos);
066: }
067:
068: /**
069: * Uses the parser Format instance.
070: *
071: * @see Format#parseObject(String, ParsePosition)
072: */
073: public Object parseObject(String source, ParsePosition pos) {
074: return parser.parseObject(source, pos);
075: }
076:
077: /**
078: * Provides access to the parser Format implementation.
079: *
080: * @return parser Format implementation
081: */
082: public Format getParser() {
083: return this .parser;
084: }
085:
086: /**
087: * Provides access to the parser Format implementation.
088: *
089: * @return formatter Format implementation
090: */
091: public Format getFormatter() {
092: return this .formatter;
093: }
094:
095: /**
096: * Utility method to parse and then reformat a String.
097: *
098: * @param input String to reformat
099: * @return A reformatted String
100: * @throws ParseException thrown by parseObject(String) call
101: */
102: public String reformat(String input) throws ParseException {
103: return format(parseObject(input));
104: }
105:
106: }
|