summaryrefslogtreecommitdiffstats
path: root/poxml/parser.h
blob: 923723c5b5f2356fad96cafd958c26f176ccf079 (plain)
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
#ifndef PARSER_H
#define PARSER_H

#include <tqxml.h>
#include <tqmap.h>
#include <tqregexp.h>

struct BlockInfo {
    int start_line;
    int start_col;
    int end_line;
    int end_col;

    // used to detect sub-messages
    int offset;

    BlockInfo() {
        start_line = 0;
        start_col = 0;
        end_line = 0;
        end_col = 0;

        // used to detect sub-messages
        offset = 0;
    }
};

class MsgBlock {
 public:
    MsgBlock() { start = end = 0; do_not_split = false; }
    MsgBlock(const MsgBlock &rhs ) {
        *this = rhs;
    }
    TQValueList<BlockInfo> lines;
    TQString tag;
    TQString comment;
    TQString msgid;
    TQString msgid_plural;
    TQString msgstr;
    TQStringList msgstr_plurals;
    int start, end;
    bool do_not_split;

    void operator=(const MsgBlock& rhs) {
        lines = rhs.lines;
        tag = rhs.tag;
        comment = rhs.comment;
        msgid = rhs.msgid;
	msgid_plural = rhs.msgid_plural;
        msgstr = rhs.msgstr;
	msgstr_plurals = rhs.msgstr_plurals;
        start = rhs.start;
        end = rhs.end;
        do_not_split = rhs.do_not_split;
    }
};

class ParaCounter
{
public:
    ParaCounter() { current = 0; }
    void addAnchor(TQString anchor) { anchors.insert(anchor, current); }
    void increasePara() { current++; }

    TQMap<TQString, int> anchors;
    int current;
};

class MsgList : public TQValueList<MsgBlock>
{
public:
    MsgList() {}
    ParaCounter pc;
};

class StructureParser : public QXmlDefaultHandler
{
public:
    bool startDocument();
    bool startElement( const TQString&, const TQString&, const TQString& ,
                       const TQXmlAttributes& );
    bool endElement( const TQString&, const TQString&, const TQString& );
    bool characters( const TQString &ch);
    static bool isCuttingTag(const TQString &tag);
    static bool isSingleTag(const TQString &qName);
    static bool isLiteralTag(const TQString &qName);
    void setDocumentLocator ( TQXmlLocator * l ) { locator = l; }
    bool skippedEntity ( const TQString & name );
    bool fatalError ( const TQXmlParseException & );
    bool comment ( const TQString & );
    bool error(const TQXmlParseException &e ) { return fatalError(e); }
    bool warning(const TQXmlParseException &e ) { return fatalError(e); }
    MsgList getList() const { return list; }
    MsgList splitMessage(const MsgBlock &message);

    virtual bool startCDATA();
    virtual bool endCDATA();

    static bool closureTag(const TQString& message, const TQString &tag);
    static bool isClosure(const TQString &message);
    static void descape(TQString &message);
    static TQString escapeLiterals( const TQString &contents);
    static TQString descapeLiterals( const TQString &contents);
    static void cleanupTags( TQString &contents );
    static void removeEmptyTags( TQString &contents);
    static void stripWhiteSpace( TQString &contents);

private:
    bool formatMessage(MsgBlock &message) const;

    TQXmlLocator *locator;
    TQString message;
    int inside, startline, startcol;
    int line;
    MsgList list;
    mutable TQRegExp infos_reg;
    mutable TQRegExp do_not_split_reg;
};

void outputMsg(const char *prefix, const TQString &message);
MsgList parseXML(const char *filename);
TQString escapePO(TQString msgid);

#endif