1 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 41 public class Formatter_ implements Closeable, Flushable { 42 43 private Appendable out; 44 private Locale locale; 45 private IOException ioException; 46 47 public Formatter_() { 48 this(null, Locale.getDefault()); 49 } 50 51 public Formatter_(Appendable a) { 52 this(a, Locale.getDefault()); 53 } 54 55 public Formatter_(Locale l) { 56 this(null, l); 57 } 58 59 public Formatter_(Appendable a, Locale l) { 60 out = a != null ? a : new StringBuilder (); 61 locale = l; 62 } 63 64 public Formatter_(String fileName) throws FileNotFoundException { 65 this(new FileOutputStream(fileName)); 66 } 67 68 public Formatter_(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { 69 this(fileName, csn, Locale.getDefault()); 70 } 71 72 public Formatter_(String fileName, String 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 csn) throws FileNotFoundException, UnsupportedEncodingException { 81 this(file, csn, Locale.getDefault()); 82 } 83 84 public Formatter_(File file, String 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 csn) throws UnsupportedEncodingException { 97 this(os, csn, Locale.getDefault()); 98 } 99 100 public Formatter_(OutputStream os, String 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 out() { 110 assertOpen(); 111 return out; 112 } 113 114 public String 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 format, Object ... args) { 147 return format(locale, format, args); 148 } 149 150 public Formatter_ format(Locale locale, String format, Object ... args) { 151 assertOpen(); 152 new FormatterContext(locale).printf(format, args); 153 return this; 154 } 155 156 private static Appendable assertNotNull(Appendable appendable) { 157 if (appendable == null) throw new NullPointerException (); 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 s) { 180 try { 181 out.append(s); 182 } catch (IOException e) { 183 ioException = e; 184 } 185 } 186 187 public void append(String 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 |