KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > format > FormatContext


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.format;
33
34 import java.text.DecimalFormatSymbols JavaDoc;
35 import java.util.*;
36 import java.util.regex.*;
37
38 /**
39  * @author Taras Puchko
40  */

41 public abstract class FormatContext {
42
43     private static final DecimalFormatSymbols JavaDoc US_SYMBOLS = new DecimalFormatSymbols JavaDoc(Locale.US);
44     private static final Pattern PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([-#+ 0,(\\<]*)?(\\d+)?(?:\\.(\\d+))?" +
45             "([bBhHsScCdoxXeEfgGaA%n]|(?:[tT][HIklMSLNpzZsQBbhAaCYyjmdeRTrDFc]))");
46
47     private Locale locale;
48     private DecimalFormatSymbols JavaDoc symbols;
49     private Object JavaDoc[] arguments;
50     private int effectiveIndex;
51     private int ordinaryIndex;
52     private boolean effectiveIndexComputed;
53     private String JavaDoc specifier;
54     private int explicitIndex;
55     private String JavaDoc flags;
56     private int width;
57     private int precision;
58     private String JavaDoc conversion;
59
60     protected FormatContext(Locale locale) {
61         this.locale = locale;
62     }
63
64     public abstract void append(char c);
65
66     public abstract void append(String JavaDoc s);
67
68     public abstract void append(String JavaDoc s, int start, int end);
69
70     public abstract boolean writeFormattable();
71
72     public void printf(String JavaDoc format, Object JavaDoc... args) {
73         arguments = args;
74         effectiveIndex = 0;
75         ordinaryIndex = 0;
76         int position = 0;
77         for (Matcher matcher = PATTERN.matcher(format); matcher.find(position); position = matcher.end()) {
78             writeText(format, position, matcher.start());
79             effectiveIndexComputed = false;
80             specifier = matcher.group();
81             explicitIndex = parse(matcher.group(1));
82             String JavaDoc f = matcher.group(2);
83             flags = f != null ? f : "";
84             width = parse(matcher.group(3));
85             precision = parse(matcher.group(4));
86             conversion = matcher.group(5);
87             Conversion.getInstance(conversion).format(this);
88         }
89         writeText(format, position, format.length());
90     }
91
92     private static int parse(String JavaDoc s) {
93         return s == null ? -1 : Integer.valueOf(s);
94     }
95
96     private char getConversionType() {
97         return conversion.charAt(0);
98     }
99
100     public int getWidth() {
101         return width;
102     }
103
104     public int getPrecision() {
105         return precision;
106     }
107
108     public int getNumberPrecision() {
109         return precision >= 0 ? precision : 6;
110     }
111
112     public Locale getLocale() {
113         return locale;
114     }
115
116     public DecimalFormatSymbols JavaDoc getSymbols(boolean localized) {
117         if (localized && locale != null) {
118             if (symbols == null) {
119                 symbols = new DecimalFormatSymbols JavaDoc(locale);
120             }
121             return symbols;
122         } else {
123             return US_SYMBOLS;
124         }
125     }
126
127     public IllegalFormatConversionException getConversionException() {
128         return new IllegalFormatConversionException(getConversionType(), getArgument().getClass());
129     }
130
131     public boolean isUpperCase() {
132         return Character.isUpperCase(getConversionType());
133     }
134
135     public boolean isFlag(char c) {
136         return flags.indexOf(c) >= 0;
137     }
138
139     public Object JavaDoc getArgument() {
140         if (!effectiveIndexComputed) {
141             computeEffectiveIndex();
142             effectiveIndexComputed = true;
143         }
144         return arguments[effectiveIndex - 1];
145     }
146
147     private void computeEffectiveIndex() {
148         if (flags.indexOf('<') < 0) {
149             effectiveIndex = explicitIndex != -1 ? explicitIndex : ++ordinaryIndex;
150         }
151         if (arguments == null || effectiveIndex == 0 || effectiveIndex > arguments.length) {
152             throw new MissingFormatArgumentException(specifier);
153         }
154     }
155
156     private void writeText(String JavaDoc s, int start, int end) {
157         if (start == end) return;
158         int i = s.indexOf('%', start);
159         if (i >= 0 && i < end) {
160             String JavaDoc unknownFormat = s.substring(i + 1, end);
161             throw new UnknownFormatConversionException(unknownFormat.length() > 0 ? unknownFormat : "%");
162         }
163         append(s, start, end);
164     }
165
166     public void writeRestricted(String JavaDoc s) {
167         writePadded(precision != -1 && precision < s.length() ? s.substring(0, precision) : s);
168     }
169
170     public void writePadded(String JavaDoc s) {
171         if (isFlag('-')) {
172             writeCaseSensitive(s);
173             writePadding(s);
174         } else {
175             writePadding(s);
176             writeCaseSensitive(s);
177         }
178     }
179
180     private void writePadding(String JavaDoc s) {
181         for (int i = width - s.length(); i > 0; i--) {
182             append(' ');
183         }
184     }
185
186     private void writeCaseSensitive(String JavaDoc s) {
187         if (isUpperCase()) {
188             append(s.toUpperCase());
189         } else {
190             append(s);
191         }
192     }
193
194     public void assertNoFlag(char flag) {
195         if (isFlag(flag)) throw new FormatFlagsConversionMismatchException(flags, getConversionType());
196     }
197
198     public void assertNoPrecision() {
199         if (precision != -1) throw new IllegalFormatPrecisionException(precision);
200     }
201
202     public void assertNoWidth() {
203         if (width != -1) throw new IllegalFormatWidthException(width);
204     }
205
206     public void checkWidth() {
207         if ((isFlag('-') || isFlag('0')) && width == -1) {
208             throw new MissingFormatWidthException(specifier);
209         }
210     }
211
212     public void checkFlags() {
213         if (isFlag('+') && isFlag(' ') || isFlag('-') && isFlag('0')) {
214             throw new IllegalFormatFlagsException(flags);
215         }
216     }
217
218 }
219
Popular Tags