File: DataFormatter.java

package info (click to toggle)
libsapdbc-java 5567-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,840 kB
  • ctags: 6,242
  • sloc: java: 49,887; makefile: 83
file content (205 lines) | stat: -rw-r--r-- 5,811 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*


    ========== licence begin GPL
    Copyright (C) 2002-2003 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end


*/

package com.sap.dbtech.util.printf;

import com.sap.dbtech.util.*;
/**
 *
 */
abstract class DataFormatter extends Formatter {
    protected char kind;
    protected int width = defaultValue_C;
    protected int precision = defaultValue_C;
    protected boolean alignRight = false;
    protected char padChar = ' ';
    protected static final int defaultValue_C = -1;
    protected static final int dynamicValue_C = -2;
/**
 *
 * @param format java.lang.String
 * @exception com.sap.dbtech.util.printf.FormatException The exception description.
 */
protected DataFormatter(String format) throws FormatException {
    this.parseFormatString (format);
}
/**
 *
 * @return java.lang.String
 * @param objects com.sap.dbtech.util.printf.OArrayEnumeration
 * @exception com.sap.dbtech.util.printf.PrintfArgumentMismatch The exception description.
 */
String format (OArrayEnumeration objects) throws PrintfArgumentMismatch {
    int actualWidth = this.width;
    int actualPrecision = this.precision;
    Number argValue;
    Object obj;
    String result;

    try {
        if (this.width == dynamicValue_C) {
            argValue = (Number) objects.nextElement();
            actualWidth = argValue.intValue ();
        }
        if (this.precision == dynamicValue_C) {
            argValue = (Number) objects.nextElement();
            actualPrecision = argValue.intValue ();
        }
        obj = objects.nextElement();
    }
    catch (ArrayIndexOutOfBoundsException exc) {
        throw new PrintfArgumentMismatch(MessageTranslator.translate(MessageKey.ERROR_FORMAT_TOFEWARGS));
    }
    result = this.formatObject (obj, this.kind, actualWidth,
        actualPrecision, this.alignRight, this.padChar);
    return result;
}
/**
 *
 * @return java.lang.String
 * @param obj java.lang.Object
 * @param kind int
 * @param width int
 * @param prexision int
 * @param alignRight boolean
 * @param padChar char
 */
protected abstract String formatObject (Object obj, int kind, int width, int precision, boolean alignRight, char padChar)
        throws PrintfArgumentMismatch;
/**
 *
 * @return java.lang.String
 * @param obj java.lang.Object
 * @param kind int
 * @param width int
 * @param prexision int
 * @param alignRight boolean
 * @param padChar char
 */
protected String formatString (String str, int kind, int width, int precision, boolean alignRight, char padChar)
{
    if (width > str.length ()) {
        if (alignRight) {
            str = StringUtil.padLeft (str, width, padChar);
        }
        else {
            str = StringUtil.padRight (str, width, padChar);
        }
    }
    else if ((precision >= 0) && (precision < str.length ())) {
        str = str.substring (0, precision);
    }
    return str;
}
/**
 *
 * @param format java.lang.String
 * @exception com.sap.dbtech.util.printf.FormatException The exception description.
 */
private void parseFormatString (String format) throws FormatException {
    boolean inFlags = true;
    char current = 1;
    int pos = 0;    // skip over %
    int intval;

    if (format.charAt (0) != '%') {
        throw new FormatException (MessageTranslator.translate(MessageKey.ERROR_FORMAT_PERCENT));
    }
    /*
     * parse flags
     */
    while (inFlags) {
        ++pos;
        current = format.charAt (pos);
        switch (current) {
            case '-':
                this.alignRight = true;
                break;
            case '+':
                // not implemented: always a sign character
                break;
            case ' ':
                // not implemented: blank instead of '+'
                break;
            case '#':
                // not implemented: alternate form
                break;
            case '0':
                this.padChar = '0';
                break;
            default:
                inFlags = false;
                break;
        }
    }
    /*
     * width
     */
    if (current == '*') {
        this.width = dynamicValue_C;
        ++pos;
        current = format.charAt (pos);
    }
    else {
        intval = 0;
        while (('0' <= current) && (current <= '9')) {
            intval *= 10;
            intval += current - '0';
            ++pos;
            current = format.charAt (pos);
        }
        if (intval != 0) {
            this.width = intval;
        }
    }
    /*
     * precision
     */
    if (current == '.') {
        ++pos;
        current = format.charAt (pos);
        if (current == '*') {
            this.width = dynamicValue_C;
            ++pos;
            current = format.charAt (pos);
        }
        else {
            intval = 0;
            while (('0' <= current) && (current <= '9')) {
                intval *= 10;
                intval += current - '0';
                ++pos;
                current = format.charAt (pos);
            }
            if (intval != 0) {
                this.precision = intval;
            }
        }
    }
    /*
     * kind
     */
    this.kind = current;
}
}