FormCalcScriptHandler.java
19.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xfa.scripthandler.formcalc;
import com.adobe.xfa.AppModel;
import com.adobe.xfa.Arg;
import com.adobe.xfa.DependencyTracker;
import com.adobe.xfa.Node;
import com.adobe.xfa.Obj;
import com.adobe.xfa.SOMParser;
import com.adobe.xfa.ScriptDebugger;
import com.adobe.xfa.ScriptHandler;
import com.adobe.xfa.formcalc.CalcException;
import com.adobe.xfa.formcalc.CalcParser;
import com.adobe.xfa.formcalc.CalcSymbol;
import com.adobe.xfa.formcalc.DebugHost;
import com.adobe.xfa.formcalc.Frame;
import com.adobe.xfa.formcalc.FrameTable;
import com.adobe.xfa.formcalc.Instruction;
import com.adobe.xfa.formcalc.ProtocolHost;
import com.adobe.xfa.formcalc.ScopeTable;
import com.adobe.xfa.formcalc.ScriptHost;
import com.adobe.xfa.formcalc.Stack;
import com.adobe.xfa.formcalc.SymbolTable;
import com.adobe.xfa.ut.BooleanHolder;
import com.adobe.xfa.ut.ExFull;
import com.adobe.xfa.ut.IntegerHolder;
import com.adobe.xfa.ut.MsgFormat;
import com.adobe.xfa.ut.MsgFormatPos;
import com.adobe.xfa.ut.ResId;
import com.adobe.xfa.ut.ResourceLoader;
import com.adobe.xfa.ut.StringUtils;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
public class FormCalcScriptHandler
extends ScriptHandler {
private String msReturnMessage;
private FormCalcParser moParser = null;
private final AppModel moAppModel;
private boolean mbFatalError;
public FormCalcScriptHandler(AppModel oAppModel) {
this(oAppModel, null);
}
public FormCalcScriptHandler(AppModel oAppModel, ScriptDebugger oScriptDebugger) {
super(oScriptDebugger);
this.moAppModel = oAppModel;
this.mbFatalError = true;
}
@Override
public void executeOrSyntaxCheck(String script, Arg returnCode, int eReason, boolean bSyntaxCheckOnly, String locale) {
if (this.moParser == null) {
this.moParser = new FormCalcParser(this.moAppModel, this);
}
this.mbFatalError = true;
this.msReturnMessage = "";
int nScriptID = -1;
ScriptDebugger oDebugger = this.getDebugger();
if (oDebugger != null) {
nScriptID = oDebugger.getScriptID((ScriptHandler)this, script, this.moAppModel.getContext(), eReason);
}
this.msReturnMessage = this.moParser.execute(script, locale, nScriptID, returnCode, false);
if (oDebugger != null) {
oDebugger.didExecuteScript(nScriptID, returnCode);
}
}
@Override
public void syntaxCheck(String script) {
if (this.moParser == null) {
this.moParser = new FormCalcParser(this.moAppModel, this);
}
this.mbFatalError = true;
this.msReturnMessage = "";
int nScriptID = -1;
Arg returnCode = null;
this.msReturnMessage = this.moParser.execute(script, "", nScriptID, returnCode, true);
}
@Override
public String languageName() {
return "formcalc";
}
public boolean getCancelOrTimeState() {
return false;
}
@Override
public void setOption(String sOptionName, String sOptionValue) {
if (this.moParser == null) {
this.moParser = new FormCalcParser(this.moAppModel, this);
}
if (sOptionName.equals("DupsMode")) {
this.moParser.setDupsMode((sOptionValue.length() >= 1 ? sOptionValue.charAt(0) : '\u0000') == '1');
}
}
@Override
public String getOption(String sOptionName) {
if (sOptionName.equals("DupsMode") && this.moParser != null) {
return this.moParser.getDupsMode() ? "1" : "0";
}
return "";
}
@Override
public FormCalcScriptHandler clone() {
FormCalcScriptHandler oNewScriptHandler = new FormCalcScriptHandler(this.moAppModel, this.getDebugger());
oNewScriptHandler.moParser = this.moParser;
oNewScriptHandler.msReturnMessage = this.msReturnMessage;
return oNewScriptHandler;
}
@Override
public boolean wasFatalError() {
return this.mbFatalError;
}
void setFatalError(boolean bFatal) {
this.mbFatalError = bFatal;
}
@Override
public boolean debugCommand(int eCmd) {
switch (eCmd) {
case 0: {
this.moParser.command(this.getParser(true), 0);
return true;
}
case 1: {
this.moParser.command(this.getParser(true), 1);
return true;
}
case 2: {
this.moParser.command(this.getParser(true), 2);
return true;
}
case 3: {
this.moParser.command(this.getParser(true), 1);
return true;
}
case 4: {
this.moParser.command(this.getParser(true), 3);
return true;
}
}
return false;
}
@Override
public boolean debugBreakPoint(int nScriptID, int nLine, int eSetType) {
if (eSetType == 0 || eSetType == 1) {
return this.moParser.breakPoint(this.getParser(true), nScriptID, nLine, eSetType == 0);
}
return false;
}
@Override
public boolean debugGetStack(List<String> oStack) {
oStack.clear();
String sStackTrace = this.moParser.getStackTrace(this.getParser(true));
StringTokenizer sToke = new StringTokenizer(sStackTrace, "\n");
while (sToke.hasMoreTokens()) {
String sEntry = sToke.nextToken();
oStack.add(sEntry);
}
return true;
}
@Override
public boolean debugGetVariables(List<String> oVarNames, List<Arg> oVarValues) {
oVarNames.clear();
oVarValues.clear();
ArrayList<CalcSymbol> oCalcSymbols = new ArrayList<CalcSymbol>();
this.moParser.getVariables(this.getParser(true), oVarNames, oCalcSymbols);
for (int i = 0; i < oVarNames.size(); ++i) {
Arg oArg = new Arg();
FormCalcScriptHandler.FCValToXFAArg(oCalcSymbols.get(i), oArg);
oVarValues.add(oArg);
}
return true;
}
@Override
protected int debugFeatures() {
int nFeatures = 30;
return nFeatures;
}
static void FCValToXFAArg(CalcSymbol oFCVal, Arg oArgVal) {
switch (oFCVal.getType()) {
case 3: {
oArgVal.setDouble(new Double(oFCVal.getNumericValue()));
break;
}
case 6: {
oArgVal.setObject(oFCVal.getObjValue());
break;
}
case 2:
case 5: {
oArgVal.setString(oFCVal.getStringValue());
break;
}
case 1: {
oArgVal.setNull();
break;
}
default: {
oArgVal.empty();
}
}
}
static int FCResIdToErrorCode(int nErrorResId) {
if (nErrorResId == ResId.FC_ERR_SYNTAX) {
return 8;
}
if (nErrorResId == ResId.FC_ERR_LOOP) {
return 10;
}
if (nErrorResId == ResId.FC_ERR_FUNC_USED) {
return 100;
}
if (nErrorResId == ResId.FC_ERR_FUNC_UNKN) {
return 101;
}
return 8;
}
protected String getReturnMessage() {
return this.msReturnMessage;
}
protected CalcParser getParser(boolean bForceCreation) {
if (this.moParser == null) {
if (bForceCreation) {
this.moParser = new FormCalcParser(this.moAppModel, this);
} else {
return null;
}
}
return this.moParser;
}
public static class FormCalcParser
extends CalcParser
implements ScriptHost,
ProtocolHost,
DebugHost {
private final CalcParser moParser;
private final AppModel moAppModel;
private final FormCalcScriptHandler moScriptHandler;
public FormCalcParser(AppModel oAppModel, FormCalcScriptHandler oScriptHandler) {
this.moAppModel = oAppModel;
this.moScriptHandler = oScriptHandler;
this.moParser = new CalcParser();
this.moParser.setScriptHost(this);
this.moParser.setProtocolHost(this);
this.moParser.setDebugHost(this);
EnumSet<CalcParser.LegacyVersion> oLegacyScripting = EnumSet.noneOf(CalcParser.LegacyVersion.class);
if (this.moAppModel.getLegacySetting(AppModel.XFA_LEGACY_V30_SCRIPTING)) {
oLegacyScripting.add(CalcParser.LegacyVersion.V30_SCRIPTING);
}
if (this.moAppModel.getLegacySetting(AppModel.XFA_LEGACY_V32_SCRIPTING)) {
oLegacyScripting.add(CalcParser.LegacyVersion.V32_SCRIPTING);
}
if (oLegacyScripting.size() == 0) {
oLegacyScripting.add(CalcParser.LegacyVersion.CUR_SCRIPTING);
}
this.moParser.setLegacyScripting(oLegacyScripting);
}
String execute(String script, String locale, int nScriptID, Arg returnCode, boolean bSyntaxCheckOnly) {
ScriptDebugger oDebugger;
CalcSymbol oResult;
boolean bSuccess;
boolean bSave;
CalcParser oCalcParser = this.moParser;
if (this.moParser.inUse()) {
oCalcParser = (CalcParser)this.moParser.clone();
}
oCalcParser.putScript(script);
oCalcParser.setScriptID(nScriptID);
if (!StringUtils.isEmpty(locale)) {
oCalcParser.setLocale(locale);
}
if ((oDebugger = this.moScriptHandler.getDebugger()) != null) {
oCalcParser.debugEnable(true);
}
if ((bSuccess = oCalcParser.compile(bSave = !bSyntaxCheckOnly, bSave, bSyntaxCheckOnly)) && !bSyntaxCheckOnly) {
if (oDebugger != null) {
oDebugger.willExecuteScript(nScriptID);
}
oCalcParser.evaluate(true, true);
}
if ((oResult = oCalcParser.getCalcResult()) == null) {
return "";
}
String sReturnMessage = null;
if (oResult.getType() == 0) {
String sNoExpr;
IntegerHolder oErrorLine = new IntegerHolder();
IntegerHolder oErrorCode = new IntegerHolder();
String sErr = oResult.getErrorValue(oErrorLine, oErrorCode);
if (sErr.equals(sNoExpr = ResourceLoader.loadResource(ResId.FC_ERR_NO_EXPR))) {
returnCode.empty();
return sReturnMessage;
}
MsgFormat oFmt = new MsgFormat(ResId.ScriptHandlerError, sErr);
if (bSyntaxCheckOnly) {
throw new ScriptHandler.ScriptException(oFmt, oErrorLine.value, FormCalcScriptHandler.FCResIdToErrorCode(oErrorCode.value));
}
throw new ExFull(oFmt);
}
if (oResult.getType() == 10) {
sReturnMessage = oResult.getStringValue();
returnCode.empty();
return sReturnMessage;
}
FormCalcScriptHandler.FCValToXFAArg(oResult, returnCode);
return sReturnMessage;
}
@Override
public Obj getItem(String sItem, Obj[] oObj) {
try {
DependencyTracker oDependencyTracker = this.moAppModel.dependencyTracker();
SOMParser oParser = new SOMParser(oDependencyTracker);
ArrayList<SOMParser.SomResultInfo> oResultList = new ArrayList<SOMParser.SomResultInfo>();
int resultListLength = 0;
if (oParser.resolve(this.moAppModel.getContext(), sItem, oObj, oResultList, null)) {
resultListLength = oResultList.size();
}
if (resultListLength == 0) {
this.moScriptHandler.setFatalError(false);
MsgFormatPos sFmt = new MsgFormatPos(ResId.FC_ERR_ACCESSOR);
sFmt.format(sItem);
CalcSymbol calcsymbol = new CalcSymbol(sFmt.toString(), true, 0, 0);
throw new CalcException(calcsymbol);
}
Arg arg = oResultList.get((int)0).value;
return arg.getObject();
}
catch (ExFull oError) {
CalcSymbol calcsymbol = new CalcSymbol(oError.toString(), true, 0, 0);
throw new CalcException(calcsymbol);
}
}
@Override
public CalcSymbol[] getItemValue(String sItem, Obj[] oObj) {
try {
DependencyTracker oDependencyTracker = this.moAppModel.dependencyTracker();
SOMParser oParser = new SOMParser(oDependencyTracker);
ArrayList<SOMParser.SomResultInfo> oResultList = new ArrayList<SOMParser.SomResultInfo>();
int resultListLength = 0;
if (oParser.resolve(this.moAppModel.getContext(), sItem, oObj, oResultList, null)) {
resultListLength = oResultList.size();
}
if (resultListLength == 0) {
this.moScriptHandler.setFatalError(false);
MsgFormatPos sFmt = new MsgFormatPos(ResId.FC_ERR_ACCESSOR);
sFmt.format(sItem);
CalcSymbol calcsymbol = new CalcSymbol(sFmt.toString(), true, 0, 0);
throw new CalcException(calcsymbol);
}
CalcSymbol[] oSym = new CalcSymbol[resultListLength];
for (int i = 0; i < resultListLength; ++i) {
ScriptDebugger oDebugger;
Obj obj = oResultList.get((int)i).object;
Arg arg = oResultList.get((int)i).value;
boolean bNullValue = false;
if (arg.getArgType() == 7) {
Obj o = arg.getObject();
boolean bl = bNullValue = !o.getScriptProperty(arg, "", oDependencyTracker, true, true);
}
if ((oDebugger = this.moScriptHandler.getDebugger()) != null) {
oDebugger.resolvedValue(sItem, arg);
}
if (arg.getArgType() == 8) {
throw arg.getException();
}
oSym[i] = bNullValue ? new CalcSymbol() : (arg.getArgType() == 6 ? new CalcSymbol(arg.getString()) : (arg.getArgType() == 5 ? new CalcSymbol(arg.getDouble(false)) : (arg.getArgType() == 4 ? new CalcSymbol(arg.getInteger().doubleValue()) : (arg.getArgType() == 3 ? new CalcSymbol(arg.getBool() != false ? 1.0 : 0.0) : (arg.getArgType() == 7 ? new CalcSymbol(obj, null) : new CalcSymbol())))));
}
return oSym;
}
catch (ExFull oError) {
CalcSymbol calcsymbol = new CalcSymbol(oError.toString(), true, 0, 0);
throw new CalcException(calcsymbol);
}
}
@Override
public int putItemValue(String sItem, Obj[] oObj, CalcSymbol oValue) {
try {
Node oNode;
String sValue = null;
if (oValue.getType() != 1) {
sValue = oValue.getStringValue();
}
if (!(oNode = this.moAppModel.getContext()).performSOMAssignment(sItem, sValue, oObj)) {
this.moScriptHandler.setFatalError(false);
MsgFormatPos sFmt = new MsgFormatPos(ResId.FC_ERR_ACCESSOR);
sFmt.format(sItem);
CalcSymbol calcsymbol = new CalcSymbol(sFmt.toString(), true, 0, 0);
throw new CalcException(calcsymbol);
}
return 0;
}
catch (ExFull oError) {
CalcSymbol calcsymbol = new CalcSymbol(oError.toString(), true, 0, 0);
throw new CalcException(calcsymbol);
}
}
@Override
public int putItem(Obj[] oObj, CalcSymbol oValue) {
return 0;
}
@Override
public CalcSymbol getUrl(String sUrl) throws CalcException {
MsgFormatPos sFmt = new MsgFormatPos(ResId.PROTOCOL_ERR_SYS);
sFmt.format(sUrl);
CalcSymbol oErr = new CalcSymbol(sFmt.toString(), true, 0, 0);
throw new CalcException(oErr);
}
@Override
public CalcSymbol putUrl(String sUrl, String sData, String sEnc) throws CalcException {
MsgFormatPos sFmt = new MsgFormatPos(ResId.PROTOCOL_ERR_SYS);
sFmt.format(sUrl);
CalcSymbol oErr = new CalcSymbol(sFmt.toString(), true, 0, 0);
throw new CalcException(oErr);
}
@Override
public CalcSymbol postUrl(String sUrl, String sSoapHeader, String sData, String sContentType, String sEnc) throws CalcException {
MsgFormatPos sFmt = new MsgFormatPos(ResId.PROTOCOL_ERR_SYS);
sFmt.format(sUrl);
CalcSymbol oErr = new CalcSymbol(sFmt.toString(), true, 0, 0);
throw new CalcException(oErr);
}
@Override
public int stopped(int nScriptID, int nLine) {
ScriptDebugger oDebugger = this.moScriptHandler.getDebugger();
if (oDebugger != null) {
oDebugger.stopped(nScriptID, nLine);
}
return -1;
}
@Override
public void poll() {
ScriptDebugger oDebugger = this.moScriptHandler.getDebugger();
if (oDebugger != null) {
oDebugger.poll(this.moScriptHandler);
}
}
@Override
public boolean breakPoint(CalcParser oParser, int nScriptID, int nLine, boolean bSet) {
return oParser.moCode.debugBreakPoint(nScriptID, nLine, bSet);
}
@Override
public boolean command(CalcParser oParser, int eCmd) {
return oParser.moCode.debugCommand(oParser, eCmd);
}
@Override
public String getStackTrace(CalcParser oParser) {
return oParser.moFrame.getStackTrace(oParser.mStack);
}
@Override
public void getVariables(CalcParser oParser, List<String> oNames, List<CalcSymbol> oValues) {
ArrayList<CalcSymbol> oSymbols = new ArrayList<CalcSymbol>();
oParser.moData.enumerate(oParser.moScope, oSymbols);
Frame oTopFrame = null;
CalcSymbol oFuncSym = null;
String sFuncPrefix = "";
FrameTable oFrameTable = oParser.moFrame;
if (oFrameTable.getDepth() > 0) {
oTopFrame = oFrameTable.peek();
oFuncSym = oTopFrame.getFuncSym();
sFuncPrefix = oFuncSym.getName();
sFuncPrefix = sFuncPrefix + '`';
}
Iterator<CalcSymbol> i$ = oSymbols.iterator();
while (i$.hasNext()) {
CalcSymbol oSym = i$.next();
String sName = oSym.getName();
if (oSym.getType() == 7) {
if (oFuncSym == null || !sFuncPrefix.startsWith(sName)) continue;
sName = sName.substring(sFuncPrefix.length());
int nStackAddr = oTopFrame.getStackAddr();
int nArgCount = oTopFrame.getArgCount();
int nStackIdx = oSym.getIdxValue();
oSym = oParser.mStack.peek(nStackAddr - nArgCount + nStackIdx);
}
oNames.add(sName);
oValues.add(oSym);
}
}
@Override
public boolean cancelActionOccured() {
return this.moScriptHandler.getCancelOrTimeState();
}
}
}