2
This file is part of the KDE project
3
Copyright (C) 2001 Ewald Snel <ewald@rambo.its.tudelft.nl>
4
Copyright (C) 2001 Tomasz Grobelny <grotk@poczta.onet.pl>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
14
#include <qdatetime.h>
15
#include <qtextcodec.h>
20
#include "rtfimport_dom.h"
23
* Escape the special XML characters and be careful to catch any unallowed control character
25
QString CheckAndEscapeXmlText(const QString& strText)
27
QString strReturn(strText);
30
for (uint i=0; i<strReturn.length(); i++)
33
const int test = ch.unicode();
35
// The i+= is for the additional characters
36
if (test == 38) { strReturn.replace(i, 1, "&"); i+=4; } // &
37
else if (test == 60) { strReturn.replace(i, 1, "<"); i+=3; } // <
38
else if (test == 62) { strReturn.replace(i, 1, ">"); i+=3; } // >
39
else if (test == 34) { strReturn.replace(i, 1, """); i+=5; } // "
40
else if (test == 39) { strReturn.replace(i, 1, "'"); i+=5; } // '
41
else if (test >= 32) continue; // Normal character (from space on)
42
else if ((test == 9) || (test == 10) || (test == 13) ) continue; // Allowed control characters: TAB, LF, CR
45
// An unallowed control character:
46
// - could be a bug in the RTF file
47
// - could be not supported encoding.
48
// In any case, we must replace this character.
49
kdDebug(30515) << "Control character in XML stream: " << test << endl;
50
strReturn.replace(i, 1, '?'); // Replacement character
64
* Creates a new document.
65
* @param doctype the document type (tag)
67
DomNode::DomNode( const char *doctype )
71
hasAttributes = false;
72
str += "<?xml version = '1.0' encoding = 'UTF-8'?><!DOCTYPE " ;
79
* Creates a new document node (no memory allocation).
80
* @param level the document depth of the node
82
void DomNode::clear( int level )
85
documentLevel = level;
87
hasAttributes = false;
92
* @param name the name of the new node (tag)
94
void DomNode::addNode( const char *name )
105
* @param text the text to write into the document node
107
void DomNode::addTextNode( const char *text, QTextCodec* codec )
113
kdError(30515) << "No QTextCodec available!" << endl;
117
str += CheckAndEscapeXmlText(codec->toUnicode(text));
122
* Add border to existing frameset (see KWord DTD).
124
void DomNode::addBorder( int id, const QColor &color, int style, double width )
127
sprintf( attr, "%cRed", id );
128
setAttribute( attr, color.red() );
129
sprintf( attr, "%cGreen", id );
130
setAttribute( attr, color.green() );
131
sprintf( attr, "%cBlue", id );
132
setAttribute( attr, color.blue() );
133
sprintf( attr, "%cStyle", id );
134
setAttribute( attr, style );
135
sprintf( attr, "%cWidth", id );
136
setAttribute( attr, width );
140
* Add color attributes to document node.
141
* @param color the color
143
void DomNode::addColor( const QColor &color )
145
setAttribute( "red", color.red() );
146
setAttribute( "green", color.green() );
147
setAttribute( "blue", color.blue() );
151
* Add rectangle attributes to document node.
153
void DomNode::addRect( int left, int top, int right, int bottom )
155
setAttribute( "left", .05*left );
156
setAttribute( "top", .05*top );
157
setAttribute( "right", .05*right );
158
setAttribute( "bottom", .05*bottom );
162
* Add pixmap or clipart key.
163
* @param dt date/time
164
* @param filename the filename of the image
165
* @param name the relative path to the image in the store (optional)
167
void DomNode::addKey( const QDateTime& dt, const QString& filename, const QString& name )
169
const QDate date ( dt.date() );
170
const QTime time ( dt.time() );
173
setAttribute( "filename", CheckAndEscapeXmlText(filename) );
174
setAttribute( "year", date.year() );
175
setAttribute( "month", date.month() );
176
setAttribute( "day", date.day() );
177
setAttribute( "hour", time.hour() );
178
setAttribute( "minute", time.minute() );
179
setAttribute( "second", time.second() );
180
setAttribute( "msec", time.msec() );
184
setAttribute( "name", CheckAndEscapeXmlText(name) );
190
* Add frameset to document (see KWord DTD).
192
void DomNode::addFrameSet( const char *name, int frameType, int frameInfo )
194
addNode( "FRAMESET" );
195
setAttribute( "name", name );
196
setAttribute( "frameType", frameType );
197
setAttribute( "frameInfo", frameInfo );
198
setAttribute( "removable", 0 );
199
setAttribute( "visible", 1 );
203
* Add frame to existing frameset (see KWord DTD).
205
void DomNode::addFrame( int left, int top, int right, int bottom,
206
int autoCreateNewFrame, int newFrameBehaviour,
210
addRect( left, top, right, bottom );
211
setAttribute( "runaround", 1 );
212
setAttribute( "runaroundGap", 2 );
213
setAttribute( "autoCreateNewFrame", autoCreateNewFrame );
214
setAttribute( "newFrameBehaviour", newFrameBehaviour );
215
setAttribute( "sheetSide", sheetSide );
219
* Sets a new attribute to a string value.
221
void DomNode::setAttribute( const QString& attribute, const QString& value )
229
hasAttributes = true;
233
* Sets a new attribute to an integer value.
235
void DomNode::setAttribute( const char *attribute, int value )
238
sprintf( strvalue, "%d", value );
239
setAttribute( attribute, (const char *)strvalue );
243
* Sets a new attribute to a double value.
245
void DomNode::setAttribute( const char *attribute, double value )
248
sprintf( strvalue, "%f", value );
249
setAttribute( attribute, (const char *)strvalue );
253
* Closes a document node.
254
* @param name the node (tag) to close
256
void DomNode::closeNode( const char *name )
270
for (int i=documentLevel-1; i>0; i--)
278
* Closes the current XML tag (if open).
279
* @param nl add a newline
281
void DomNode::closeTag( bool nl )
291
for (int i=documentLevel-1; i>0; i--)
298
hasAttributes = false;
302
* Appends a child node.
303
* @param child the node to append to this document node
305
void DomNode::appendNode( const DomNode &child )
307
const QString childStr ( child.toString() );
308
closeTag( (childStr.length() >= 2 && (childStr[0] == '<' || childStr[1] == '<')) );
313
* Appends XML text to node
315
void DomNode::append( const QString& _str)
320
void DomNode::append( const QCString& cstr)
322
str += QString::fromUtf8(cstr);
325
void DomNode::append( const char ch)
331
* Returns true if node is empty.
333
bool DomNode::isEmpty( void ) const
335
return str.isEmpty();
339
* Returns the data of the document node.
341
QString DomNode::toString( void ) const