KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > Formatter_


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.java.util;
33
34 import java.io.*;
35 import java.util.*;
36 import net.sf.retrotranslator.runtime.format.FormatContext;
37
38 /**
39  * @author Taras Puchko
40  */

41 public class Formatter_ implements Closeable, Flushable {
42
43     private Appendable JavaDoc out;
44     private Locale locale;
45     private IOException ioException;
46
47     public Formatter_() {
48         this(null, Locale.getDefault());
49     }
50
51     public Formatter_(Appendable JavaDoc a) {
52         this(a, Locale.getDefault());
53     }
54
55     public Formatter_(Locale l) {
56         this(null, l);
57     }
58
59     public Formatter_(Appendable JavaDoc a, Locale l) {
60         out = a != null ? a : new StringBuilder JavaDoc();
61         locale = l;
62     }
63
64     public Formatter_(String JavaDoc fileName) throws FileNotFoundException {
65         this(new FileOutputStream(fileName));
66     }
67
68     public Formatter_(String JavaDoc fileName, String JavaDoc csn) throws FileNotFoundException, UnsupportedEncodingException {
69         this(fileName, csn, Locale.getDefault());
70     }
71
72     public Formatter_(String JavaDoc fileName, String JavaDoc csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException {
73         this(new FileOutputStream(fileName), csn, l);
74     }
75
76     public Formatter_(File file) throws FileNotFoundException {
77         this(new FileOutputStream(file));
78     }
79
80     public Formatter_(File file, String JavaDoc csn) throws FileNotFoundException, UnsupportedEncodingException {
81         this(file, csn, Locale.getDefault());
82     }
83
84     public Formatter_(File file, String JavaDoc csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException {
85         this(new FileOutputStream(file), csn, l);
86     }
87
88     public Formatter_(PrintStream ps) {
89         this(assertNotNull(ps), Locale.getDefault());
90     }
91
92     public Formatter_(OutputStream os) {
93         this(new BufferedWriter(new OutputStreamWriter(os)));
94     }
95
96     public Formatter_(OutputStream os, String JavaDoc csn) throws UnsupportedEncodingException {
97         this(os, csn, Locale.getDefault());
98     }
99
100     public Formatter_(OutputStream os, String JavaDoc csn, Locale l) throws UnsupportedEncodingException {
101         this(new BufferedWriter(new OutputStreamWriter(os, csn)), l);
102     }
103
104     public Locale locale() {
105         assertOpen();
106         return locale;
107     }
108
109     public Appendable JavaDoc out() {
110         assertOpen();
111         return out;
112     }
113
114     public String JavaDoc toString() {
115         assertOpen();
116         return out.toString();
117     }
118
119     public void flush() {
120         assertOpen();
121         if (out instanceof Flushable) {
122             try {
123                 ((Flushable) out).flush();
124             } catch (IOException e) {
125                 ioException = e;
126             }
127         }
128     }
129
130     public void close() {
131         if (out == null) return;
132         if (out instanceof Closeable) {
133             try {
134                 ((Closeable) out).close();
135             } catch (IOException e) {
136                 ioException = e;
137             }
138         }
139         out = null;
140     }
141
142     public IOException ioException() {
143         return ioException;
144     }
145
146     public Formatter_ format(String JavaDoc format, Object JavaDoc... args) {
147         return format(locale, format, args);
148     }
149
150     public Formatter_ format(Locale locale, String JavaDoc format, Object JavaDoc... args) {
151         assertOpen();
152         new FormatterContext(locale).printf(format, args);
153         return this;
154     }
155
156     private static Appendable JavaDoc assertNotNull(Appendable JavaDoc appendable) {
157         if (appendable == null) throw new NullPointerException JavaDoc();
158         return appendable;
159     }
160
161     private void assertOpen() {
162         if (out == null) throw new FormatterClosedException();
163     }
164
165     private class FormatterContext extends FormatContext {
166
167         public FormatterContext(Locale locale) {
168             super(locale);
169         }
170
171         public void append(char c) {
172             try {
173                 out.append(c);
174             } catch (IOException e) {
175                 ioException = e;
176             }
177         }
178
179         public void append(String JavaDoc s) {
180             try {
181                 out.append(s);
182             } catch (IOException e) {
183                 ioException = e;
184             }
185         }
186
187         public void append(String JavaDoc s, int start, int end) {
188             try {
189                 out.append(s, start, end);
190             } catch (IOException e) {
191                 ioException = e;
192             }
193         }
194
195         public boolean writeFormattable() {
196             if (!(getArgument() instanceof Formattable_)) return false;
197             Formatter_ formatter = Formatter_.this;
198             if (getLocale() != formatter.locale) formatter = new Formatter_(out, getLocale());
199             int formatFlags = 0;
200             if (isFlag('-')) formatFlags |= FormattableFlags_.LEFT_JUSTIFY;
201             if (isUpperCase()) formatFlags |= FormattableFlags_.UPPERCASE;
202             if (isFlag('#')) formatFlags |= FormattableFlags_.ALTERNATE;
203             ((Formattable_) getArgument()).formatTo(formatter, formatFlags, getWidth(), getPrecision());
204             return true;
205         }
206
207     }
208
209 }
210
Popular Tags