Imported Upstream version 1.8.2
[platform/upstream/doxygen.git] / qtools / qxml.h
1 /****************************************************************************
2 ** 
3 **
4 ** Definition of QXmlSimpleReader and related classes.
5 **
6 ** Created : 000518
7 **
8 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
9 **
10 ** This file is part of the XML module of the Qt GUI Toolkit.
11 **
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
15 **
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
20 **
21 ** Licensees holding valid Qt Enterprise Edition licenses may use this
22 ** file in accordance with the Qt Commercial License Agreement provided
23 ** with the Software.
24 **
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 **
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 **   information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
32 **
33 ** Contact info@trolltech.com if any conditions of this licensing are
34 ** not clear to you.
35 **
36 **********************************************************************/
37
38 #ifndef QXML_H
39 #define QXML_H
40
41 #include <qmodules.h>
42
43 #if !defined(QT_MODULE_XML)
44 #define QM_EXPORT
45 #else
46 #define QM_EXPORT Q_EXPORT
47 #endif
48
49 #ifndef QT_H
50 #include <qtextstream.h>
51 #include <qfile.h>
52 #include <qstring.h>
53 #include <qstringlist.h>
54 #include <qvaluestack.h>
55 #include <qmap.h>
56 #endif // QT_H
57
58 #ifndef QT_NO_XML
59
60 class QXmlNamespaceSupport;
61 class QXmlAttributes;
62 class QXmlContentHandler;
63 class QXmlDefaultHandler;
64 class QXmlDTDHandler;
65 class QXmlEntityResolver;
66 class QXmlErrorHandler;
67 class QXmlLexicalHandler;
68 class QXmlDeclHandler;
69 class QXmlInputSource;
70 class QXmlLocator;
71 class QXmlNamespaceSupport;
72 class QXmlParseException;
73
74 class QXmlReader;
75 class QXmlSimpleReader;
76
77 class QXmlSimpleReaderPrivate;
78 class QXmlNamespaceSupportPrivate;
79 class QXmlAttributesPrivate;
80 class QXmlInputSourcePrivate;
81 class QXmlParseExceptionPrivate;
82 class QXmlLocatorPrivate;
83 class QXmlDefaultHandlerPrivate;
84
85
86 //
87 // SAX Namespace Support
88 //
89
90 #if defined(Q_TEMPLATEDLL)
91 // MOC_SKIP_BEGIN
92 template class QM_EXPORT QMap<QString, QString>;
93 template class QM_EXPORT QValueStack<QMap<QString, QString> >;
94 template class QM_EXPORT QValueStack<QString>;
95 // MOC_SKIP_END
96 #endif
97
98 class QM_EXPORT QXmlNamespaceSupport
99 {
100 public:
101     QXmlNamespaceSupport();
102     ~QXmlNamespaceSupport();
103
104     void setPrefix( const QString&, const QString& );
105
106     QString prefix( const QString& ) const;
107     QString uri( const QString& ) const;
108     void splitName( const QString&, QString&, QString& ) const;
109     void processName( const QString&, bool, QString&, QString& ) const;
110     QStringList prefixes() const;
111     QStringList prefixes( const QString& ) const;
112
113     void pushContext();
114     void popContext();
115     void reset();
116 private:
117     QValueStack<QMap<QString, QString> > nsStack;
118     QMap<QString, QString> ns;
119
120     QXmlNamespaceSupportPrivate *d;
121 };
122
123
124 //
125 // SAX Attributes
126 //
127
128 class QM_EXPORT QXmlAttributes
129 {
130 public:
131     QXmlAttributes() {}
132     virtual ~QXmlAttributes() {}
133
134     int index( const QString& qName ) const;
135     int index( const QString& uri, const QString& localPart ) const;
136     int length() const;
137     QString localName( int index ) const;
138     QString qName( int index ) const;
139     QString uri( int index ) const;
140     QString type( int index ) const;
141     QString type( const QString& qName ) const;
142     QString type( const QString& uri, const QString& localName ) const;
143     QString value( int index ) const;
144     QString value( const QString& qName ) const;
145     QString value( const QString& uri, const QString& localName ) const;
146
147 private:
148     QStringList qnameList;
149     QStringList uriList;
150     QStringList localnameList;
151     QStringList valueList;
152
153     QXmlAttributesPrivate *d;
154
155     friend class QXmlSimpleReader;
156 };
157
158 //
159 // SAX Input Source
160 //
161
162 class QM_EXPORT QXmlInputSource
163 {
164 public:
165     QXmlInputSource();
166     QXmlInputSource( QTextStream& stream );
167     QXmlInputSource( QFile& file );
168     virtual ~QXmlInputSource();
169
170     virtual const QString& data() const;
171     virtual void setData( const QString& d );
172
173 private:
174     void readInput( QByteArray& rawData );
175
176     QString input;
177
178     QXmlInputSourcePrivate *d;
179 };
180
181 //
182 // SAX Exception Classes
183 //
184
185 class QM_EXPORT QXmlParseException
186 {
187 public:
188     QXmlParseException( const QString& name="", int c=-1, int l=-1, const QString& p="", const QString& s="" )
189         : msg( name ), column( c ), line( l ), pub( p ), sys( s )
190     { }
191
192     int columnNumber() const;
193     int lineNumber() const;
194     QString publicId() const;
195     QString systemId() const;
196     QString message() const;
197
198 private:
199     QString msg;
200     int column;
201     int line;
202     QString pub;
203     QString sys;
204
205     QXmlParseExceptionPrivate *d;
206 };
207
208
209 //
210 // XML Reader
211 //
212
213 class QM_EXPORT QXmlReader
214 {
215 public:
216     virtual ~QXmlReader() {}
217     virtual bool feature( const QString& name, bool *ok = 0 ) const = 0;
218     virtual void setFeature( const QString& name, bool value ) = 0;
219     virtual bool hasFeature( const QString& name ) const = 0;
220     virtual void* property( const QString& name, bool *ok = 0 ) const = 0;
221     virtual void setProperty( const QString& name, void* value ) = 0;
222     virtual bool hasProperty( const QString& name ) const = 0;
223     virtual void setEntityResolver( QXmlEntityResolver* handler ) = 0;
224     virtual QXmlEntityResolver* entityResolver() const = 0;
225     virtual void setDTDHandler( QXmlDTDHandler* handler ) = 0;
226     virtual QXmlDTDHandler* DTDHandler() const = 0;
227     virtual void setContentHandler( QXmlContentHandler* handler ) = 0;
228     virtual QXmlContentHandler* contentHandler() const = 0;
229     virtual void setErrorHandler( QXmlErrorHandler* handler ) = 0;
230     virtual QXmlErrorHandler* errorHandler() const = 0;
231     virtual void setLexicalHandler( QXmlLexicalHandler* handler ) = 0;
232     virtual QXmlLexicalHandler* lexicalHandler() const = 0;
233     virtual void setDeclHandler( QXmlDeclHandler* handler ) = 0;
234     virtual QXmlDeclHandler* declHandler() const = 0;
235     virtual bool parse( const QXmlInputSource& input ) = 0;
236 };
237
238 class QM_EXPORT QXmlSimpleReader : public QXmlReader
239 {
240 public:
241     QXmlSimpleReader();
242     virtual ~QXmlSimpleReader();
243
244     bool feature( const QString& name, bool *ok = 0 ) const;
245     void setFeature( const QString& name, bool value );
246     bool hasFeature( const QString& name ) const;
247
248     void* property( const QString& name, bool *ok = 0 ) const;
249     void setProperty( const QString& name, void* value );
250     bool hasProperty( const QString& name ) const;
251
252     void setEntityResolver( QXmlEntityResolver* handler );
253     QXmlEntityResolver* entityResolver() const;
254     void setDTDHandler( QXmlDTDHandler* handler );
255     QXmlDTDHandler* DTDHandler() const;
256     void setContentHandler( QXmlContentHandler* handler );
257     QXmlContentHandler* contentHandler() const;
258     void setErrorHandler( QXmlErrorHandler* handler );
259     QXmlErrorHandler* errorHandler() const;
260     void setLexicalHandler( QXmlLexicalHandler* handler );
261     QXmlLexicalHandler* lexicalHandler() const;
262     void setDeclHandler( QXmlDeclHandler* handler );
263     QXmlDeclHandler* declHandler() const;
264
265     bool parse( const QXmlInputSource& input );
266
267 private:
268     // variables
269     QXmlContentHandler* contentHnd;
270     QXmlErrorHandler*   errorHnd;
271     QXmlDTDHandler*     dtdHnd;
272     QXmlEntityResolver* entityRes;
273     QXmlLexicalHandler* lexicalHnd;
274     QXmlDeclHandler*    declHnd;
275
276     QChar c; // the character at reading position
277     int lineNr; // number of line
278     int columnNr; // position in line
279     int pos; // position in string
280
281     int     namePos;
282     QChar   nameArray[256]; // only used for names
283     QString nameValue; // only used for names
284     int     refPos;
285     QChar   refArray[256]; // only used for references
286     QString refValue; // only used for references
287     int     stringPos;
288     QChar   stringArray[256]; // used for any other strings that are parsed
289     QString stringValue; // used for any other strings that are parsed
290
291     QString xml;
292     int xmlLength;
293     QString xmlRef; // used for parsing of entity references
294
295     QValueStack<QString> tags;
296
297     QXmlSimpleReaderPrivate* d;
298
299     static const QChar QEOF;
300
301     // inlines
302     virtual bool is_S( const QChar& );
303     virtual bool is_Letter( const QChar& );
304     virtual bool is_NameBeginning( const QChar& );
305     virtual bool is_Digit( const QChar& );
306     virtual bool is_CombiningChar( const QChar& );
307     virtual bool is_Extender( const QChar& );
308     virtual bool is_NameChar( const QChar& );
309
310     QString& string();
311     void stringClear();
312     void stringAddC();
313     void stringAddC(const QChar&);
314     QString& name();
315     void nameClear();
316     void nameAddC();
317     void nameAddC(const QChar&);
318     QString& ref();
319     void refClear();
320     void refAddC();
321     void refAddC(const QChar&);
322
323     // used by parseReference() and parsePEReference()
324     enum EntityRecognitionContext { InContent, InAttributeValue, InEntityValue, InDTD };
325
326     // private functions
327     void eat_ws();
328     void next_eat_ws();
329
330     void next();
331     bool atEnd();
332
333     void init( const QXmlInputSource& i );
334
335     bool entityExist( const QString& ) const;
336
337     bool parseProlog();
338     bool parseElement();
339     bool parseElementEmptyTag( bool &t, QString &uri, QString &lname );
340     bool parseElementETagBegin2( QString &uri, QString &lname );
341     bool parseElementAttribute( QString &prefix, QString &uri, QString &lname );
342     bool parseMisc();
343     bool parseContent();
344
345     bool parsePI(bool xmldecl=FALSE);
346     bool parseDoctype();
347     bool parseComment();
348
349     bool parseName( bool useRef=FALSE );
350     bool parseNmtoken();
351     bool parseAttribute();
352     bool parseReference( bool &charDataRead, EntityRecognitionContext context );
353     bool processReference( bool &charDataRead, EntityRecognitionContext context );
354
355     bool parseExternalID( bool allowPublicID = FALSE );
356     bool parsePEReference( EntityRecognitionContext context );
357     bool parseMarkupdecl();
358     bool parseAttlistDecl();
359     bool parseAttType();
360     bool parseAttValue();
361     bool parseElementDecl();
362     bool parseNotationDecl();
363     bool parseChoiceSeq();
364     bool parseEntityDecl();
365     bool parseEntityValue();
366
367     bool parseString( const QString& s );
368
369     void reportParseError();
370
371     friend class QXmlSimpleReaderPrivate;
372     friend class QXmlLocator;
373 };
374
375 //
376 // SAX Locator
377 //
378
379 class QM_EXPORT QXmlLocator
380 {
381 public:
382     QXmlLocator( QXmlSimpleReader* parent )
383     { reader = parent; }
384     ~QXmlLocator()
385     { }
386
387     int columnNumber();
388     int lineNumber();
389 //    QString getPublicId()
390 //    QString getSystemId()
391
392 private:
393     QXmlSimpleReader* reader;
394
395     QXmlLocatorPrivate *d;
396 };
397
398 //
399 // SAX handler classes
400 //
401
402 class QM_EXPORT QXmlContentHandler
403 {
404 public:
405     virtual ~QXmlContentHandler() {}
406     virtual void setDocumentLocator( QXmlLocator* locator ) = 0;
407     virtual bool startDocument() = 0;
408     virtual bool endDocument() = 0;
409     virtual bool startPrefixMapping( const QString& prefix, const QString& uri ) = 0;
410     virtual bool endPrefixMapping( const QString& prefix ) = 0;
411     virtual bool startElement( const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts ) = 0;
412     virtual bool endElement( const QString& namespaceURI, const QString& localName, const QString& qName ) = 0;
413     virtual bool characters( const QString& ch ) = 0;
414     virtual bool ignorableWhitespace( const QString& ch ) = 0;
415     virtual bool processingInstruction( const QString& target, const QString& data ) = 0;
416     virtual bool skippedEntity( const QString& name ) = 0;
417     virtual QString errorString() = 0;
418 };
419
420 class QM_EXPORT QXmlErrorHandler
421 {
422 public:
423     virtual ~QXmlErrorHandler() {}
424     virtual bool warning( const QXmlParseException& exception ) = 0;
425     virtual bool error( const QXmlParseException& exception ) = 0;
426     virtual bool fatalError( const QXmlParseException& exception ) = 0;
427     virtual QString errorString() = 0;
428 };
429
430 class QM_EXPORT QXmlDTDHandler
431 {
432 public:
433     virtual ~QXmlDTDHandler() {}
434     virtual bool notationDecl( const QString& name, const QString& publicId, const QString& systemId ) = 0;
435     virtual bool unparsedEntityDecl( const QString& name, const QString& publicId, const QString& systemId, const QString& notationName ) = 0;
436     virtual QString errorString() = 0;
437 };
438
439 class QM_EXPORT QXmlEntityResolver
440 {
441 public:
442     virtual ~QXmlEntityResolver() {}
443     virtual bool resolveEntity( const QString& publicId, const QString& systemId, QXmlInputSource* &ret ) = 0;
444     virtual QString errorString() = 0;
445 };
446
447 class QM_EXPORT QXmlLexicalHandler
448 {
449 public:
450     virtual ~QXmlLexicalHandler() {}
451     virtual bool startDTD( const QString& name, const QString& publicId, const QString& systemId ) = 0;
452     virtual bool endDTD() = 0;
453 //    virtual bool startEntity( const QString& name ) = 0;
454 //    virtual bool endEntity( const QString& name ) = 0;
455     virtual bool startCDATA() = 0;
456     virtual bool endCDATA() = 0;
457     virtual bool comment( const QString& ch ) = 0;
458     virtual QString errorString() = 0;
459 };
460
461 class QM_EXPORT QXmlDeclHandler
462 {
463 public:
464     virtual ~QXmlDeclHandler() {}
465     virtual bool attributeDecl( const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value ) = 0;
466     virtual bool internalEntityDecl( const QString& name, const QString& value ) = 0;
467     virtual bool externalEntityDecl( const QString& name, const QString& publicId, const QString& systemId ) = 0;
468     virtual QString errorString() = 0;
469 };
470
471
472 class QM_EXPORT QXmlDefaultHandler : public QXmlContentHandler, public QXmlErrorHandler, public QXmlDTDHandler, public QXmlEntityResolver, public QXmlLexicalHandler, public QXmlDeclHandler
473 {
474 public:
475     QXmlDefaultHandler() { }
476     virtual ~QXmlDefaultHandler() { }
477
478     void setDocumentLocator( QXmlLocator* locator );
479     bool startDocument();
480     bool endDocument();
481     bool startPrefixMapping( const QString& prefix, const QString& uri );
482     bool endPrefixMapping( const QString& prefix );
483     bool startElement( const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts );
484     bool endElement( const QString& namespaceURI, const QString& localName, const QString& qName );
485     bool characters( const QString& ch );
486     bool ignorableWhitespace( const QString& ch );
487     bool processingInstruction( const QString& target, const QString& data );
488     bool skippedEntity( const QString& name );
489
490     bool warning( const QXmlParseException& exception );
491     bool error( const QXmlParseException& exception );
492     bool fatalError( const QXmlParseException& exception );
493
494     bool notationDecl( const QString& name, const QString& publicId, const QString& systemId );
495     bool unparsedEntityDecl( const QString& name, const QString& publicId, const QString& systemId, const QString& notationName );
496
497     bool resolveEntity( const QString& publicId, const QString& systemId, QXmlInputSource* &ret );
498
499     bool startDTD( const QString& name, const QString& publicId, const QString& systemId );
500     bool endDTD();
501 //    bool startEntity( const QString& name );
502 //    bool endEntity( const QString& name );
503     bool startCDATA();
504     bool endCDATA();
505     bool comment( const QString& ch );
506
507     bool attributeDecl( const QString& eName, const QString& aName, const QString& type, const QString& valueDefault, const QString& value );
508     bool internalEntityDecl( const QString& name, const QString& value );
509     bool externalEntityDecl( const QString& name, const QString& publicId, const QString& systemId );
510
511     QString errorString();
512
513 private:
514     QXmlDefaultHandlerPrivate *d;
515 };
516
517 #ifdef _WS_QWS_
518 #ifdef QT_XML_CPP
519 #define inline
520 #else
521 #define QT_NO_XML_INLINE
522 #endif
523 #endif
524
525 #ifndef QT_NO_XML_INLINE
526 //
527 // inlines
528 //
529
530 inline bool QXmlSimpleReader::is_S(const QChar& ch)
531 { return ch==' ' || ch=='\t' || ch=='\n' || ch=='\r'; }
532
533 inline bool QXmlSimpleReader::is_Letter( const QChar& ch )
534 { return ch.isLetter(); }
535
536 inline bool QXmlSimpleReader::is_NameBeginning( const QChar& ch )
537 { return ch=='_' || ch==':' || ch.isLetter(); }
538
539 inline bool QXmlSimpleReader::is_Digit( const QChar& ch )
540 { return ch.isDigit(); }
541
542 inline bool QXmlSimpleReader::is_CombiningChar( const QChar& )
543 { return FALSE; }
544
545 inline bool QXmlSimpleReader::is_Extender( const QChar& )
546 { return FALSE; }
547
548 inline bool QXmlSimpleReader::is_NameChar( const QChar& ch )
549 {
550     return ch=='.' || ch=='-' || ch=='_' || ch==':' ||
551         is_Letter(ch) || is_Digit(ch) ||
552         is_CombiningChar(ch) || is_Extender(ch);
553 }
554
555 inline void QXmlSimpleReader::next()
556 {
557     if ( !xmlRef.isEmpty() ) {
558         c = xmlRef[0];
559         xmlRef.remove( 0, 1 );
560     } else {
561         if ( c=='\n' || c=='\r' ) {
562             lineNr++;
563             columnNr = -1;
564         }
565         if ( pos >= xmlLength ) {
566             c = QEOF;
567         } else {
568             c = xml[pos];
569             columnNr++;
570             pos++;
571         }
572     }
573 }
574
575 inline bool QXmlSimpleReader::atEnd()
576 { return c == QEOF; }
577
578 inline void QXmlSimpleReader::eat_ws()
579 { while ( !atEnd() && is_S(c) ) next(); }
580
581 inline void QXmlSimpleReader::next_eat_ws()
582 { next(); eat_ws(); }
583
584
585 // use buffers instead of QString::operator+= when single characters are read
586 inline QString& QXmlSimpleReader::string()
587 {
588     stringValue += QString( stringArray, stringPos );
589     stringPos = 0;
590     return stringValue;
591 }
592 inline QString& QXmlSimpleReader::name()
593 {
594     nameValue += QString( nameArray, namePos );
595     namePos = 0;
596     return nameValue;
597 }
598 inline QString& QXmlSimpleReader::ref()
599 {
600     refValue += QString( refArray, refPos );
601     refPos = 0;
602     return refValue;
603 }
604
605 inline void QXmlSimpleReader::stringClear()
606 { stringValue = ""; stringPos = 0; }
607 inline void QXmlSimpleReader::nameClear()
608 { nameValue = ""; namePos = 0; }
609 inline void QXmlSimpleReader::refClear()
610 { refValue = ""; refPos = 0; }
611
612 inline void QXmlSimpleReader::stringAddC()
613 {
614     if ( stringPos >= 256 ) {
615         stringValue += QString( stringArray, stringPos );
616         stringPos = 0;
617     }
618     stringArray[stringPos++] = c;
619 }
620 inline void QXmlSimpleReader::nameAddC()
621 {
622     if ( namePos >= 256 ) {
623         nameValue += QString( nameArray, namePos );
624         namePos = 0;
625     }
626     nameArray[namePos++] = c;
627 }
628 inline void QXmlSimpleReader::refAddC()
629 {
630     if ( refPos >= 256 ) {
631         refValue += QString( refArray, refPos );
632         refPos = 0;
633     }
634     refArray[refPos++] = c;
635 }
636
637 inline void QXmlSimpleReader::stringAddC(const QChar& ch)
638 {
639     if ( stringPos >= 256 ) {
640         stringValue += QString( stringArray, stringPos );
641         stringPos = 0;
642     }
643     stringArray[stringPos++] = ch;
644 }
645 inline void QXmlSimpleReader::nameAddC(const QChar& ch)
646 {
647     if ( namePos >= 256 ) {
648         nameValue += QString( nameArray, namePos );
649         namePos = 0;
650     }
651     nameArray[namePos++] = ch;
652 }
653 inline void QXmlSimpleReader::refAddC(const QChar& ch)
654 {
655     if ( refPos >= 256 ) {
656         refValue += QString( refArray, refPos );
657         refPos = 0;
658     }
659     refArray[refPos++] = ch;
660 }
661 #endif
662
663 #ifdef _WS_QWS_
664 #ifdef QT_XML_CPP
665 #undef inline
666 #endif
667 #endif
668
669 #endif //QT_NO_XML
670
671 #endif