TagProcessing.cpp
1/*
2 This file is part of the KDE project
3 Copyright (C) 2001, 2002, 2004 Nicolas GOUTTE <goutte@kde.org>
4 Copyright (c) 2001 IABG mbH. All rights reserved.
5 Contact: Wolf-Michael Bolle <Bolle@IABG.de>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21*/
22
23/*
24 This file is based on the old file:
25 /home/kde/koffice/filters/kword/ascii/asciiexport.cpp
26
27 The old file was copyrighted by
28 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
29 Copyright (c) 2000 ID-PRO Deutschland GmbH. All rights reserved.
30 Contact: Wolf-Michael Bolle <Bolle@GMX.de>
31
32 The old file was licensed under the terms of the GNU Library General Public
33 License version 2.
34*/
35
36#include <kdebug.h>
37#include <tqdom.h>
38
39#include "TagProcessing.h"
40
41#define DEBUG_KWORD_TAGS
42// #define DEBUG_KWORD_IGNORED_TAGS
43
44
45void ProcessSubtags ( const TQDomNode &parentNode,
46 TQValueList<TagProcessing> &tagProcessingList,
47 KWEFKWordLeader *leader)
48{
49 //kdDebug(30508) << "Starting ProcessSubtags for node: " << parentNode.nodeName() << endl;
50
51 TQDomNode childNode;
52
53 for ( childNode = parentNode.firstChild (); !childNode.isNull (); childNode = childNode.nextSibling () )
54 {
55 if ( childNode.isElement () )
56 {
57 bool found = false;
58
59 TQValueList<TagProcessing>::Iterator tagProcessingIt;
60
61 for ( tagProcessingIt = tagProcessingList.begin ();
62 tagProcessingIt != tagProcessingList.end ();
63 tagProcessingIt++ )
64 {
65 if ( childNode.nodeName () == (*tagProcessingIt).name )
66 {
67 found = true;
68
69 if ( (*tagProcessingIt).processor != NULL )
70 {
71 ((*tagProcessingIt).processor) ( childNode, (*tagProcessingIt).data, leader );
72 }
73#ifdef DEBUG_KWORD_IGNORED_TAGS
74 else
75 {
76 kdDebug(30508) << "Ignoring " << childNode.nodeName ()
77 << " tag in " << parentNode.nodeName () << endl;
78 }
79#endif
80 break;
81 }
82 }
83
84 if ( !found )
85 {
86 kdDebug(30508) << "Unexpected tag " << childNode.nodeName ()
87 << " in " << parentNode.nodeName () << "!" << endl;
88 }
89 }
90 }
91 //kdDebug(30508) << "Ending ProcessSubtags for node: " << parentNode.nodeName() << endl;
92}
93
94void AllowNoSubtags ( const TQDomNode& myNode, KWEFKWordLeader *leader )
95{
96#ifdef DEBUG_KWORD_TAGS
97 TQString outputText;
98 TQValueList<TagProcessing> tagProcessingList;
99 ProcessSubtags (myNode, tagProcessingList, leader);
100#else
101 @_UNUSED( leader ):
102#endif
103}
104
105AttrProcessing::AttrProcessing ( const TQString& n, const TQString& t, void *d )
106 : name (n), data (d)
107{
108 if ( t == "int" )
109 type = AttrInt;
110 else if ( t == "TQString" )
111 type = AttrTQString;
112 else if ( t == "double" )
113 type = AttrDouble;
114 else if ( t == "bool" )
115 type = AttrBool;
116 else if ( t.isEmpty() )
117 type = AttrNull;
118 else
119 {
120 kdWarning(30508) << "Unknown type: " << t << " for element " << n << " assuming NULL" << endl;
121 type = AttrNull;
122 }
123}
124
125
126void ProcessAttributes ( const TQDomNode &myNode,
127 TQValueList<AttrProcessing> &attrProcessingList )
128{
129 //kdDebug(30508) << "Starting ProcessAttributes for node: " << myNode.nodeName() << endl;
130
131 TQDomNamedNodeMap myAttribs ( myNode.attributes () );
132 //kdDebug(30508) << "Attributes = " << myAttribs.length () <<endl;
133 for ( uint i = 0; i < myAttribs.length (); i++ )
134 {
135 TQDomAttr myAttrib ( myAttribs.item (i).toAttr () );
136
137 if ( !myAttrib.isNull () )
138 {
139 bool found = false;
140
141 TQValueList<AttrProcessing>::Iterator attrProcessingIt;
142
143 for ( attrProcessingIt = attrProcessingList.begin ();
144 attrProcessingIt != attrProcessingList.end ();
145 attrProcessingIt++ )
146 {
147 //kdDebug(30508) << "NAME: " << myAttrib.name () << " == " << (*attrProcessingIt).name <<endl;
148 if ( myAttrib.name () == (*attrProcessingIt).name )
149 {
150 found = true;
151
152 if ( (*attrProcessingIt).data != NULL )
153 {
154 switch ( (*attrProcessingIt).type )
155 {
156 case AttrProcessing::AttrTQString:
157 {
158 *((TQString *) (*attrProcessingIt).data) = myAttrib.value ();
159 break;
160 }
161 case AttrProcessing::AttrInt:
162 {
163 *((int *) (*attrProcessingIt).data) = myAttrib.value ().toInt ();
164 break;
165 }
166 case AttrProcessing::AttrDouble:
167 {
168 *((double *) (*attrProcessingIt).data) = myAttrib.value ().toDouble ();
169 break;
170 }
171 case AttrProcessing::AttrBool:
172 {
173 const TQString strAttr ( myAttrib.value().simplifyWhiteSpace() );
174 bool flag;
175 if ((strAttr=="yes")||(strAttr=="1")||(strAttr=="true"))
176 {
177 flag=true;
178 }
179 else if ((strAttr=="no")||(strAttr=="0")||(strAttr=="false"))
180 {
181 flag=false;
182 }
183 else
184 {
185 flag=false;
186 kdWarning(30508) << "Unknown value for a boolean: " << strAttr
187 << " in tag " << myNode.nodeName () << ", attribute "
188 << myAttrib.name() << endl;
189 }
190 *((bool *) (*attrProcessingIt).data) = flag;
191 break;
192 }
193 case AttrProcessing::AttrNull:
194 break;
195 default:
196 {
197 kdDebug(30508) << "Unexpected data type " << int( (*attrProcessingIt).type )
198 << " in " << myNode.nodeName ()
199 << " attribute " << (*attrProcessingIt).name
200 << endl;
201 break;
202 }
203 }
204 }
205#ifdef DEBUG_KWORD_IGNORED_TAGS
206 else
207 {
208 kdDebug(30508) << "Ignoring " << myNode.nodeName()
209 << " attribute " << (*attrProcessingIt).name
210 << endl;
211 }
212#endif
213 break;
214 }
215 }
216
217 if ( !found )
218 {
219 kdWarning(30508) << "Unexpected attribute " << myAttrib.name ()
220 << " in " << myNode.nodeName () << "!" << endl;
221 }
222 }
223 }
224 //kdDebug(30508) << "Ending ProcessAttributes for node: " << myNode.nodeName() << endl;
225}
226
227void AllowNoAttributes ( const TQDomNode & myNode )
228{
229#ifdef DEBUG_KWORD_TAGS
230 TQValueList<AttrProcessing> attrProcessingList;
231 ProcessAttributes (myNode, attrProcessingList);
232#else
233 Q_UNUSED( myNode );
234#endif
235}
The class AttrProcessing and the two functions ProcessAttributes () and AllowNoSubtags () allow for e...