File: xmlimport.h

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (130 lines) | stat: -rw-r--r-- 5,353 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * xmlimport.h
 *
 * (c) 2003,2009 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.h
 * Header file for XMLImport
 */

#ifndef XMLIMPORT_H
#define XMLIMPORT_H

#include <QMap>
#include <QObject>
#include <QStringList>
#include <QXmlAttributes>
#include <QXmlDefaultHandler>
#include <QXmlLocator>
#include <QXmlParseException>
#include "datatypes.h"

class CalcNode;
class Database;
class Filter;

typedef QMap<QString,QMap<int,QString> > IndexMap;
typedef QMap<QString,Filter*> FilterMap;

/**
 * Handler for parsing %PortaBase XML data files.  XML is used as an
 * import/export format for %PortaBase, allowing the content of a database
 * to be represented in a plain text format for manual or automated
 * manipulation.  This class assumes that data is being loaded into an
 * essentially empty %PortaBase file; to add data to an existing file, use
 * CSV instead.
 */
class XMLImport : public QObject, public QXmlDefaultHandler
{
    Q_OBJECT
public:
    XMLImport(Database *dbase);

    bool startDocument();
    bool startElement(const QString &namespaceURI, const QString &localName,
                      const QString &qName, const QXmlAttributes &atts);
    bool endElement(const QString &namespaceURI, const QString &localName,
                    const QString &qName);
    bool characters (const QString &ch);
    QString errorString();
    QString formattedError();
    bool fatalError(const QXmlParseException &exception);
    void setDocumentLocator(QXmlLocator *locator);

private:
    void buildParentsMap();
    bool updateGlobalRecord();
    bool addEnum();
    bool addEnumOption();
    bool addColumn();
    bool addView();
    bool addViewColumn();
    bool addSort();
    bool addSortColumn();
    bool addFilter();
    bool addFilterCondition();
    bool addCalc();
    bool addCalcNode();
    bool addRow();
    bool validateName(const QString &name);
    bool validateGlobal();
    bool validateColumns();
    bool validateEnums();
    bool validateViewColumns();
    bool validateSortColumns();
    bool validateFilterConditions();
    bool validateCalcNodes();
    QString missingSection(const QString &before, int afterIndex);
    bool isValidDefault(const QString &cname, int ctype,
                        const QString &cdefault);
    bool isValidOperator(int type, int op, int cs);
    QString isValidCalcNode(CalcNode *node);
    bool containsDuplicate(const QStringList &names, const QString &element);
    bool containsDuplicate(const IntList &names, const QString &element);
    bool validateIndexMap(const QString &intElement,
                          const QString &stringElement);
    bool setField(const QString &name);
    QString getField(const QString &name);
    QString getOptionalField(const QString &name, const QString &defaultVal);
    QString getDataField(int columnId);

private:
    Database *db; /**< The database to load data into */
    QXmlLocator *xmlLocator; /**< Current location in the file being parsed */
    QString error; /**< Error message, empty if no errors encountered yet */
    QString text; /**< The text content of the element being parsed */
    QStringList ancestors; /**< Element names from the root to here */
    QString parent; /**< The name of the current element's parent element */
    QString lastSection; /**< Name of the last top-level element parsed */
    QMap<QString,QString> fields; /**< Name/ID to value map for the current element */
    int gversion; /**< Version of the PortaBase file format this data was intended for */
    QString gview; /**< Name of the database view to set as current */
    QString gsort; /**< Name of the sorting to set as current */
    QString gfilter; /**< Name of the filter to set as current */
    QMap<int,int> enumMap; /**< Map of enum indexes to IDs */
    IntList enumIds; /**< List of enum IDs */
    QMap<int,int> columnMap; /**< Map of column indexes to IDs */
    QStringList colNames; /**< List of column names */
    IntList idList; /**< List of column IDs */
    int colCount; /**< Number of columns in the database */
    IndexMap indexMap; /**< Data structure used while parsing columns, enums, etc. */
    FilterMap filterMap; /**< Filter name to filter object mapping */
    QString colId; /**< Column ID of the data field being parsed */
    QMap<QString,QString> parents; /**< Allowed child-to-parent element name mappings */
    QStringList sections; /**< List of top-level element names */
    QStringList optionalSections; /**< Names of section elements which may be omitted */
    QStringList containers; /**< Names of elements whose child elements contain text data */
    QStringList dataFields; /**< Names of elements representing data fields */
    QStringList booleans; /**< Names of elements containing boolean value text */
    QStringList integers; /**< Names of elements containing integer value text */
    QStringList nonNegativeIntegers; /**< Names of elements containing non-negative integer value text */
    QStringList positiveIntegers; /**< Names of elements containing positive integer value text */
};

#endif