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
|
/***************************************************************************
dtdparser.cpp - description
-------------------
begin : Tue Jul 30 15:26:20 EEST 2002
copyright : (C) 2002 by Jason P. Hanley <jphanley@buffalo.edu>
(C) 2002, 2003 Andras Mantia <amantia@kde.org>
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqregexp.h>
#include <tqstringlist.h>
#include <tqdom.h>
#include <klocale.h>
#include <kurl.h>
#include <kdebug.h>
#include <kio/netaccess.h>
#include <kmessagebox.h>
#include <ktempfile.h>
#include "dtd.h"
#include "../quantacommon.h"
#include "../qextfileinfo.h"
DTD::DTD(const KURL &dtdURL, const TQString &dtepDir)
{
m_dtdURL = dtdURL;
m_dtepDir = dtepDir + "/"+TQFileInfo(dtdURL.fileName()).baseName(); //TODO: get the dir name from the DTD or from the user
}
DTD::~DTD()
{
}
TQStringList DTD::getTags()
{
return tags;
}
AttributeList* DTD::getTagAttributes(TQString tag)
{
return tagAttributes.find(tag);
}
TQStringList DTD::getTextCompletion(TQString tag)
{
return TQStringList();
}
void DTD::printContents()
{
for ( TQStringList::Iterator tagIt = tags.begin(); tagIt != tags.end(); ++tagIt ) {
TQString tag = *tagIt;
kdDebug(24000) << tag << endl;
AttributeList *attributes = getTagAttributes(tag);
for ( uint i = 0; i < attributes->count(); i++)
{
Attribute *attribute = attributes->at(i);
TQString s = " " + attribute->name + ": ";
for (uint j = 0; j < attribute->values.count(); j++)
{
s += attribute->values[j] + ", ";
}
kdDebug(24000) << s << endl;
}
}
}
void DTD::writeTagFiles()
{
TQString dirName = m_dtepDir;
KURL u;
u.setPath(dirName);
if (!QExtFileInfo::createDir(dirName)) {
QuantaCommon::dirCreationError(0, u);
return;
}
dirName.append("/");
for ( TQStringList::Iterator tagIt = tags.begin(); tagIt != tags.end(); ++tagIt ) {
TQString tag = *tagIt;
TQFile file( dirName + tag.lower() + ".tag" );
if ( file.open( IO_WriteOnly ) ) {
TQTextStream stream( &file );
stream.setEncoding(TQTextStream::UnicodeUTF8);
stream << "<!DOCTYPE TAGS>" << endl
<< "<TAGS>" << endl
<< "<tag name=\"" << tag << "\">" << endl << endl;
AttributeList *attributes = getTagAttributes(tag);
stream << QuantaCommon::xmlFromAttributes(attributes);
stream << "</tag>" << endl
<< "</TAGS>" << endl;
file.close();
} else {
kdDebug(24000) << "Unable to write tag file: " << file.name() << endl;
}
}
KConfig config(dirName + "description.rc");
config.setGroup("General");
config.writeEntry("Name", TQFileInfo(m_dtdURL.fileName()).baseName()); //TODO: get from the DTD!
config.writeEntry("NickName", TQFileInfo(m_dtdURL.fileName()).baseName()); //TODO: get from the user!
config.sync();
}
bool DTD::parseDTD(const KURL &url)
{
TQString fileName = TQString::null;
if (!KIO::NetAccess::download(url, fileName))
{
KMessageBox::error(0, i18n("<qt>Cannot download the DTD from <b>%1</b>.</qt>").arg(url.prettyURL(0, KURL::StripFileProtocol)));
return false;
}
TQFile file(fileName);
if (file.open(IO_ReadOnly))
{
TQTextStream fileStream(&file);
fileStream.setEncoding(TQTextStream::UnicodeUTF8);
TQString entireDTD = fileStream.read();
file.close();
removeComments(entireDTD);
TQString line;
TQStringList lines = TQStringList::split("\n",entireDTD);
TQStringList::Iterator it = lines.begin();
while (it != lines.end()) {
line = *it;
if (line.startsWith("<")) {
while (!line.endsWith(">") && it != lines.end()) {
++it;
line += " \\end" + *it;
}
} else if (line.startsWith("%")) {
while (!line.endsWith(";") && it != lines.end()) {
++it;
line += *it;
}
}
line = line.stripWhiteSpace();
line = line.simplifyWhiteSpace();
//kdDebug(24000) << "Parsed line is: " << line << endl;
if ( line.startsWith("<!ENTITY") && line.endsWith(">"))
{
parseDTDEntity(line);
}
else
if (line.startsWith("<!ELEMENT") && line.endsWith(">"))
{
parseDTDElement(line);
}
else
if (line.startsWith("<!ATTLIST") && line.endsWith(">"))
{
parseDTDAttlist(line);
}
else
if (line.startsWith("%") && line.endsWith(";"))
{
line.remove(0,1);
line.truncate(line.length()-1);
KURL entityURL = url;
entityURL.setPath(url.directory()+ "/" + line + ".ent");
parseDTD(entityURL);
} else
{
kdDebug(24000) << TQString("Unknown tag: [%1]").arg(line) << endl;
}
if (it != lines.end()) ++it;
}
}
}
void DTD::parseDTDEntity(TQString line) {
TQString name;
TQString *value;
line.replace("\\end", " ");
name = line.mid(11);
int firstSpace = name.find(' ');
name = name.remove(firstSpace, name.length()-firstSpace);
value = new TQString(line.mid(11+firstSpace));
value->remove(0, value->find("\"")+1);
value->remove(value->findRev("\""), value->length());
parseDTDReplace(value);
stripSpaces(value);
entities.insert(name, value);
//kdDebug() << "Entity --- Name: " << name << " --- Value: " << *value << endl;
}
void DTD::parseDTDElement(const TQString &l) {
TQString name;
TQString *value;
TQString line = l;
line.replace("\\end", " ");
name = line.mid(10);
int firstSpace = name.find(' ');
name.remove(firstSpace, name.length()-firstSpace);
value = new TQString(line.mid(10+firstSpace));
//value->remove(0, value->find("\"")+1);
value->remove(value->find(">"), 10000);
parseDTDReplace(&name);
parseDTDReplace(value);
if ( name.startsWith("(") && name.endsWith(")") ) {
name.remove(0,1);
name.remove(name.length()-1,1);
TQStringList multipleTags = TQStringList::split("|", name);
TQStringList::Iterator it = multipleTags.begin();
while(it != multipleTags.end()) {
name = *it;
name = name.stripWhiteSpace();
elements.insert(name, value);
tags.append(name);
//kdDebug() << "Element --- Name: " << name << " --- Value: " << *value << endl;
++it;
}
} else {
elements.insert(name, value);
tags.append(name);
//kdDebug() << "Element --- Name: " << name << " --- Value: " << *value << endl;
}
}
void DTD::parseDTDAttlist(const TQString &l) {
TQString name;
TQString *value;
TQString line = l;
line.replace("\\end", " ");
name = line.mid(10);
int firstSpace = name.find(' ');
name.remove(firstSpace, name.length()-firstSpace);
value = new TQString(line.mid(10+firstSpace));
//value->remove(0, value->find("\"")+1);
value->remove(value->find(">"), 10000);
parseDTDReplace(&name);
parseDTDReplace(value);
if ( name.startsWith("(") && name.endsWith(")") ) {
name.remove(0,1);
name.remove(name.length()-1,1);
TQStringList multipleTags = TQStringList::split("|", name);
TQStringList::Iterator it = multipleTags.begin();
while(it != multipleTags.end()) {
name = *it;
name = name.stripWhiteSpace();
//elements.insert(name, value);
parseTagAttributeValues(name, value);
//kdDebug() << "Attlist --- Name: " << name << " --- Value: " << *value << endl;
++it;
}
} else {
//elements.insert(name, value);
parseTagAttributeValues(name, value);
//kdDebug() << "Attlist --- Name: " << name << " --- Value: " << *value << endl;
}
}
void DTD::parseTagAttributeValues(const TQString &name, TQString *value) {
AttributeList *attributes = new AttributeList();
TQStringList attrLines = TQStringList::split("\\end",*value);
TQStringList::Iterator lineIt = attrLines.begin();
while (lineIt != attrLines.end()) //iterate through the attribute lines
{
//split the attribute line
TQStringList all = TQStringList::split(" ", *lineIt);
TQStringList::Iterator it = all.begin();
while(it != all.end())
{
Attribute *attr = new Attribute();
attr->name = *it;
//kdDebug() << "Inserting for tag " << name << ": " << *it << endl;
++it;
TQString values = *it;
//list of possible values
if ( values.startsWith("(") && values.endsWith(")") )
{
values.remove(0,1);
values.remove(values.length()-1,1);
attr->values = TQStringList::split("|", values);
TQString s = (attr->values[0]+attr->values[1]).lower();
stripSpaces(&s);
if ((s == "truefalse") || (s == "falsetrue"))
{
attr->type = "check";
} else
{
attr->type = "list";
}
} else
{
attr->values = values;
attr->type = "input";
}
//kdDebug() << " --- values: " << *it << endl;
if (it != all.end())
{
++it;
TQString s=*it;
if (s.startsWith("\"") && s.endsWith("\"") && it!=all.end())
{
s.remove(0,1);
s.remove(s.length()-1,1);
attr->defaultValue = s;
}
if (s.startsWith("#") && it != all.end())
{
s.remove(0,1);
attr->status = s.lower();
}
if (*it == "#FIXED" && it != all.end())
{
++it;
attr->values.append(*it);
}
}
if (it != all.end())
{
++it;
}
attributes->append(attr);
}
++lineIt;
}
tagAttributes.insert(name, attributes);
}
void DTD::parseDTDReplace(TQString *value) {
int begin, end;
begin = value->find("%");
end = value->find(";");
while (begin != -1 && end != -1) {
TQString replaceText = value->mid(begin+1, end-begin-1);
TQString *replaceValue = entities.find(replaceText);
if (replaceValue != 0L) {
value->replace(begin, end-begin+1, *replaceValue);
} else {
kdDebug(24000) << "Can not find entity: " << replaceText << endl;
return;
}
begin = value->find("%");
end = value->find(";");
}
}
void DTD::stripSpaces(TQString *value) {
int index=-1;
while ( (index=value->find(' ',++index)) != -1 ) {
if ( value->findRev('(',index) != -1 && value->find(')',index) != -1)
value->remove(index,1);
}
}
void DTD::removeComments(TQString &value) {
int begin, end;
begin = value.find("<!--");
end = value.find("-->",begin+2);
while (begin != -1 && end != -1) {
value.remove(begin, end-begin+3);
begin = value.find("<!--");
end = value.find("-->",begin+2);
}
begin = value.find("--");
end = value.find("--",begin+2);
while (begin != -1 && end != -1) {
value.remove(begin, end-begin+2);
begin = value.find("--");
end = value.find("--",begin+2);
}
value.replace(TQRegExp("<!>"), "");
}
bool DTD::parseDTD()
{
return parseDTD(m_dtdURL);
}
|