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
|
/*
* xmlimport.cpp
*
* (c) 2003,2009-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
*
* 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.
*/
/** @file xmlimport.cpp
* Source file for XMLImport
*/
#include <QLocale>
#include "calc/calcnode.h"
#include "condition.h"
#include "database.h"
#include "filter.h"
#include "formatting.h"
#include "pbdialog.h"
#include "xmlimport.h"
/**
* Constructor.
*
* @param dbase The database being imported into
*/
XMLImport::XMLImport(Database *dbase) : QObject(), QXmlDefaultHandler(),
db(dbase), error("")
{
buildParentsMap();
sections.append("global");
sections.append("enums");
sections.append("enumoptions");
sections.append("columns");
sections.append("views");
sections.append("viewcolumns");
sections.append("sorts");
sections.append("sortcolumns");
sections.append("filters");
sections.append("filterconditions");
sections.append("calcs");
sections.append("calcnodes");
sections.append("data");
optionalSections.append("calcs");
optionalSections.append("calcnodes");
containers.append("global");
containers.append("enum");
containers.append("enumoption");
containers.append("column");
containers.append("view");
containers.append("viewcolumn");
containers.append("sort");
containers.append("sortcolumn");
containers.append("filter");
containers.append("filtercondition");
containers.append("calc");
containers.append("calcnode");
containers.append("r");
dataFields.append("s");
dataFields.append("i");
dataFields.append("f");
dataFields.append("b");
dataFields.append("n");
dataFields.append("d");
dataFields.append("t");
dataFields.append("c");
dataFields.append("q");
dataFields.append("p");
dataFields.append("e");
booleans.append("scdesc");
booleans.append("fccase");
booleans.append("b");
integers.append("i");
integers.append("d");
integers.append("t");
integers.append("q");
nonNegativeIntegers.append("eindex");
nonNegativeIntegers.append("eoindex");
nonNegativeIntegers.append("cindex");
nonNegativeIntegers.append("ctype");
nonNegativeIntegers.append("cid");
nonNegativeIntegers.append("vcindex");
nonNegativeIntegers.append("scindex");
nonNegativeIntegers.append("fcposition");
nonNegativeIntegers.append("fcoperator");
nonNegativeIntegers.append("calcid");
nonNegativeIntegers.append("calcdecimals");
nonNegativeIntegers.append("cnid");
nonNegativeIntegers.append("cnnodeid");
nonNegativeIntegers.append("cnparentid");
nonNegativeIntegers.append("cntype");
positiveIntegers.append("gversion");
positiveIntegers.append("eid");
positiveIntegers.append("vrpp");
positiveIntegers.append("vdeskrpp");
positiveIntegers.append("vcwidth");
positiveIntegers.append("vcdeskwidth");
}
/**
* Get the base error message (if any) generated during the import attempt.
*
* @return An error message, or an empty string if none
*/
QString XMLImport::errorString()
{
return error;
}
/**
* Get a detailed error message, including the line and column of the XML
* file at which the error occurred.
*
* @return A detailed error message, or an empty string if none
*/
QString XMLImport::formattedError()
{
if (!error.isEmpty()) {
QLocale locale = QLocale::system();
QString fullError = tr("Error at") + ": ";
fullError += tr("Line") + " ";
fullError += locale.toString(xmlLocator->lineNumber()) + ", ";
fullError += tr("Column") + " ";
fullError += locale.toString(xmlLocator->columnNumber()) + "\n";
fullError += error;
return fullError;
}
return error;
}
/**
* Called when the SAX parser starts work on a new XML file. Resets a number
* of string and collection attributes back to their default, empty state.
*
* @return True if the state attributes were successfully reset
*/
bool XMLImport::startDocument()
{
lastSection = "";
error = "";
gview = "";
gsort = "";
gfilter = "";
ancestors.clear();
fields.clear();
enumMap.clear();
enumIds.clear();
columnMap.clear();
idList.clear();
filterMap.clear();
return true;
}
/**
* Called when the SAX parser finishes parsing the start tag of an element.
* Makes sure that this element can validly be positioned here, and prepares
* to handle any contained text and/or attributes.
*
* @param namespaceURI Empty string, since no namespace processing is being done
* @param localName Empty string, since no namespace processing is being done
* @param qName The name of the element being parsed
* @param atts The attributes of the element being parsed
* @return True if the element looks valid so far, false otherwise
*/
bool XMLImport::startElement(const QString&, const QString&,
const QString &qName, const QXmlAttributes &atts)
{
int count = ancestors.count();
if (qName == "portabase") {
if (count == 0) {
ancestors.append(qName);
return true;
}
else {
error = "portabase " + tr("only allowed as root element");
return false;
}
}
if (!parents.contains(qName)) {
error = tr("Unknown element") + ": " + qName;
return false;
}
parent = ancestors[count - 1];
if (parents[qName] != parent) {
error = tr("%1 not allowed as child of %2");
error = error.arg(qName).arg(parent);
return false;
}
int sectionIndex = sections.indexOf(qName);
if (sectionIndex != -1) {
QString missing = missingSection(lastSection, sectionIndex);
if (!missing.isEmpty()) {
error = tr("Missing element") + ": " + missing;
return false;
}
lastSection = qName;
}
ancestors.append(qName);
if (dataFields.contains(qName)) {
bool ok = false;
colId = atts.value("c");
colId.toInt(&ok);
if (!ok) {
error = tr("\"c\" attribute is missing or non-integer");
return false;
}
}
if (containers.contains(qName)) {
fields.clear();
}
if (qName == "enums" || qName == "views" || qName == "sorts"
|| qName == "filters" || qName == "calcnodes") {
indexMap.clear();
}
text = "";
return true;
}
/**
* Called when the SAX parser finishes parsing the end tag of an element.
* Adds the information gathered over the course of parsing the element to
* the target PortaBase file.
*
* @param namespaceURI Empty string, since no namespace processing is being done
* @param localName Empty string, since no namespace processing is being done
* @param qName The name of the element being parsed
* @return True if the element contained valid data, false otherwise
*/
bool XMLImport::endElement(const QString&, const QString&,
const QString &qName)
{
ancestors.removeAll(qName);
if (!setField(qName)) {
return false;
}
if (qName == "global") {
return updateGlobalRecord();
}
else if (qName == "enum") {
return addEnum();
}
else if (qName == "enums") {
return validateEnums();
}
else if (qName == "enumoption") {
return addEnumOption();
}
else if (qName == "enumoptions") {
return validateIndexMap("eoindex", "eotext");
}
else if (qName == "column") {
return addColumn();
}
else if (qName == "columns") {
return validateColumns();
}
else if (qName == "view") {
return addView();
}
else if (qName == "viewcolumn") {
return addViewColumn();
}
else if (qName == "viewcolumns") {
return validateViewColumns();
}
else if (qName == "sort") {
return addSort();
}
else if (qName == "sortcolumn") {
return addSortColumn();
}
else if (qName == "sortcolumns") {
return validateSortColumns();
}
else if (qName == "filter") {
return addFilter();
}
else if (qName == "filtercondition") {
return addFilterCondition();
}
else if (qName == "filterconditions") {
return validateFilterConditions();
}
else if (qName == "calc") {
return addCalc();
}
else if (qName == "calcnode") {
return addCalcNode();
}
else if (qName == "calcnodes") {
return validateCalcNodes();
}
else if (qName == "r") {
return addRow();
}
else if (qName == "data") {
db->calculateAll();
return true;
}
else if (dataFields.contains(qName)) {
fields.insert(colId, text);
return true;
}
else {
fields.insert(qName, text);
return true;
}
}
/**
* Called when text content is found in the body of an element. Appends it
* to any text previously found in the same element, and saves it for later
* use when parsing the element's end tag.
*
* @param ch The text which was just parsed
* @return True if the text was appended successfully
*/
bool XMLImport::characters(const QString &ch)
{
text += ch;
return true;
}
/**
* Called when the SAX parser encounters an error from which it cannot recover
* (typically, this means the file being parsed is not a well-formed XML
* file). Stores the exception message as the error message and returns
* false.
*
* @param exception The SAX parsing exception which occurred
* @return False (always)
*/
bool XMLImport::fatalError(const QXmlParseException &exception)
{
if (error.isEmpty()) {
error = exception.message();
}
return false;
}
/**
* Called by the SAX parser to provide this object with a means of determining
* exactly where in the source XML file the parser is. This information is
* used to give line and column numbers in error messages.
*
* @param locator The object used to determine the parser's current location
* in the file
*/
void XMLImport::setDocumentLocator(QXmlLocator *locator)
{
xmlLocator = locator;
}
/**
* Check to see if any of the top-level elements in the file have been
* omitted, and if so if that omission can still result in a valid file.
* (Some sections are in fact optional, due to having been introduced in
* newer versions of PortaBase so exports from older versions won't have
* them.)
*
* @param before The name of the last top-level element which was parsed
* @param afterIndex The position index at which the top-level element now
* being parsed is found when all sections are present
* @return The name of a missing required element, or an empty string if none
*/
QString XMLImport::missingSection(const QString &before, int afterIndex)
{
if (afterIndex == 0) {
// first section, there can't be one missing before it
return "";
}
QString previous = sections[afterIndex - 1];
if (before == previous) {
// no missing section
return "";
}
if (!optionalSections.contains(previous)) {
// missing non-optional section
return before;
}
// missing an optional section, see if anything earlier is missing
return missingSection(before, afterIndex - 1);
}
/**
* Update the "_global" view in the PortaBase file to contain the data which
* was found in the XML file (specifically, the current view, filter, and
* sorting names). A specified file format version older than the first
* PortaBase version to support XML export or newer than the one native to
* the version of PortaBase being run is treated as an error. The created
* file will always be of the version native for the PortaBase binary in use.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::updateGlobalRecord()
{
gversion = getField("gversion").toInt();
gview = getField("gview");
gsort = getField("gsort");
gfilter = getField("gfilter");
if (!error.isEmpty()) {
return false;
}
if (gversion < 7 || gversion > FILE_VERSION) {
error = tr("Unsupported") + " gversion: " + QString::number(gversion);
return false;
}
db->setGlobalInfo(gview, gsort, gfilter);
return true;
}
/**
* Add the enumeration which was just parsed from the XML file to the
* target database. None of its options are added here, those will be found
* later in the XML file and added to the database then.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addEnum()
{
int eindex = getField("eindex").toInt();
QString ename = getField("ename");
int eid = getField("eid").toInt();
if (!error.isEmpty()) {
return false;
}
if (!validateName(ename)) {
return false;
}
if (eid < FIRST_ENUM) {
error = tr("Invalid") + " eid: " + QString::number(eid);
return false;
}
if (indexMap.contains(ename)) {
error = tr("Duplicate") + " ename: " + ename;
return false;
}
QMap<int,QString> mapping;
indexMap.insert(ename, mapping);
// empty option list; enum options will be added later
QStringList options;
db->addEnum(ename, options, eindex, eid);
enumMap.insert(eindex, eid);
return true;
}
/**
* Add the enumeration option which was just parsed from the XML file to the
* target database.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addEnumOption()
{
QString idString = getField("eoenum");
int eoenum = idString.toInt();
int eoindex = getField("eoindex").toInt();
QString eotext = getField("eotext");
if (!error.isEmpty()) {
return false;
}
if (!validateName(eotext)) {
return false;
}
if (!enumIds.contains(eoenum)) {
error = tr("Invalid") + " eoenum: " + idString;
return false;
}
indexMap[idString].insert(eoindex, eotext);
db->addEnumOption(db->getEnumName(eoenum), eotext, eoindex);
return true;
}
/**
* Add the column definition which was just parsed from the XML file to the
* target database.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addColumn()
{
int cindex = getField("cindex").toInt();
QString cname = getField("cname");
int ctype = getField("ctype").toInt();
QString cdefault = getField("cdefault");
int cid = getField("cid").toInt();
if (!error.isEmpty()) {
return false;
}
if (!validateName(cname)) {
return false;
}
if (ctype > LAST_TYPE) {
if (!enumIds.contains(ctype)) {
error = tr("Invalid") + " ctype: " + QString::number(ctype);
return false;
}
}
if (!isValidDefault(cname, ctype, cdefault)) {
return false;
}
db->addColumn(cindex, cname, ctype, cdefault, cid);
columnMap.insert(cindex, cid);
return true;
}
/**
* Add the view which was just parsed from the XML file to the target
* database. None of its component columns are added here, those will be
* found later in the XML file and added to the database then.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addView()
{
QString vname = getField("vname");
int vrpp = getField("vrpp").toInt();
int vdeskrpp = getField("vdeskrpp").toInt();
if (!error.isEmpty()) {
return false;
}
QString defaultSort = getOptionalField("vsort", "_none");
QString defaultFilter = getOptionalField("vfilter", "_none");
if (vname != "_all") {
if (!validateName(vname)) {
return false;
}
}
if (indexMap.contains(vname)) {
error = tr("Duplicate") + " vname: " + vname;
return false;
}
QMap<int,QString> mapping;
indexMap.insert(vname, mapping);
// empty column list; view columns will be added later
QStringList cols;
db->addView(vname, cols, defaultSort, defaultFilter, vrpp, vdeskrpp);
return true;
}
/**
* Add the view column definition which was just parsed from the XML file to
* the target database.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addViewColumn()
{
QString vcview = getField("vcview");
int vcindex = getField("vcindex").toInt();
QString vcname = getField("vcname");
int vcwidth = getField("vcwidth").toInt();
int vcdeskwidth = getField("vcdeskwidth").toInt();
if (!error.isEmpty()) {
return false;
}
if (!indexMap.contains(vcview)) {
error = tr("Invalid") + " vcview: " + vcview;
return false;
}
if (!colNames.contains(vcname)) {
error = tr("Invalid") + " vcname: " + vcname;
return false;
}
indexMap[vcview].insert(vcindex, vcname);
db->addViewColumn(vcview, vcname, vcindex, vcwidth, vcdeskwidth);
return true;
}
/**
* Add the sorting which was just parsed from the XML file to a collection of
* sorting data. It will be added to the database later, when parsing of the
* <sortcolumns> element concludes.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addSort()
{
QString sname = getField("sname");
if (!error.isEmpty()) {
return false;
}
if (sname != "_single") {
if (!validateName(sname)) {
return false;
}
}
if (indexMap.contains(sname)) {
error = tr("Duplicate") + " sname: " + sname;
return false;
}
QMap<int,QString> mapping;
indexMap.insert(sname, mapping);
return true;
}
/**
* Add the sorting column definition which was just parsed from the XML file
* to the collection of that sorting's columns. These will be added to the
* database later, when parsing of the <sortcolumns> element concludes.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addSortColumn()
{
QString scsort = getField("scsort");
int scindex = getField("scindex").toInt();
QString scname = getField("scname");
int scdesc = getField("scdesc").toInt();
if (!error.isEmpty()) {
return false;
}
if (!indexMap.contains(scsort)) {
error = tr("Invalid") + " scsort: " + scsort;
return false;
}
if (!colNames.contains(scname)) {
error = tr("Invalid") + " scname: " + scname;
return false;
}
if (scdesc == 1) {
// mangle name to indicate descending order
scname = "_" + scname;
}
indexMap[scsort].insert(scindex, scname);
return true;
}
/**
* Add the filter which was just parsed from the XML file to a collection of
* filter data. It will be added to the database later, when parsing of the
* <filterconditions> element concludes.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addFilter()
{
QString fname = getField("fname");
if (!error.isEmpty()) {
return false;
}
if (fname != "_allrows" && fname != "_simple") {
if (!validateName(fname)) {
return false;
}
}
if (indexMap.contains(fname)) {
error = tr("Duplicate") + " fname: " + fname;
return false;
}
QMap<int,QString> mapping;
indexMap.insert(fname, mapping);
Filter *filter = new Filter(db, fname);
filterMap.insert(fname, filter);
return true;
}
/**
* Add the filter condition definition which was just parsed from the XML file
* to the collection of that filter's conditions. These will be added to the
* database later, when parsing of the <filterconditions> element concludes.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addFilterCondition()
{
QString fcfilter = getField("fcfilter");
int fcposition = getField("fcposition").toInt();
QString fccolumn = getField("fccolumn");
int fcoperator = getField("fcoperator").toInt();
QString fcconstant = getField("fcconstant");
int fccase = getField("fccase").toInt();
if (!error.isEmpty()) {
return false;
}
if (!indexMap.contains(fcfilter)) {
error = tr("Invalid") + " fcfilter: " + fcfilter;
return false;
}
int ctype = STRING;
if (fccolumn != "_anytext") {
if (!colNames.contains(fccolumn)) {
error = tr("Invalid") + " fccolumn: " + fccolumn;
return false;
}
ctype = db->getType(fccolumn);
QString errMsg = db->isValidValue(ctype, fcconstant);
if (!errMsg.isEmpty()) {
error = tr("Invalid") + " fcconstant: " + fcconstant + "("
+ errMsg + ")";
return false;
}
}
if (!isValidOperator(ctype, fcoperator, fccase)) {
return false;
}
indexMap[fcfilter].insert(fcposition, QString::number(fcposition));
Condition *condition = new Condition(db);
condition->setColName(fccolumn);
condition->setOperator((Condition::Operator)fcoperator);
condition->setConstant(fcconstant);
condition->setCaseSensitive(fccase);
filterMap[fcfilter]->addCondition(condition, fcposition);
return true;
}
/**
* Add the calculation which was just parsed from the XML file to the target
* database. None of its component nodes are added here, those will be
* found later in the XML file and added to the database then.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addCalc()
{
int calcid = getField("calcid").toInt();
int calcdecimals = getField("calcdecimals").toInt();
if (!error.isEmpty()) {
return false;
}
int index = idList.indexOf(calcid);
if (index == -1) {
error = tr("Invalid") + " calcid: " + QString::number(calcid);
return false;
}
QString colName = colNames[index];
int type = db->getType(colName);
if (type != CALC) {
error = tr("Invalid") + " calcid: " + QString::number(calcid);
return false;
}
db->updateCalc(colName, 0, calcdecimals);
return true;
}
/**
* Add the calculation node definition which was just parsed from the XML
* file to the target database.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addCalcNode()
{
int cnid = getField("cnid").toInt();
int cnnodeid = getField("cnnodeid").toInt();
int cnparentid = getField("cnparentid").toInt();
CalcNode::NodeType cntype = (CalcNode::NodeType)getField("cntype").toInt();
QString cnvalue = getField("cnvalue");
if (!error.isEmpty()) {
return false;
}
int index = idList.indexOf(cnid);
if (index == -1) {
error = tr("Invalid") + " cnid: " + QString::number(cnid);
return false;
}
QString colName = colNames[index];
int type = db->getType(colName);
if (type != CALC) {
error = tr("Invalid") + " cnid: " + QString::number(cnid);
return false;
}
if (cnparentid >= cnnodeid && !(cnparentid == 0 && cnnodeid == 0)) {
error = tr("Invalid") + " cnparentid: " + QString::number(cnparentid);
return false;
}
if (!indexMap.contains(colName)) {
QMap<int,QString> mapping;
indexMap.insert(colName, mapping);
}
indexMap[colName].insert(cnnodeid, QString::number(cnparentid));
CalcNode node(cntype, cnvalue);
db->addCalcNode(cnid, &node, cnnodeid, cnparentid);
return true;
}
/**
* Add the row of the main data table which was just parsed from the XML
* file to the target database.
*
* @return False if an error occurred, true otherwise
*/
bool XMLImport::addRow()
{
QStringList values;
for (int i = 0; i < colCount; i++) {
values.append(getDataField(idList[i]));
}
if (!error.isEmpty()) {
return false;
}
error = db->addRow(values, 0, true);
return error.isEmpty();
}
/**
* Make sure that the provided name meets PortaBase's minimum standards for
* database artifact names: not empty, and doesn't start with an underscore
* (names with leading underscores are reserved for internal use).
*
* @param name The name to be validated
* @return True if the provided name is potentially valid, false otherwise
*/
bool XMLImport::validateName(const QString &name)
{
if (name.isEmpty()) {
error = PBDialog::tr("No name entered");
return false;
}
if (name.startsWith("_")) {
error = PBDialog::tr("Name must not start with '_'") + ": " + name;
return false;
}
return true;
}
/**
* Make sure that the data intended for the "_global" view actually referred
* to items present later in the file. Called after all views, sortings, and
* filters have been parsed and imported.
*
* @return False if a missing view, sorting, or filter is referenced
*/
bool XMLImport::validateGlobal()
{
if (gview != "_all") {
QStringList views = db->listViews();
if (!views.contains(gview)) {
error = tr("Invalid") + " gview: " + gview;
return false;
}
}
if (!gsort.isEmpty()) {
QStringList sortings = db->listSortings();
if (!sortings.contains(gsort)) {
error = tr("Invalid") + " gsort: " + gsort;
return false;
}
}
if (gfilter != "_allrows") {
QStringList filters = db->listFilters();
if (!filters.contains(gfilter)) {
error = tr("Invalid") + " gfilter: " + gfilter;
return false;
}
}
return true;
}
/**
* Make sure that the set of enums presented in the XML is valid when taken
* as a whole. Returns false if there is a gap in the position index
* sequence or if an ID is used more than once. Called when parsing the
* <enums> element's closing tag.
*
* @return False if a problem was found with the enum set, true otherwise
*/
bool XMLImport::validateEnums()
{
int count = enumMap.count();
for (int i = 0; i < count; i++) {
if (!enumMap.contains(i)) {
error = tr("Missing") + " eindex: " + QString::number(i);
return false;
}
enumIds.append(enumMap[i]);
}
if (containsDuplicate(enumIds, "eid")) {
return false;
}
return true;
}
/**
* Make sure that the set of data column definitions is valid when taken as a
* whole. Returns false if there is a gap in the position index sequence or
* if a name or ID is used more than once. Otherwise, initializes the data
* table with the columns that were in the XML file.
*
* @return False if the provided column set was invalid, true otherwise
*/
bool XMLImport::validateColumns()
{
colNames = db->listColumns();
if (containsDuplicate(colNames, "cname")) {
return false;
}
colCount = columnMap.count();
for (int i = 0; i < colCount; i++) {
if (!columnMap.contains(i)) {
error = tr("Missing") + " cindex: " + QString::number(i);
return false;
}
idList.append(columnMap[i]);
}
if (containsDuplicate(idList, "cid")) {
return false;
}
db->updateDataFormat();
return true;
}
/**
* Make sure that all the sets of view columns are valid when taken as a
* whole. Returns false if there is a gap in a position index sequence, if
* a column is used more than once in the same view, or if the all columns
* view is missing or using a sequence that doesn't match the data columns
* definition.
*
* @return True if all the view component columns are valid, false otherwise
*/
bool XMLImport::validateViewColumns()
{
if (!indexMap.contains("_all")) {
error = tr("Missing") + " vcview: _all";
return false;
}
if (!validateIndexMap("vcindex", "vcname")) {
return false;
}
return true;
}
/**
* Make sure that all the sets of sorting columns are valid when taken as a
* whole. Returns false if there is a gap in a position index sequence or if
* a column is used more than once in the same sorting. Otherwise, adds all
* the sortings defined in the XML file to the target database.
*
* @return True if all the sortings are valid, false otherwise
*/
bool XMLImport::validateSortColumns()
{
if (!validateIndexMap("scindex", "scname")) {
return false;
}
IndexMap::Iterator iter;
QStringList allCols;
QStringList descCols;
int count;
for (iter = indexMap.begin(); iter != indexMap.end(); ++iter) {
allCols.clear();
descCols.clear();
QString sname = iter.key();
QMap<int,QString> mapping = iter.value();
count = mapping.count();
for (int i = 0; i < count; i++) {
QString item = mapping[i];
if (item.startsWith("_")) {
item = item.remove(0, 1);
descCols.append(item);
}
allCols.append(item);
}
db->addSorting(sname, allCols, descCols);
}
return true;
}
/**
* Make sure that all the sets of filter conditions are valid when taken as a
* whole. Returns false if there is a gap in a position index sequence or if
* a condition is used more than once in the same filter. Otherwise, adds
* all the filters defined in the XML file to the target database.
*
* @return True if all the filters are valid, false otherwise
*/
bool XMLImport::validateFilterConditions()
{
if (!validateIndexMap("fcposition", "fcposition")) {
return false;
}
FilterMap::Iterator iter;
for (iter = filterMap.begin(); iter != filterMap.end(); ++iter) {
Filter *filter = iter.value();
if (iter.key() != "_allrows") {
// _allrows already added during database initialization
db->addFilter(filter, false);
}
delete filter;
}
// validation of global table data must happen after everything is defined
if (!validateGlobal()) {
return false;
}
return true;
}
/**
* Make sure that all the sets of calculation nodes are valid when taken as a
* whole. Returns false if a node ID is used more than once in the same
* calculation, if a node's specified parent doesn't exist, or if one of the
* calculations doesn't validate.
*
* @return True if all the calculations are valid, false otherwise
*/
bool XMLImport::validateCalcNodes()
{
IndexMap::Iterator iter;
IntList nodeIds;
for (iter = indexMap.begin(); iter != indexMap.end(); ++iter) {
QMap<int,QString> mapping = iter.value();
QMap<int,QString>::Iterator iter2;
nodeIds.clear();
for (iter2 = mapping.begin(); iter2 != mapping.end(); ++iter2) {
nodeIds.append(iter2.key());
}
if (containsDuplicate(nodeIds, "cnnodeid")) {
return false;
}
for (iter2 = mapping.begin(); iter2 != mapping.end(); ++iter2) {
QString parentId = iter2.value();
if (!nodeIds.contains(parentId.toInt())) {
error = tr("Invalid") + " cnparentid: " + parentId;
return false;
}
}
CalcNode *root = db->loadCalc(iter.key());
error = isValidCalcNode(root);
delete root;
if (!error.isEmpty()) {
return false;
}
}
return true;
}
/**
* Validate the provided calculation node.
*
* @param node The calculation node to be validated
* @return A description of the error encountered, or an empty string if none
*/
QString XMLImport::isValidCalcNode(CalcNode *node)
{
CalcNode::NodeType type = node->type();
if (type < 0 || (type > CalcNode::TimeColumn && type < CALC_FIRST_OP)
|| type > CALC_LAST_OP) {
return tr("Invalid") + " cntype: " + QString::number(type);
}
QString value = node->value();
if (type == CalcNode::Constant) {
bool ok;
Formatting::parseDouble(value, &ok);
if (!ok) {
return tr("Invalid") + " cnvalue: " + value;
}
}
else if (type == CalcNode::DateConstant) {
QString error = db->isValidValue(DATE, value);
if (!error.isEmpty()) {
return tr("Invalid") + " cnvalue: " + value;
}
}
else if (type == CalcNode::Column) {
if (!colNames.contains(value)) {
return tr("Invalid") + " cnvalue: " + value;
}
int colType = db->getType(value);
if (colType != INTEGER && colType != FLOAT && colType != BOOLEAN) {
return tr("Invalid") + " cnvalue: " + value;
}
}
else if (type == CalcNode::DateColumn) {
if (!colNames.contains(value)) {
return tr("Invalid") + " cnvalue: " + value;
}
if (db->getType(value) != DATE) {
return tr("Invalid") + " cnvalue: " + value;
}
}
CalcNodeList children = node->getChildren();
int count = children.count();
int max = node->maxChildren();
if (max != -1 && count > max) {
return tr("Too many child nodes for node type") + ": "
+ QString::number(type);
}
for (int i = 0; i < count; i++) {
QString error = isValidCalcNode(children[i]);
if (!error.isEmpty()) {
return error;
}
}
return "";
}
/**
* Determine if a provided string is a valid default value for a column of
* the specified type. This is different than determining if a string is a
* valid data value of that type because dates and times have special rules
* for default values.
*
* @param cname The name of the column whose default is being checked
* @param ctype The data type of the column
* @param cdefault The default value specified in the XML file
* @return True if the default value is valid, false otherwise
*/
bool XMLImport::isValidDefault(const QString &cname, int ctype,
const QString &cdefault)
{
// in case it's needed...
QString errorMsg = tr("Invalid") + " cdefault: " + cdefault
+ " (" + cname + ")";
if (ctype == DATE) {
bool ok = false;
int value = cdefault.toInt(&ok);
if (!ok || (value != 0 && value != 17520914)) {
error = errorMsg;
return false;
}
}
else if (ctype == TIME) {
bool ok = false;
int value = cdefault.toInt(&ok);
if (!ok || (value != 0 && value != -1)) {
error = errorMsg;
return false;
}
}
else {
if (!db->isValidValue(ctype, cdefault).isEmpty()) {
error = errorMsg;
return false;
}
}
return true;
}
/**
* Determine if the given condition operator code and case sensitivity are
* valid for the specified data type.
*
* @param type The data type of the column being compared
* @param op The operator code specified in the XML file
* @param cs Case sensitivity indicator
* @return True if the provided values are valid, false otherwise
*/
bool XMLImport::isValidOperator(int type, int op, int cs)
{
// in case it's needed...
QString opError = tr("Invalid") + " fcoperator: " + QString::number(op)
+ " (ctype = " + QString::number(type) + ")";
if (type == BOOLEAN) {
if (op != Condition::Equals) {
error = opError;
return false;
}
}
else if (type >= FIRST_ENUM) {
if (op != Condition::Equals && op != Condition::NotEqual) {
error = opError;
return false;
}
if (cs == 0) {
error = tr("Invalid") + " fccase: 0 (ctype = "
+ QString::number(type) + ")";
return false;
}
}
else if (type == STRING || type == NOTE) {
if (op != Condition::Equals && op != Condition::Contains
&& op != Condition::StartsWith && op != Condition::NotEqual) {
error = opError;
return false;
}
}
else {
if (op != Condition::Equals && op != Condition::NotEqual
&& op != Condition::LessThan && op != Condition::LessEqual
&& op != Condition::GreaterThan
&& op != Condition::GreaterEqual) {
error = opError;
return false;
}
}
return true;
}
/**
* Determine if the given list (of names for the given element type) contains
* any duplicate entries.
*
* @param names The list of all names for a certain kind of database item
* @param element The name of the element for which the check is being done
* @return True if at least one duplicate was found, false otherwise
*/
bool XMLImport::containsDuplicate(const QStringList &names,
const QString &element)
{
QStringList copy(names);
int count = copy.count();
for (int i = 0; i < count; i++) {
QString name = copy[0];
copy.removeAt(0);
if (copy.contains(name)) {
error = tr("Duplicate") + " " + element + ": " + name;
return true;
}
}
return false;
}
/**
* Determine if the given list (of IDs for the given element type) contains
* any duplicate entries.
*
* @param items The list of all IDs for a certain kind of database item
* @param element The name of the element for which the check is being done
* @return True if at least one duplicate was found, false otherwise
*/
bool XMLImport::containsDuplicate(const IntList &items, const QString &element)
{
IntList copy(items);
int count = copy.count();
for (int i = 0; i < count; i++) {
int item = copy[0];
copy.removeAt(0);
if (copy.contains(item)) {
error = tr("Duplicate") + " " + element + ": "
+ QString::number(item);
return true;
}
}
return false;
}
/**
* Validate the data currently in the <code>indexMap</code> attribute. Looks
* for gaps in the position index sequence and duplicate names. When
* validating views, also makes sure that the all columns view uses the same
* sequence as the database format definition.
*
* @param indexElement The name of the element containing the index property
* @param stringElement The name of the element containing the name property
* @return False if a problem was found with the data, true otherwise
*/
bool XMLImport::validateIndexMap(const QString &indexElement,
const QString &stringElement)
{
IndexMap::Iterator iter;
int i;
QStringList itemList;
for (iter = indexMap.begin(); iter != indexMap.end(); ++iter) {
QMap<int,QString> mapping = iter.value();
int indexCount = mapping.count();
itemList.clear();
for (i = 0; i < indexCount; i++) {
if (!mapping.contains(i)) {
error = tr("Missing") + " " + indexElement + ": "
+ QString::number(i);
return false;
}
QString item = mapping[i];
if (item.startsWith("_")) {
// unmangle descending sort columns
item = item.remove(0, 1);
}
itemList.append(item);
}
if (iter.key() == "_all") {
if (itemList != colNames) {
error = tr("Incorrect _all view column sequence");
return false;
}
}
if (containsDuplicate(itemList, stringElement)) {
return false;
}
}
return true;
}
/**
* Set the most recently parsed text data to be the value of the named
* property. Performs validation on the value based on the declared type of
* that property (boolean, integer, non-negative integer, etc.), and makes
* sure that it isn't a duplicate element. Called for every closing tag,
* although the value isn't always needed.
*
* @param name The name of the element the text was inside of
* @return True if the text is a non-duplicate valid value of its type
*/
bool XMLImport::setField(const QString &name)
{
if (!containers.contains(parent)) {
// no meaningful text content, move on
return true;
}
bool ok = false;
if (booleans.contains(name)) {
int value = text.toInt(&ok);
if (!ok || value < 0 || value > 1) {
error = name + ": " + tr("must be 0 or 1");
return false;
}
}
else if (integers.contains(name)) {
text.toInt(&ok);
if (!ok) {
error = name + ": " + tr("must be an integer");
return false;
}
}
else if (nonNegativeIntegers.contains(name)) {
int value = text.toInt(&ok);
if (!ok || value < 0) {
error = name + ": " + tr("must be a non-negative integer");
return false;
}
}
else if (positiveIntegers.contains(name)) {
int value = text.toInt(&ok);
if (!ok || value <= 0) {
error = name + ": " + tr("must be a positive integer");
return false;
}
}
if (parent != "r" && fields.contains(name)) {
error = tr("Duplicate element") + ": " + name;
return false;
}
fields.insert(name, text);
return true;
}
/**
* Get the text content of the specified required element. Sets an
* appropriate error message if no such content was found.
*
* @param name The name of the element whose content is desired
* @return The value of the named element, or an empty string if absent
*/
QString XMLImport::getField(const QString &name)
{
if (!fields.contains(name)) {
error = tr("Missing element") + ": " + name;
return "";
}
return fields[name];
}
/**
* Get the text content of the specified optional element. Uses the provided
* default value if no such content was found.
*
* @param name The name of the element whose content is desired
* @param defaultVal The default value
* @return The value of the named element, or the default value if absent
*/
QString XMLImport::getOptionalField(const QString &name,
const QString &defaultVal)
{
if (!fields.contains(name)) {
return defaultVal;
}
return fields[name];
}
/**
* Gets the last parsed value for data column with the specified ID. Sets an
* appropriate error message if no value was found for that column.
*
* @param columnId The ID of the column whose value is desired
* @return The latest value in that column, or an empty string if absent
*/
QString XMLImport::getDataField(int columnId)
{
QString idString = QString::number(columnId);
if (!fields.contains(idString)) {
error = tr("Missing data for column ID %1").arg(idString);
return "";
}
return fields[idString];
}
/**
* Build a mapping between allowed element names and their parent elements.
* Used for validation purposes.
*/
void XMLImport::buildParentsMap()
{
parents.insert("global", "portabase");
parents.insert("columns", "portabase");
parents.insert("views", "portabase");
parents.insert("viewcolumns", "portabase");
parents.insert("sorts", "portabase");
parents.insert("sortcolumns", "portabase");
parents.insert("filters", "portabase");
parents.insert("filterconditions", "portabase");
parents.insert("enums", "portabase");
parents.insert("enumoptions", "portabase");
parents.insert("calcs", "portabase");
parents.insert("calcnodes", "portabase");
parents.insert("data", "portabase");
parents.insert("gversion", "global");
parents.insert("gview", "global");
parents.insert("gsort", "global");
parents.insert("gfilter", "global");
// accidentally exported in 1.6, allow it and ignore it
parents.insert("gcrypt", "global");
parents.insert("enum", "enums");
parents.insert("eindex", "enum");
parents.insert("ename", "enum");
parents.insert("eid", "enum");
parents.insert("enumoption", "enumoptions");
parents.insert("eoenum", "enumoption");
parents.insert("eoindex", "enumoption");
parents.insert("eotext", "enumoption");
parents.insert("column", "columns");
parents.insert("cindex", "column");
parents.insert("cname", "column");
parents.insert("ctype", "column");
parents.insert("cdefault", "column");
parents.insert("cid", "column");
parents.insert("view", "views");
parents.insert("vname", "view");
parents.insert("vrpp", "view");
parents.insert("vdeskrpp", "view");
parents.insert("vsort", "view");
parents.insert("vfilter", "view");
parents.insert("viewcolumn", "viewcolumns");
parents.insert("vcview", "viewcolumn");
parents.insert("vcindex", "viewcolumn");
parents.insert("vcname", "viewcolumn");
parents.insert("vcwidth", "viewcolumn");
parents.insert("vcdeskwidth", "viewcolumn");
parents.insert("sort", "sorts");
parents.insert("sname", "sort");
parents.insert("sortcolumn", "sortcolumns");
parents.insert("scsort", "sortcolumn");
parents.insert("scindex", "sortcolumn");
parents.insert("scname", "sortcolumn");
parents.insert("scdesc", "sortcolumn");
parents.insert("filter", "filters");
parents.insert("fname", "filter");
parents.insert("filtercondition", "filterconditions");
parents.insert("fcfilter", "filtercondition");
parents.insert("fcposition", "filtercondition");
parents.insert("fccolumn", "filtercondition");
parents.insert("fcoperator", "filtercondition");
parents.insert("fcconstant", "filtercondition");
parents.insert("fccase", "filtercondition");
parents.insert("calc", "calcs");
parents.insert("calcid", "calc");
parents.insert("calcdecimals", "calc");
parents.insert("calcnode", "calcnodes");
parents.insert("cnid", "calcnode");
parents.insert("cnnodeid", "calcnode");
parents.insert("cnparentid", "calcnode");
parents.insert("cntype", "calcnode");
parents.insert("cnvalue", "calcnode");
parents.insert("r", "data");
parents.insert("s", "r");
parents.insert("i", "r");
parents.insert("f", "r");
parents.insert("b", "r");
parents.insert("n", "r");
parents.insert("d", "r");
parents.insert("t", "r");
parents.insert("c", "r");
parents.insert("q", "r");
parents.insert("p", "r");
parents.insert("e", "r");
}
|