001: package gnu.text;
002:
003: import java.text.*;
004: import java.text.FieldPosition;
005: import java.io.Writer;
006:
007: /** Given a Format, pad the formatted result to a specified width. */
008:
009: public class PadFormat extends ReportFormat {
010: /** Minimum width. */
011: int minWidth;
012: char padChar;
013: /** What percentage of padding appears after the data.
014: * -1 means internal padding (after initial '-' or '+' or '0x' or '0X'). */
015: int where;
016:
017: Format fmt;
018:
019: public PadFormat(Format fmt, int minWidth, char padChar, int where) {
020: this .fmt = fmt;
021: this .minWidth = minWidth;
022: this .padChar = padChar;
023: this .where = where;
024: }
025:
026: public PadFormat(Format fmt, int minWidth) {
027: this (fmt, minWidth, ' ', 100);
028: }
029:
030: public int format(Object[] args, int start, Writer dst,
031: FieldPosition fpos) throws java.io.IOException {
032: return format(fmt, args, start, dst, padChar, minWidth, 1, 0,
033: where, fpos);
034: }
035:
036: public static int padNeeded(int actualWidth, int minWidth,
037: int colInc, int minPad) {
038: int total = actualWidth + minPad;
039: if (colInc <= 1)
040: colInc = minWidth - total;
041: while (total < minWidth)
042: total += colInc;
043: return total - actualWidth;
044: }
045:
046: public static int format(Format fmt, Object[] args, int start,
047: Writer dst, char padChar, int minWidth, int colInc,
048: int minPad, int where, FieldPosition fpos)
049: throws java.io.IOException {
050: /*
051: if (where == 100)
052: {
053: int oldLength = sbuf.length();
054: fmt.format(obj, sbuf, fpos);
055: int newLength = sbuf.length();
056: int pad = padNeeded(newLength - oldLength, minWidth, colInc, minPad);
057: while (--pad >= 0)
058: sbuf.append(padChar);
059: return start + ?;
060: }
061: */
062:
063: StringBuffer tbuf = new StringBuffer(200);
064: if (fmt instanceof ReportFormat)
065: start = ((ReportFormat) fmt)
066: .format(args, start, tbuf, fpos);
067: else if (fmt instanceof MessageFormat) {
068: // FIXME - only correct if start == 0.
069: fmt.format(args, tbuf, fpos);
070: start = args.length;
071: } else {
072: fmt.format(args[start], tbuf, fpos);
073: start++;
074: }
075: int len = tbuf.length();
076: int pad = padNeeded(len, minWidth, colInc, minPad);
077: int prefix = 0;
078: String text = tbuf.toString();
079: if (pad > 0) {
080: if (where == -1) {
081: if (len > 0) {
082: char ch = text.charAt(0);
083: if (ch == '-' || ch == '+') {
084: prefix++;
085: dst.write(ch);
086: }
087: if (len - prefix > 2 && text.charAt(prefix) == '0') {
088: dst.write('0');
089: ch = text.charAt(++prefix);
090: if (ch == 'x' || ch == 'X') {
091: prefix++;
092: dst.write(ch);
093: }
094: }
095: if (prefix > 0)
096: text = text.substring(prefix);
097: }
098: where = 0;
099: }
100: int padAfter = (pad * where) / 100;
101: int padBefore = pad - padAfter;
102: /*
103: if (fpos != null && padBefore > 0)
104: {
105: // This is still broken in JDK 1.2 beta2. Check beta3. FIXME.
106: fpos.setBeginIndex(fpos.getBeginIndex() + padBefore);
107: fpos.setEndIndex(fpos.getEndIndex() + padBefore);
108: }
109: */
110: while (--padBefore >= 0)
111: dst.write(padChar);
112: dst.write(text);
113: while (--padAfter >= 0)
114: dst.write(padChar);
115: } else {
116: dst.write(text);
117: }
118: return start;
119: }
120: }
|