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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
|
/*
* Copyright (c) 2003, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.rowset.internal;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.sql.*;
import javax.sql.*;
import javax.sql.rowset.*;
import com.sun.rowset.*;
import java.io.IOException;
import java.text.MessageFormat;
/**
* The document handler that receives parse events that an XML parser sends while it
* is parsing an XML document representing a <code>WebRowSet</code> object. The
* parser sends strings to this <code>XmlReaderContentHandler</code> and then uses
* these strings as arguments for the <code>XmlReaderContentHandler</code> methods
* it invokes. The final goal of the SAX parser working with an
* <code>XmlReaderContentHandler</code> object is to read an XML document that represents
* a <code>RowSet</code> object.
* <P>
* A rowset consists of its properties, metadata, and data values. An XML document
* representating a rowset includes the values in these three categories along with
* appropriate XML tags to identify them. It also includes a top-level XML tag for
* the rowset and three section tags identifying the three categories of values.
* <P>
* The tags in an XML document are hierarchical.
* This means that the top-level tag, <code>RowSet</code>, is
* followed by the three sections with appropriate tags, which are in turn each
* followed by their constituent elements. For example, the <code>properties</code>
* element will be followed by an element for each of the properties listed in
* in this <code>XmlReaderContentHandler</code> object's <code>properties</code>
* field. The content of the other two fields, <code>colDef</code>, which lists
* the rowset's metadata elements, and <code>data</code>, which lists the rowset's data
* elements, are handled similarly .
* <P>
* This implementation of <code>XmlReaderContentHandler</code> provides the means for the
* parser to determine which elements need to have a value set and then to set
* those values. The methods in this class are all called by the parser; an
* application programmer never calls them directly.
*
*/
public class XmlReaderContentHandler extends DefaultHandler {
private HashMap <String, Integer> propMap;
private HashMap <String, Integer> colDefMap;
private HashMap <String, Integer> dataMap;
private HashMap<String,Class<?>> typeMap;
private Vector<Object[]> updates;
private Vector<String> keyCols;
private String columnValue;
private String propertyValue;
private String metaDataValue;
private int tag = -1;
private int state;
private WebRowSetImpl rs;
private boolean nullVal;
private boolean emptyStringVal;
private RowSetMetaData md;
private int idx;
private String lastval;
private String Key_map;
private String Value_map;
private String tempStr;
private String tempUpdate;
private String tempCommand;
private Object [] upd;
/**
* A list of the properties for a rowset. There is a constant defined to
* correspond to each of these properties so that a <code>HashMap</code>
* object can be created to map the properties, which are strings, to
* the constants, which are integers.
*/
private String [] properties = {"command", "concurrency", "datasource",
"escape-processing", "fetch-direction", "fetch-size",
"isolation-level", "key-columns", "map",
"max-field-size", "max-rows", "query-timeout",
"read-only", "rowset-type", "show-deleted",
"table-name", "url", "null", "column", "type",
"class", "sync-provider", "sync-provider-name",
"sync-provider-vendor", "sync-provider-version",
"sync-provider-grade","data-source-lock"};
/**
* A constant representing the tag for the command property.
*/
private static final int CommandTag = 0;
/**
* A constant representing the tag for the concurrency property.
*/
private static final int ConcurrencyTag = 1;
/**
* A constant representing the tag for the datasource property.
*/
private static final int DatasourceTag = 2;
/**
* A constant representing the tag for the escape-processing property.
*/
private static final int EscapeProcessingTag = 3;
/**
* A constant representing the tag for the fetch-direction property.
*/
private static final int FetchDirectionTag = 4;
/**
* A constant representing the tag for the fetch-size property.
*/
private static final int FetchSizeTag = 5;
/**
* A constant representing the tag for the isolation-level property
*/
private static final int IsolationLevelTag = 6;
/**
* A constant representing the tag for the key-columns property.
*/
private static final int KeycolsTag = 7;
/**
* A constant representing the tag for the map property.
* This map is the type map that specifies the custom mapping
* for an SQL user-defined type.
*/
private static final int MapTag = 8;
/**
* A constant representing the tag for the max-field-size property.
*/
private static final int MaxFieldSizeTag = 9;
/**
* A constant representing the tag for the max-rows property.
*/
private static final int MaxRowsTag = 10;
/**
* A constant representing the tag for the query-timeout property.
*/
private static final int QueryTimeoutTag = 11;
/**
* A constant representing the tag for the read-only property.
*/
private static final int ReadOnlyTag = 12;
/**
* A constant representing the tag for the rowset-type property.
*/
private static final int RowsetTypeTag = 13;
/**
* A constant representing the tag for the show-deleted property.
*/
private static final int ShowDeletedTag = 14;
/**
* A constant representing the tag for the table-name property.
*/
private static final int TableNameTag = 15;
/**
* A constant representing the tag for the URL property.
*/
private static final int UrlTag = 16;
/**
* A constant representing the tag for the null property.
*/
private static final int PropNullTag = 17;
/**
* A constant representing the tag for the column property.
*/
private static final int PropColumnTag = 18;
/**
* A constant representing the tag for the type property.
*/
private static final int PropTypeTag = 19;
/**
* A constant representing the tag for the class property.
*/
private static final int PropClassTag = 20;
/**
* A constant representing the tag for the sync-provider.
*/
private static final int SyncProviderTag = 21;
/**
* A constant representing the tag for the sync-provider
* name
*/
private static final int SyncProviderNameTag = 22;
/**
* A constant representing the tag for the sync-provider
* vendor tag.
*/
private static final int SyncProviderVendorTag = 23;
/**
* A constant representing the tag for the sync-provider
* version tag.
*/
private static final int SyncProviderVersionTag = 24;
/**
* A constant representing the tag for the sync-provider
* grade tag.
*/
private static final int SyncProviderGradeTag = 25;
/**
* A constant representing the tag for the data source lock.
*/
private static final int DataSourceLock = 26;
/**
* A listing of the kinds of metadata information available about
* the columns in a <code>WebRowSet</code> object.
*/
private String [] colDef = {"column-count", "column-definition", "column-index",
"auto-increment", "case-sensitive", "currency",
"nullable", "signed", "searchable",
"column-display-size", "column-label", "column-name",
"schema-name", "column-precision", "column-scale",
"table-name", "catalog-name", "column-type",
"column-type-name", "null"};
/**
* A constant representing the tag for column-count.
*/
private static final int ColumnCountTag = 0;
/**
* A constant representing the tag for column-definition.
*/
private static final int ColumnDefinitionTag = 1;
/**
* A constant representing the tag for column-index.
*/
private static final int ColumnIndexTag = 2;
/**
* A constant representing the tag for auto-increment.
*/
private static final int AutoIncrementTag = 3;
/**
* A constant representing the tag for case-sensitive.
*/
private static final int CaseSensitiveTag = 4;
/**
* A constant representing the tag for currency.
*/
private static final int CurrencyTag = 5;
/**
* A constant representing the tag for nullable.
*/
private static final int NullableTag = 6;
/**
* A constant representing the tag for signed.
*/
private static final int SignedTag = 7;
/**
* A constant representing the tag for searchable.
*/
private static final int SearchableTag = 8;
/**
* A constant representing the tag for column-display-size.
*/
private static final int ColumnDisplaySizeTag = 9;
/**
* A constant representing the tag for column-label.
*/
private static final int ColumnLabelTag = 10;
/**
* A constant representing the tag for column-name.
*/
private static final int ColumnNameTag = 11;
/**
* A constant representing the tag for schema-name.
*/
private static final int SchemaNameTag = 12;
/**
* A constant representing the tag for column-precision.
*/
private static final int ColumnPrecisionTag = 13;
/**
* A constant representing the tag for column-scale.
*/
private static final int ColumnScaleTag = 14;
/**
* A constant representing the tag for table-name.
*/
private static final int MetaTableNameTag = 15;
/**
* A constant representing the tag for catalog-name.
*/
private static final int CatalogNameTag = 16;
/**
* A constant representing the tag for column-type.
*/
private static final int ColumnTypeTag = 17;
/**
* A constant representing the tag for column-type-name.
*/
private static final int ColumnTypeNameTag = 18;
/**
* A constant representing the tag for null.
*/
private static final int MetaNullTag = 19;
private String [] data = {"currentRow", "columnValue", "insertRow", "deleteRow",
"modifyRow", "updateValue", "null" , "emptyString"};
private static final int RowTag = 0;
private static final int ColTag = 1;
private static final int InsTag = 2;
private static final int DelTag = 3;
private static final int ModTag = 4;
private static final int UpdTag = 5;
private static final int NullTag = 6;
private static final int EmptyStringTag = 7;
private static final int InsDelTag = 8;
private static final int UpdRowTag = 9;
private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
// Error messages
private static final String SCHEMA_LOC_MESSAGE = "readXML : Malformed xsi:schemaLocation: %s.";
private static final String SCHEMA_MESSAGE = "readXML : Non-standard schema specified for namespace %s : %s.";
/**
* A constant indicating the state of this <code>XmlReaderContentHandler</code>
* object in which it has not yet been called by the SAX parser and therefore
* has no indication of what type of input to expect from the parser next.
* <P>
* The state is set to <code>INITIAL</code> at the end of each
* section, which allows the sections to appear in any order and
* still be parsed correctly (except that metadata must be
* set before data values can be set).
*/
private static final int INITIAL = 0;
/**
* A constant indicating the state in which this <code>XmlReaderContentHandler</code>
* object expects the next input received from the
* SAX parser to be a string corresponding to one of the elements in
* <code>properties</code>.
*/
private static final int PROPERTIES = 1;
/**
* A constant indicating the state in which this <code>XmlReaderContentHandler</code>
* object expects the next input received from the
* SAX parser to be a string corresponding to one of the elements in
* <code>colDef</code>.
*/
private static final int METADATA = 2;
/**
* A constant indicating the state in which this <code>XmlReaderContentHandler</code>
* object expects the next input received from the
* SAX parser to be a string corresponding to one of the elements in
* <code>data</code>.
*/
private static final int DATA = 3;
private JdbcRowSetResourceBundle resBundle;
private boolean sawRoot;
private final boolean validating;
/**
* Constructs a new <code>XmlReaderContentHandler</code> object that will
* assist the SAX parser in reading a <code>WebRowSet</code> object in the
* format of an XML document. In addition to setting some default values,
* this constructor creates three <code>HashMap</code> objects, one for
* properties, one for metadata, and one for data. These hash maps map the
* strings sent by the SAX parser to integer constants so that they can be
* compared more efficiently in <code>switch</code> statements.
*
* @param r the <code>RowSet</code> object in XML format that will be read
*/
public XmlReaderContentHandler(RowSet r) {
this(r, true);
}
/**
* Constructs a new {@code XmlReaderContentHandler} with a validation flag.
* When validation is disabled, the handler accepts legacy WebRowSet XML tags
* in addition to the standard tags.
*
* @param r the {@code RowSet} object in XML format that will be read
* @param validating the flag indicating whether schema validation is on
*/
XmlReaderContentHandler(RowSet r, boolean validating) {
this.validating = validating;
// keep the rowset we've been given
rs = (WebRowSetImpl)r;
// set-up the token maps
initMaps();
// allocate the collection for the updates
updates = new Vector<>();
// start out with the empty string
columnValue = "";
propertyValue = "";
metaDataValue = "";
nullVal = false;
idx = 0;
tempStr = "";
tempUpdate = "";
tempCommand = "";
try {
resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();
} catch(IOException ioe) {
throw new RuntimeException(ioe);
}
}
/**
* Creates and initializes three new <code>HashMap</code> objects that map
* the strings returned by the SAX parser to <code>Integer</code>
* objects. The strings returned by the parser will match the strings that
* are array elements in this <code>XmlReaderContentHandler</code> object's
* <code>properties</code>, <code>colDef</code>, or <code>data</code>
* fields. For each array element in these fields, there is a corresponding
* constant defined. It is to these constants that the strings are mapped.
* In the <code>HashMap</code> objects, the string is the key, and the
* integer is the value.
* <P>
* The purpose of the mapping is to make comparisons faster. Because comparing
* numbers is more efficient than comparing strings, the strings returned
* by the parser are mapped to integers, which can then be used in a
* <code>switch</code> statement.
*/
private void initMaps() {
int items, i;
propMap = new HashMap<>();
items = properties.length;
for (i=0;i<items;i++) {
propMap.put(properties[i], i);
}
colDefMap = new HashMap<>();
items = colDef.length;
for (i=0;i<items;i++) {
colDefMap.put(colDef[i], i);
}
dataMap = new HashMap<>();
items = data.length;
for (i=0;i<items;i++) {
dataMap.put(data[i], i);
}
if (!validating) {
// Accept legacy tags when schema validation is disabled.
dataMap.put("insdel", InsDelTag);
dataMap.put("updateRow", UpdRowTag);
}
//Initialize connection map here
typeMap = new HashMap<>();
}
public void startDocument() throws SAXException {
}
public void endDocument() throws SAXException {
}
/**
* Sets this <code>XmlReaderContentHandler</code> object's <code>tag</code>
* field if the given name is the key for a tag and this object's state
* is not <code>INITIAL</code>. The field is set
* to the constant that corresponds to the given element name.
* If the state is <code>INITIAL</code>, the state is set to the given
* name, which will be one of the sections <code>PROPERTIES</code>,
* <code>METADATA</code>, or <code>DATA</code>. In either case, this
* method puts this document handler in the proper state for calling
* the method <code>endElement</code>.
* <P>
* If the state is <code>DATA</code> and the tag is <code>RowTag</code>,
* <code>DelTag</code>, or <code>InsTag</code>, this method moves the
* rowset's cursor to the insert row and sets this
* <code>XmlReaderContentHandler</code> object's <code>idx</code>
* field to <code>0</code> so that it will be in the proper
* state when the parser calls the method <code>endElement</code>.
*
* @param lName the name of the element; either (1) one of the array
* elements in the fields <code>properties</code>,
* <code>colDef</code>, or <code>data</code> or
* (2) one of the <code>RowSet</code> elements
* <code>"properties"</code>, <code>"metadata"</code>, or
* <code>"data"</code>
* @param attributes <code>org.xml.sax.AttributeList</code> objects that are
* attributes of the named section element; may be <code>null</code>
* if there are no attributes, which is the case for
* <code>WebRowSet</code> objects
* @exception SAXException if a general SAX error occurs
*/
public void startElement(String uri, String lName, String qName, Attributes attributes) throws SAXException {
if (validating && !sawRoot) {
sawRoot = true;
checkSchemaLocation(attributes);
}
int tag;
String name = "";
name = lName;
switch (getState()) {
case PROPERTIES:
tempCommand = "";
tag = propMap.get(name);
if (tag == PropNullTag)
setNullValue(true);
else
setTag(tag);
break;
case METADATA:
tag = colDefMap.get(name);
if (tag == MetaNullTag)
setNullValue(true);
else
setTag(tag);
break;
case DATA:
/**
* This has been added to clear out the values of the previous read
* so that we should not add up values of data between different tags
*/
tempStr = "";
tempUpdate = "";
if(dataMap.get(name) == null) {
tag = NullTag;
} else if(dataMap.get(name) == EmptyStringTag) {
tag = EmptyStringTag;
} else {
tag = dataMap.get(name);
}
if (tag == NullTag) {
setNullValue(true);
} else if(tag == EmptyStringTag) {
setEmptyStringValue(true);
} else {
setTag(tag);
if (tag == RowTag || tag == DelTag || tag == InsTag ||
tag == ModTag || tag == InsDelTag) {
idx = 0;
try {
rs.moveToInsertRow();
} catch (SQLException ex) {
;
}
}
}
break;
default:
setState(name);
}
}
/**
* Verifies that the optional xsi:schemaLocation attribute, if present,
* references the standard WebRowSet XML schema.
*
* <p>The XML parser is configured with the standard WebRowSet schema
* explicitly through the JAXP Validation API, so schema validation does
* not depend on xsi:schemaLocation being present in the XML instance
* document. This method therefore performs only an additional consistency
* check on the schema hint provided by the document.</p>
*
* @param attributes the attributes of the root webRowSet element
* @throws SAXException if a non-standard schema location is specified
*/
private void checkSchemaLocation(Attributes attributes) throws SAXException {
String schemaLocation = attributes.getValue(XSI_NS, "schemaLocation");
// schemaLocation is optional when schema is supplied explicitly
if (schemaLocation == null || schemaLocation.isBlank()) {
return;
}
String[] tokens = schemaLocation.trim().split("\\s+");
if (tokens.length % 2 != 0) {
throw new SAXException(SCHEMA_LOC_MESSAGE.formatted(schemaLocation));
}
for (int i = 0; i < tokens.length; i += 2) {
String ns = tokens[i];
String xsd = tokens[i + 1];
if (!isWebRowSetSchema(xsd)) {
throw new SAXException(SCHEMA_MESSAGE.formatted(ns, xsd));
}
}
}
private static boolean isWebRowSetSchema(String xsd) {
return xsd.equals(WebRowSetXmlReader.WEBROWSET_XSD)
|| xsd.endsWith("/" + WebRowSetXmlReader.WEBROWSET_XSD);
}
/**
* Sets the value for the given element if <code>name</code> is one of
* the array elements in the fields <code>properties</code>,
* <code>colDef</code>, or <code>data</code> and this
* <code>XmlReaderContentHandler</code> object's state is not
* <code>INITIAL</code>. If the state is <code>INITIAL</code>,
* this method does nothing.
* <P>
* If the state is <code>METADATA</code> and
* the argument supplied is <code>"metadata"</code>, the rowset's
* metadata is set. If the state is <code>PROPERTIES</code>, the
* appropriate property is set using the given name to determine
* the appropriate value. If the state is <code>DATA</code> and
* the argument supplied is <code>"data"</code>, this method sets
* the state to <code>INITIAL</code> and returns. If the argument
* supplied is one of the elements in the field <code>data</code>,
* this method makes the appropriate changes to the rowset's data.
*
* @param lName the name of the element; either (1) one of the array
* elements in the fields <code>properties</code>,
* <code>colDef</code>, or <code>data</code> or
* (2) one of the <code>RowSet</code> elements
* <code>"properties"</code>, <code>"metadata"</code>, or
* <code>"data"</code>
*
* @exception SAXException if a general SAX error occurs
*/
@SuppressWarnings("fallthrough")
public void endElement(String uri, String lName, String qName) throws SAXException {
int tag;
String name = "";
name = lName;
switch (getState()) {
case PROPERTIES:
if (name.equals("properties")) {
state = INITIAL;
break;
}
try {
tag = propMap.get(name);
switch (tag) {
case KeycolsTag:
if (keyCols != null) {
int i[] = new int[keyCols.size()];
for (int j = 0; j < i.length; j++)
i[j] = Integer.parseInt(keyCols.elementAt(j));
rs.setKeyColumns(i);
}
break;
case PropClassTag:
//Added the handling for Class tags to take care of maps
//Makes an entry into the map upon end of class tag
try{
typeMap.put(Key_map,sun.reflect.misc.ReflectUtil.forName(Value_map));
}catch(ClassNotFoundException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errmap").toString(), ex.getMessage()));
}
break;
case MapTag:
//Added the handling for Map to take set the typeMap
rs.setTypeMap(typeMap);
break;
default:
break;
}
if (getNullValue()) {
setPropertyValue(null);
setNullValue(false);
} else {
setPropertyValue(propertyValue);
}
} catch (SQLException ex) {
throw new SAXException(ex.getMessage());
}
// propertyValue need to be reset to an empty string
propertyValue = "";
setTag(-1);
break;
case METADATA:
if (name.equals("metadata")) {
try {
rs.setMetaData(md);
state = INITIAL;
} catch (SQLException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errmetadata").toString(), ex.getMessage()));
}
} else {
try {
if (getNullValue()) {
setMetaDataValue(null);
setNullValue(false);
} else {
setMetaDataValue(metaDataValue);
}
} catch (SQLException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errmetadata").toString(), ex.getMessage()));
}
// metaDataValue needs to be reset to an empty string
metaDataValue = "";
}
setTag(-1);
break;
case DATA:
if (name.equals("data")) {
state = INITIAL;
return;
}
if(dataMap.get(name) == null) {
tag = NullTag;
} else {
tag = dataMap.get(name);
}
switch (tag) {
case ColTag:
try {
idx++;
if (getNullValue()) {
insertValue(null);
setNullValue(false);
} else {
insertValue(tempStr);
}
// columnValue now need to be reset to the empty string
columnValue = "";
} catch (SQLException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errinsertval").toString(), ex.getMessage()));
}
break;
case RowTag:
case ModTag:
case InsDelTag:
try {
rs.insertRow();
rs.moveToCurrentRow();
rs.next();
// Making this as the original to turn off the
// rowInserted flagging
rs.setOriginalRow();
// Legacy currentRow elements can carry update tags when
// schema validation is disabled.
applyUpdates();
} catch (SQLException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errconstr").toString(), ex.getMessage()));
}
break;
case DelTag:
try {
rs.insertRow();
rs.moveToCurrentRow();
rs.next();
rs.setOriginalRow();
// Legacy deleteRow elements may omit update tags when
// schema validation is disabled.
applyUpdates();
rs.deleteRow();
} catch (SQLException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errdel").toString() , ex.getMessage()));
}
break;
case InsTag:
try {
rs.insertRow();
rs.moveToCurrentRow();
rs.next();
applyUpdates();
} catch (SQLException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errinsert").toString() , ex.getMessage()));
}
break;
case UpdTag:
case UpdRowTag:
// Empty updateValue/updateRow placeholders are ignored when
// they carry no value.
if(getNullValue())
{
addUpdate(null);
setNullValue(false);
} else if(getEmptyStringValue()) {
addUpdate("");
setEmptyStringValue(false);
} else if(!tempUpdate.isEmpty()) {
addUpdate(tempUpdate);
}
break;
default:
break;
}
default:
break;
}
}
private void applyUpdates() throws SAXException {
// now handle any updates
if (updates.size() > 0) {
try {
Object upd[];
Iterator<?> i = updates.iterator();
while (i.hasNext()) {
upd = (Object [])i.next();
idx = ((Integer)upd[0]).intValue();
insertValue((String)(upd[1]));
}
rs.updateRow();
} catch (SQLException ex) {
throw new SAXException(MessageFormat.format(resBundle.handleGetObject("xmlrch.errupdrow").toString() , ex.getMessage()));
}
updates.removeAllElements();
}
}
/**
* Queues an update value for the current column to be applied when the
* row element is complete.
*
* @param value the update value, can be {@code null}
*/
private void addUpdate(String value) {
upd = new Object[2];
upd[0] = Integer.valueOf(idx);
upd[1] = value;
updates.add(upd);
}
/**
* Sets a property, metadata, or data value with the characters in
* the given array of characters, starting with the array element
* indicated by <code>start</code> and continuing for <code>length</code>
* number of characters.
* <P>
* The SAX parser invokes this method and supplies
* the character array, start position, and length parameter values it
* got from parsing the XML document. An application programmer never
* invokes this method directly.
*
* @param ch an array of characters supplied by the SAX parser, all or part of
* which will be used to set a value
* @param start the position in the given array at which to start
* @param length the number of consecutive characters to use
*/
public void characters(char[] ch, int start, int length) throws SAXException {
try {
switch (getState()) {
case PROPERTIES:
// If tag is -1, startElement has not registered a valid tag
// Thus, we are in whitespace between elements, and should exit
if (tag == -1) {
break;
}
propertyValue = new String(ch, start, length);
/**
* This has been added for handling of special characters. When special
* characters are encountered the characters function gets called for
* each of the characters so we need to append the value got in the
* previous call as it is the same data present between the start and
* the end tag.
**/
tempCommand = tempCommand.concat(propertyValue);
propertyValue = tempCommand;
// Added the following check for handling of type tags in maps
if(tag == PropTypeTag)
{
Key_map = propertyValue;
}
// Added the following check for handling of class tags in maps
else if(tag == PropClassTag)
{
Value_map = propertyValue;
}
break;
case METADATA:
// The parser will come here after the endElement as there is
// "\n" in the after endTag is printed. This will cause a problem
// when the data between the tags is an empty string so adding
// below condition to take care of that situation.
if (tag == -1)
{
break;
}
metaDataValue = new String(ch, start, length);
break;
case DATA:
setDataValue(ch, start, length);
break;
default:
;
}
} catch (SQLException ex) {
throw new SAXException(resBundle.handleGetObject("xmlrch.chars").toString() + ex.getMessage());
}
}
private void setState(String s) throws SAXException {
if (s.equals("webRowSet")) {
state = INITIAL;
} else if (s.equals("properties")) {
if (state != PROPERTIES)
state = PROPERTIES;
else
state = INITIAL;
} else if (s.equals("metadata")) {
if (state != METADATA)
state = METADATA;
else
state = INITIAL;
} else if (s.equals("data")) {
if (state != DATA)
state = DATA;
else
state = INITIAL;
}
}
/**
* Retrieves the current state of this <code>XmlReaderContentHandler</code>
* object's rowset, which is stored in the document handler's
* <code>state</code> field.
*
* @return one of the following constants:
* <code>XmlReaderContentHandler.PROPERTIES</code>
* <code>XmlReaderContentHandler.METADATA</code>
* <code>XmlReaderContentHandler.DATA</code>
* <code>XmlReaderContentHandler.INITIAL</code>
*/
private int getState() {
return state;
}
private void setTag(int t) {
tag = t;
}
private int getTag() {
return tag;
}
private void setNullValue(boolean n) {
nullVal = n;
}
private boolean getNullValue() {
return nullVal;
}
private void setEmptyStringValue(boolean e) {
emptyStringVal = e;
}
private boolean getEmptyStringValue() {
return emptyStringVal;
}
private String getStringValue(String s) {
return s;
}
private int getIntegerValue(String s) {
return Integer.parseInt(s);
}
private boolean getBooleanValue(String s) {
return Boolean.valueOf(s).booleanValue();
}
private java.math.BigDecimal getBigDecimalValue(String s) {
return new java.math.BigDecimal(s);
}
private byte getByteValue(String s) {
return Byte.parseByte(s);
}
private short getShortValue(String s) {
return Short.parseShort(s);
}
private long getLongValue(String s) {
return Long.parseLong(s);
}
private float getFloatValue(String s) {
return Float.parseFloat(s);
}
private double getDoubleValue(String s) {
return Double.parseDouble(s);
}
private byte[] getBinaryValue(String s) {
return s.getBytes();
}
private java.sql.Date getDateValue(String s) {
return new java.sql.Date(getLongValue(s));
}
private java.sql.Time getTimeValue(String s) {
return new java.sql.Time(getLongValue(s));
}
private java.sql.Timestamp getTimestampValue(String s) {
return new java.sql.Timestamp(getLongValue(s));
}
private void setPropertyValue(String s) throws SQLException {
// find out if we are going to be dealing with a null
boolean nullValue = getNullValue();
// Empty nullable properties preserve the legacy <null/> behavior while
// remaining schema-valid.
switch(getTag()) {
case CommandTag:
if (nullValue || s == null || s.isEmpty())
; //rs.setCommand(null);
else
rs.setCommand(s);
break;
case ConcurrencyTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setConcurrency(getIntegerValue(s));
break;
case DatasourceTag:
if (nullValue || s == null || s.isEmpty())
rs.setDataSourceName(null);
else
rs.setDataSourceName(s);
break;
case EscapeProcessingTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setEscapeProcessing(getBooleanValue(s));
break;
case FetchDirectionTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setFetchDirection(getIntegerValue(s));
break;
case FetchSizeTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setFetchSize(getIntegerValue(s));
break;
case IsolationLevelTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setTransactionIsolation(getIntegerValue(s));
break;
case KeycolsTag:
break;
case PropColumnTag:
if (keyCols == null)
keyCols = new Vector<>();
keyCols.add(s);
break;
case MapTag:
break;
case MaxFieldSizeTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setMaxFieldSize(getIntegerValue(s));
break;
case MaxRowsTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setMaxRows(getIntegerValue(s));
break;
case QueryTimeoutTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setQueryTimeout(getIntegerValue(s));
break;
case ReadOnlyTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setReadOnly(getBooleanValue(s));
break;
case RowsetTypeTag:
if (nullValue) {
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
} else {
//rs.setType(getIntegerValue(s));
String strType = getStringValue(s);
int iType = 0;
if(strType.trim().equals("ResultSet.TYPE_SCROLL_INSENSITIVE")) {
iType = 1004;
} else if(strType.trim().equals("ResultSet.TYPE_SCROLL_SENSITIVE")) {
iType = 1005;
} else if(strType.trim().equals("ResultSet.TYPE_FORWARD_ONLY")) {
iType = 1003;
}
rs.setType(iType);
}
break;
case ShowDeletedTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue").toString());
else
rs.setShowDeleted(getBooleanValue(s));
break;
case TableNameTag:
if (nullValue || s == null || s.isEmpty())
//rs.setTableName(null);
;
else
rs.setTableName(s);
break;
case UrlTag:
if (nullValue || s == null || s.isEmpty())
rs.setUrl(null);
else
rs.setUrl(s);
break;
case SyncProviderNameTag:
if (nullValue) {
rs.setSyncProvider(null);
} else {
String str = s.substring(0,s.indexOf('@')+1);
rs.setSyncProvider(str);
}
break;
case SyncProviderVendorTag:
// to be implemented
break;
case SyncProviderVersionTag:
// to be implemented
break;
case SyncProviderGradeTag:
// to be implemented
break;
case DataSourceLock:
// to be implemented
break;
default:
break;
}
}
private void setMetaDataValue(String s) throws SQLException {
// find out if we are going to be dealing with a null
boolean nullValue = getNullValue();
switch (getTag()) {
case ColumnCountTag:
md = new RowSetMetaDataImpl();
idx = 0;
if (nullValue) {
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
} else {
md.setColumnCount(getIntegerValue(s));
}
break;
case ColumnDefinitionTag:
break;
case ColumnIndexTag:
idx++;
break;
case AutoIncrementTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setAutoIncrement(idx, getBooleanValue(s));
break;
case CaseSensitiveTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setCaseSensitive(idx, getBooleanValue(s));
break;
case CurrencyTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setCurrency(idx, getBooleanValue(s));
break;
case NullableTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setNullable(idx, getIntegerValue(s));
break;
case SignedTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setSigned(idx, getBooleanValue(s));
break;
case SearchableTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setSearchable(idx, getBooleanValue(s));
break;
case ColumnDisplaySizeTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setColumnDisplaySize(idx, getIntegerValue(s));
break;
case ColumnLabelTag:
if (nullValue)
md.setColumnLabel(idx, null);
else
md.setColumnLabel(idx, s);
break;
case ColumnNameTag:
if (nullValue)
md.setColumnName(idx, null);
else
md.setColumnName(idx, s);
break;
case SchemaNameTag:
if (nullValue) {
md.setSchemaName(idx, null); }
else
md.setSchemaName(idx, s);
break;
case ColumnPrecisionTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setPrecision(idx, getIntegerValue(s));
break;
case ColumnScaleTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setScale(idx, getIntegerValue(s));
break;
case MetaTableNameTag:
if (nullValue)
md.setTableName(idx, null);
else
md.setTableName(idx, s);
break;
case CatalogNameTag:
if (nullValue)
md.setCatalogName(idx, null);
else
md.setCatalogName(idx, s);
break;
case ColumnTypeTag:
if (nullValue)
throw new SQLException(resBundle.handleGetObject("xmlrch.badvalue1").toString());
else
md.setColumnType(idx, getIntegerValue(s));
break;
case ColumnTypeNameTag:
if (nullValue)
md.setColumnTypeName(idx, null);
else
md.setColumnTypeName(idx, s);
break;
default:
//System.out.println("MetaData: Unknown Tag: (" + getTag() + ")");
break;
}
}
private void setDataValue(char[] ch, int start, int len) throws SQLException {
switch (getTag()) {
case ColTag:
columnValue = new String(ch, start, len);
/**
* This has been added for handling of special characters. When special
* characters are encountered the characters function gets called for
* each of the characters so we need to append the value got in the
* previous call as it is the same data present between the start and
* the end tag.
**/
tempStr = tempStr.concat(columnValue);
break;
case UpdTag:
case UpdRowTag:
/**
* This has been added for handling of special characters. When special
* characters are encountered the characters function gets called for
* each of the characters so we need to append the value got in the
* previous call as it is the same data present between the start and
* the end tag.
**/
tempUpdate = tempUpdate.concat(new String(ch,start,len));
break;
case InsTag:
}
}
private void insertValue(String s) throws SQLException {
if (s == null || getNullValue()) {
rs.updateNull(idx);
return;
}
// no longer have to deal with those pesky nulls.
int type = rs.getMetaData().getColumnType(idx);
switch (type) {
case java.sql.Types.BIT:
rs.updateBoolean(idx, getBooleanValue(s));
break;
case java.sql.Types.BOOLEAN:
rs.updateBoolean(idx, getBooleanValue(s));
break;
case java.sql.Types.SMALLINT:
case java.sql.Types.TINYINT:
rs.updateShort(idx, getShortValue(s));
break;
case java.sql.Types.INTEGER:
rs.updateInt(idx, getIntegerValue(s));
break;
case java.sql.Types.BIGINT:
rs.updateLong(idx, getLongValue(s));
break;
case java.sql.Types.REAL:
case java.sql.Types.FLOAT:
rs.updateFloat(idx, getFloatValue(s));
break;
case java.sql.Types.DOUBLE:
rs.updateDouble(idx, getDoubleValue(s));
break;
case java.sql.Types.NUMERIC:
case java.sql.Types.DECIMAL:
rs.updateObject(idx, getBigDecimalValue(s));
break;
case java.sql.Types.BINARY:
case java.sql.Types.VARBINARY:
case java.sql.Types.LONGVARBINARY:
rs.updateBytes(idx, getBinaryValue(s));
break;
case java.sql.Types.DATE:
rs.updateDate(idx, getDateValue(s));
break;
case java.sql.Types.TIME:
rs.updateTime(idx, getTimeValue(s));
break;
case java.sql.Types.TIMESTAMP:
rs.updateTimestamp(idx, getTimestampValue(s));
break;
case java.sql.Types.CHAR:
case java.sql.Types.VARCHAR:
case java.sql.Types.LONGVARCHAR:
rs.updateString(idx, getStringValue(s));
break;
default:
}
}
/**
* Throws the given <code>SAXParseException</code> object. This
* exception was originally thrown by the SAX parser and is passed
* to the method <code>error</code> when the SAX parser invokes it.
*
* @param e the <code>SAXParseException</code> object to throw
*/
public void error (SAXParseException e) throws SAXParseException {
throw e;
}
// dump warnings too
/**
* Prints a warning message to <code>System.out</code> giving the line
* number and uri for what caused the warning plus a message explaining
* the reason for the warning. This method is invoked by the SAX parser.
*
* @param err a warning generated by the SAX parser
*/
public void warning (SAXParseException err) throws SAXParseException {
System.out.println (MessageFormat.format(resBundle.handleGetObject("xmlrch.warning").toString(), new Object[] { err.getMessage(), err.getLineNumber(), err.getSystemId() }));
}
/**
*
*/
public void notationDecl(String name, String publicId, String systemId) {
}
/**
*
*/
public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) {
}
/**
* Returns the current row of this <code>Rowset</code>object.
* The ResultSet's cursor is positioned at the Row which is needed
*
* @return the <code>Row</code> object on which the <code>RowSet</code>
* implementation objects's cursor is positioned
*/
private Row getPresentRow(WebRowSetImpl rs) throws SQLException {
//rs.setOriginalRow();
// ResultSetMetaData rsmd = rs.getMetaData();
// int numCols = rsmd.getColumnCount();
// Object vals[] = new Object[numCols];
// for(int j = 1; j<= numCols ; j++){
// vals[j-1] = rs.getObject(j);
// }
// return(new Row(numCols, vals));
return null;
}
}
|