summaryrefslogtreecommitdiffstats
path: root/ksayit/src/saxhandler.cpp
blob: 7e2d6aa79ca79f45bea14e16ac8ad86e76046e44 (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
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
//
// C++ Implementation: saxhandler
//
// Description:
//
//
// Author: Robert Vogl <voglrobe@web.de>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
// #include <iostream> // cout
// using namespace std;

// KDE includes
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>

// App specific includes
#include "saxhandler.h"

SaxHandler::SaxHandler()
 : TQXmlDefaultHandler()
{
    m_output = TQString();
    m_rtf = true;
    m_tagmap["action"]        = "B";
    m_tagmap["application"]   = "B";
    m_tagmap["function"]      = "B";
    m_tagmap["guibutton"]     = "B";
    m_tagmap["guiicon"]       = "B";
    m_tagmap["guilabel"]      = "B";
    m_tagmap["guimenu"]       = "B";
    m_tagmap["guimenuitem"]   = "B";
    m_tagmap["guisubmenu"]    = "B";
    m_tagmap["menuchoice"]    = "B";
    m_tagmap["mousebutton"]   = "B";
    m_tagmap["option"]        = "B";
    m_tagmap["author"]        = "B";
    m_tagmap["corpauthor"]    = "B";
    m_tagmap["warning"]       = "FONT color=\"red\"";
    m_tagmap["command"]       = "TT";
    m_tagmap["email"]         = "TT";
    m_tagmap["filename"]      = "TT";
    m_tagmap["keycap"]        = "TT";
    m_tagmap["keycode"]       = "TT";
    m_tagmap["keycombo"]      = "TT";
    m_tagmap["keysym"]        = "TT";
    m_tagmap["link"]          = "TT";
    m_tagmap["literal"]       = "TT";
    m_tagmap["userinput"]     = "TT";
    m_tagmap["citation"]      = "EM";
    m_tagmap["emphasis"]      = "EM";
    m_tagmap["foreignphrase"] = "EM";
    m_tagmap["phrase"]        = "EM";
    m_tagmap["comment"]       = "EM";
    m_tagmap["note"]          = "EM";
    m_tagmap["tip"]           = "EM";
    m_tagmap["subscript"]     = "small";
    m_tagmap["superscript"]   = "small";
    m_tagmap["itemizedlist"]  = "UL";
    m_tagmap["listitem"]      = "LI";
}

SaxHandler::~SaxHandler()
{
}


void SaxHandler::setRTF(bool rtf)
{
    m_rtf = rtf;
}


bool SaxHandler::startElement(const TQString &,
                    const TQString &,
                    const TQString & qName,
                    const TQXmlAttributes & atts )
{
    if ( !m_rtf )
        return true;
        
    TQString tag = qName.lower();
    
    TagMapT::iterator it;
    it = m_tagmap.find(tag);
    if ( it != m_tagmap.end() ){
        // tag found in hash table
        TQString rtftag = (*it).second;
        m_output += "<" + rtftag + ">";
    }
    
    return true;
}


bool SaxHandler::endElement(const TQString &,
                    const TQString &,
                    const TQString & qName)
{
    if ( !m_rtf )
        return true;

    TQString tag = qName.lower();

    TagMapT::iterator it;
    it = m_tagmap.find(tag);
    if ( it != m_tagmap.end() ){
        // tag found in hash table
        TQString rtftag = (*it).second;
        m_output += "</" + rtftag.section(" ", 0, 0) + ">";
    }

    return true;
}


bool SaxHandler::characters(const TQString & ch)
{
    m_output += ch;
    return true;
}


bool SaxHandler::fatalError(const TQXmlParseException &exc)
{
    TQString err = i18n("Fatal error while parsing XML-Paragraph:\n");
    err += i18n("%1, Line: %2").tqarg(exc.message()).tqarg(exc.lineNumber());
    KMessageBox::error(0, err, i18n("Fatal error") );
    return false;
}


bool SaxHandler::resolveEntity(const TQString &publicId,
                    const TQString &systemId,
                    TQXmlInputSource* &ret)
{
    return true;
}


bool SaxHandler::externalEntityDecl(const TQString & name,
                    const TQString & publicId,
                    const TQString & systemId)
{
    kdDebug(100200) << "externalEntityDecl(): " << name << ", " << publicId << ", " << systemId << endl;
    return true;
}


bool SaxHandler::internalEntityDecl(const TQString & name,
                    const TQString & value)
{
    kdDebug() << "internalEntityDecl(): " << name << ", " << value << endl;
    return true;
}


bool SaxHandler::skippedEntity(const TQString &name)
{
    TQString warn = i18n("Unresolved entity found: %1.\n").tqarg(name);
    warn += i18n("KSayIt does not support DocBook files with external entities. ");
    warn += i18n("Parsing can continue, but the resulting text will contain gaps.");
    
    int res;   
    // TODO: "Option: don't show again this warning."
    res = KMessageBox::warningContinueCancel(0, warn, i18n("Parser problem") );
    if ( res == KMessageBox::Cancel )
        return false;
    return true;
}


void SaxHandler::getData( TQString &data ) const
{
    data = m_output;
}


void SaxHandler::reset()
{
    m_output = TQString();
}