DecimalValue.java
9.01 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xfa.content;
import com.adobe.xfa.AppModel;
import com.adobe.xfa.Attribute;
import com.adobe.xfa.Element;
import com.adobe.xfa.Int;
import com.adobe.xfa.Model;
import com.adobe.xfa.Node;
import com.adobe.xfa.ScriptTable;
import com.adobe.xfa.XFA;
import com.adobe.xfa.content.Content;
import com.adobe.xfa.content.DecimalScript;
import com.adobe.xfa.ut.ExFull;
import com.adobe.xfa.ut.MsgFormatPos;
import com.adobe.xfa.ut.Numeric;
import com.adobe.xfa.ut.ResId;
import com.adobe.xfa.ut.StringUtils;
import org.xml.sax.Attributes;
public final class DecimalValue
extends Content {
int mnInputFracDigits;
public DecimalValue(Element parent, Node prevSibling) {
super(parent, prevSibling, null, "decimal", "decimal", null, XFA.DECIMALTAG, "decimal");
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
return super.equals(object) && this.getValue() == ((DecimalValue)object).getValue();
}
@Override
public int hashCode() {
long bits = Double.doubleToLongBits(this.getValue());
return super.hashCode() ^ (int)(bits ^ bits >>> 32);
}
public String getFormattedValue() {
int nFoundAt;
int nNeedFracDigits;
boolean bNegativeNumber;
String value = this.getStrValue();
if (StringUtils.isEmpty(value)) {
return "";
}
Int oInt = (Int)this.getAttribute(XFA.FRACDIGITSTAG);
int nFracDigits = oInt.getValue();
Int oInt2 = (Int)this.getAttribute(XFA.LEADDIGITSTAG);
int nLeadDigits = oInt2.getValue();
if (nFracDigits == -1) {
nNeedFracDigits = this.mnInputFracDigits;
} else if (nFracDigits >= 0) {
nNeedFracDigits = nFracDigits;
} else {
return "";
}
StringBuilder sFormattedValue = new StringBuilder(value);
boolean bl = bNegativeNumber = value.indexOf(45) == 0;
if (bNegativeNumber) {
sFormattedValue.deleteCharAt(0);
}
if ((nFoundAt = sFormattedValue.toString().indexOf(46)) != -1) {
if (nLeadDigits >= 0 && nFoundAt > nLeadDigits) {
return "";
}
int nHaveFracDigits = sFormattedValue.length() - ++nFoundAt;
if (nHaveFracDigits > nNeedFracDigits) {
sFormattedValue.setLength(sFormattedValue.length() - (nHaveFracDigits - nNeedFracDigits));
} else if (nFracDigits == -1) {
while (nHaveFracDigits < nNeedFracDigits) {
sFormattedValue.append('0');
++nHaveFracDigits;
}
}
} else {
if (nLeadDigits >= 0 && sFormattedValue.length() > nLeadDigits) {
return "";
}
if (nFracDigits == -1 && nNeedFracDigits > 0) {
sFormattedValue.append('.');
while (nNeedFracDigits > 0) {
sFormattedValue.append('0');
--nNeedFracDigits;
}
}
}
if (bNegativeNumber) {
sFormattedValue.insert(0, '-');
}
return sFormattedValue.toString();
}
@Override
public ScriptTable getScriptTable() {
return DecimalScript.getScriptTable();
}
public double getValue() {
String sValue = this.getFormattedValue();
double dVal = Numeric.stringToDouble(sValue, false);
return dVal;
}
@Override
public void setStrValue(String sText, boolean bNotify, boolean bDefault) {
this.trackInputFracDigits(sText);
super.setStrValue(sText, bNotify, bDefault);
}
public void setValue(double dValue, boolean bFromData, boolean bNotify, boolean bDefault) {
Int oFracInt = (Int)this.getAttribute(XFA.FRACDIGITSTAG);
Int oLeadInt = (Int)this.getAttribute(XFA.LEADDIGITSTAG);
int nFracDigits = oFracInt.getValue();
int nLeadDigits = oLeadInt.getValue();
if (nFracDigits == -1) {
nFracDigits = this.mnInputFracDigits;
}
String sValue = Numeric.doubleToString(dValue, nFracDigits, false);
if (nLeadDigits < 0) {
this.setStrValue(sValue, bNotify, bDefault);
return;
}
boolean bNegativeNumber = sValue.indexOf(45) == 0;
boolean bTooManyLeadDigits = false;
int nFoundAt = sValue.indexOf(46);
if (nFoundAt != -1) {
if (bNegativeNumber) {
--nFoundAt;
}
if (nFoundAt > nLeadDigits) {
bTooManyLeadDigits = true;
}
} else if (sValue.length() - (bNegativeNumber ? 1 : 0) > nLeadDigits) {
bTooManyLeadDigits = true;
}
if (!bFromData && bTooManyLeadDigits) {
if (!this.getAppModel().getLegacySetting(AppModel.XFA_LEGACY_V30_SCRIPTING)) {
MsgFormatPos message = new MsgFormatPos(ResId.TypeMismatch);
message.format(this.getSOMExpression());
message.format(sValue);
this.getModel().addErrorList(new ExFull(message), 3, this);
}
return;
}
this.setStrValue(sValue, bNotify, bDefault);
}
@Override
public void setValue(String sValue, boolean bFromData, boolean bNotify, boolean bDefault) {
if (StringUtils.isEmpty(sValue)) {
super.setStrValue(null, true, false);
} else {
boolean bTypeMismatch;
double dVal = Numeric.stringToDouble(sValue, true);
boolean bl = bTypeMismatch = Double.isInfinite(dVal) || Double.isNaN(dVal);
if (bTypeMismatch) {
if (!bFromData && !this.getAppModel().getLegacySetting(AppModel.XFA_LEGACY_V30_SCRIPTING)) {
MsgFormatPos message = new MsgFormatPos(ResId.TypeMismatch);
message.format(this.getSOMExpression());
message.format(sValue);
this.getModel().addErrorList(new ExFull(message), 3, this);
}
if (this.getAppModel().getLegacySetting(AppModel.XFA_LEGACY_V29_SCRIPTING) && !this.getAppModel().getLegacySetting(AppModel.XFA_PATCH_W_2447677)) {
this.setStrValue(null, true, false);
} else if (bFromData) {
this.setStrValue(sValue, bNotify, bDefault);
}
return;
}
try {
Int oFracInt = (Int)this.getAttribute(XFA.FRACDIGITSTAG);
int nFracDigits = oFracInt.getValue();
if (bFromData || nFracDigits == -1) {
this.setStrValue(sValue, bNotify, bDefault);
} else {
this.trackInputFracDigits(sValue);
this.setValue(dVal, bFromData, bNotify, bDefault);
}
}
catch (NumberFormatException e) {
super.setStrValue(null, true, false);
}
}
}
@Override
public String toString() {
return this.getFormattedValue();
}
private void trackInputFracDigits(String sValue) {
int nFoundAt;
this.mnInputFracDigits = 0;
Int oFracInt = (Int)this.getAttribute(XFA.FRACDIGITSTAG);
if (oFracInt.getValue() == -1 && sValue != null && (nFoundAt = sValue.indexOf(46)) != -1) {
for (int i = nFoundAt + 1; i < sValue.length(); ++i) {
char c = sValue.charAt(i);
if ('0' > c || c > '9') {
this.mnInputFracDigits = -1;
break;
}
++this.mnInputFracDigits;
}
}
}
public boolean valueHasTypeMismatch() {
Int oLeadInt;
int nLeadDigits;
String sValue = this.getFormattedValue();
double dVal = Numeric.stringToDouble(sValue, true);
if (Double.isNaN(dVal) || Double.isInfinite(dVal)) {
return true;
}
Int oFracInt = (Int)this.getAttribute(XFA.FRACDIGITSTAG);
int nFracDigits = oFracInt.getValue();
if (nFracDigits == -1) {
nFracDigits = Integer.MAX_VALUE;
}
if ((nLeadDigits = (oLeadInt = (Int)this.getAttribute(XFA.LEADDIGITSTAG)).getValue()) == -1) {
nLeadDigits = Integer.MAX_VALUE;
}
String sVal = this.getStrValue();
boolean bBeforeDecimal = true;
int length = sVal.length();
int i = 0;
while (i < length) {
int c;
i += (c = sVal.codePointAt(i)) <= 65535 ? 1 : 2;
if (48 <= c && c <= 57) {
if (bBeforeDecimal) {
--nLeadDigits;
} else {
--nFracDigits;
}
if (nLeadDigits < 0) {
return true;
}
if (nFracDigits >= 0) continue;
return true;
}
if (c != 46) continue;
bBeforeDecimal = false;
}
return false;
}
}