1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.lang.text;
18
19 import java.text.FieldPosition;
20 import java.text.Format;
21 import java.text.ParseException;
22 import java.text.ParsePosition;
23
24 /**
25 * Formats using one formatter and parses using a different formatter. An
26 * example of use for this would be a webapp where data is taken in one way and
27 * stored in a database another way.
28 *
29 * @author Archimedes Trajano
30 * @version $Id: CompositeFormat.java 598710 2007-11-27 17:34:34Z mbenson $
31 */
32 public class CompositeFormat extends Format {
33
34 /**
35 * Required for serialization support.
36 *
37 * @see java.io.Serializable
38 */
39 private static final long serialVersionUID = -4329119827877627683L;
40
41 /** The parser to use. */
42 private final Format parser;
43 /** The formatter to use. */
44 private final Format formatter;
45
46 /**
47 * Create a format that points its parseObject method to one implementation
48 * and its format method to another.
49 *
50 * @param parser implementation
51 * @param formatter implementation
52 */
53 public CompositeFormat(Format parser, Format formatter) {
54 this.parser = parser;
55 this.formatter = formatter;
56 }
57
58 /**
59 * Uses the formatter Format instance.
60 *
61 * @param obj the object to format
62 * @param toAppendTo the {@link StringBuffer} to append to
63 * @param pos the FieldPosition to use (or ignore).
64 * @return <code>toAppendTo</code>
65 * @see Format#format(Object, StringBuffer, FieldPosition)
66 */
67 public StringBuffer format(Object obj, StringBuffer toAppendTo,
68 FieldPosition pos) {
69 return formatter.format(obj, toAppendTo, pos);
70 }
71
72 /**
73 * Uses the parser Format instance.
74 *
75 * @param source the String source
76 * @param pos the ParsePosition containing the position to parse from, will
77 * be updated according to parsing success (index) or failure
78 * (error index)
79 * @return the parsed Object
80 * @see Format#parseObject(String, ParsePosition)
81 */
82 public Object parseObject(String source, ParsePosition pos) {
83 return parser.parseObject(source, pos);
84 }
85
86 /**
87 * Provides access to the parser Format implementation.
88 *
89 * @return parser Format implementation
90 */
91 public Format getParser() {
92 return this.parser;
93 }
94
95 /**
96 * Provides access to the parser Format implementation.
97 *
98 * @return formatter Format implementation
99 */
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 }