📄 qxml.cpp
字号:
/****************************************************************************** $Id: qt/src/xml/qxml.cpp 2.2.3 edited 2000-11-22 $**** Implementation of QXmlSimpleReader and related classes.**** Created : 000518**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the XML module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition licenses may use this** file in accordance with the Qt Commercial License Agreement provided** with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#define QT_XML_CPP#include "qxml.h"#include "qtextcodec.h"#include "qbuffer.h"#ifndef QT_NO_XML// NOT REVISED// Error strings for the XML reader#define XMLERR_OK "no error occured"#define XMLERR_TAGMISMATCH "tag mismatch"#define XMLERR_UNEXPECTEDEOF "unexpected end of file"#define XMLERR_FINISHEDPARSINGWHILENOTEOF "parsing is finished but end of file is not reached"#define XMLERR_LETTEREXPECTED "letter is expected"#define XMLERR_ERRORPARSINGELEMENT "error while parsing element"#define XMLERR_ERRORPARSINGPROLOG "error while parsing prolog"#define XMLERR_ERRORPARSINGMAINELEMENT "error while parsing main element"#define XMLERR_ERRORPARSINGCONTENT "error while parsing content"#define XMLERR_ERRORPARSINGNAME "error while parsing name"#define XMLERR_ERRORPARSINGNMTOKEN "error while parsing Nmtoken"#define XMLERR_ERRORPARSINGATTRIBUTE "error while parsing attribute"#define XMLERR_ERRORPARSINGMISC "error while parsing misc"#define XMLERR_ERRORPARSINGCHOICE "error while parsing choice or seq"#define XMLERR_ERRORBYCONSUMER "error triggered by consumer"#define XMLERR_UNEXPECTEDCHARACTER "unexpected character"#define XMLERR_EQUALSIGNEXPECTED "expected '=' but not found"#define XMLERR_QUOTATIONEXPECTED "expected \" or ' but not found"#define XMLERR_ERRORPARSINGREFERENCE "error while parsing reference"#define XMLERR_ERRORPARSINGPI "error while parsing processing instruction"#define XMLERR_ERRORPARSINGATTLISTDECL "error while parsing attribute list declaration"#define XMLERR_ERRORPARSINGATTTYPE "error while parsing attribute type declaration"#define XMLERR_ERRORPARSINGATTVALUE "error while parsing attribute value declaration"#define XMLERR_ERRORPARSINGELEMENTDECL "error while parsing element declaration"#define XMLERR_ERRORPARSINGENTITYDECL "error while parsing entity declaration"#define XMLERR_ERRORPARSINGNOTATIONDECL "error while parsing notation declaration"#define XMLERR_ERRORPARSINGEXTERNALID "error while parsing external id"#define XMLERR_ERRORPARSINGCOMMENT "error while parsing comment"#define XMLERR_ERRORPARSINGENTITYVALUE "error while parsing entity value declaration"#define XMLERR_CDSECTHEADEREXPECTED "expected the header for a cdata section"#define XMLERR_MORETHANONEDOCTYPE "more than one document type definition"#define XMLERR_ERRORPARSINGDOCTYPE "error while parsing document type definition"#define XMLERR_INVALIDNAMEFORPI "invalid name for processing instruction"#define XMLERR_VERSIONEXPECTED "version expected while reading the XML declaration"#define XMLERR_EDECLORSDDECLEXPECTED "EDecl or SDDecl expected while reading the XML declaration"#define XMLERR_SDDECLEXPECTED "SDDecl expected while reading the XML declaration"#define XMLERR_WRONGVALUEFORSDECL "wrong value for standalone declaration"#define XMLERR_UNPARSEDENTITYREFERENCE "unparsed entity reference in wrong context"#define XMLERR_INTERNALGENERALENTITYINDTD "internal general entity reference not allowed in DTD"#define XMLERR_EXTERNALGENERALENTITYINDTD "external parsed general entity reference not allowed in DTD"#define XMLERR_EXTERNALGENERALENTITYINAV "external parsed general entity reference not allowed in attribute value"// the constants for the lookup tablestatic const signed char cltWS = 0; // white spacestatic const signed char cltPer = 1; // %static const signed char cltAmp = 2; // &static const signed char cltGt = 3; // >static const signed char cltLt = 4; // <static const signed char cltSlash = 5; // /static const signed char cltQm = 6; // ?static const signed char cltEm = 7; // !static const signed char cltDash = 8; // -static const signed char cltCB = 9; // ]static const signed char cltOB = 10; // [static const signed char cltEq = 11; // =static const signed char cltDq = 12; // "static const signed char cltSq = 13; // 'static const signed char cltUnknown = 14;// character lookup tablestatic const signed char charLookupTable[256]={ cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x00 - 0x07 cltUnknown, // 0x08 cltWS, // 0x09 \t cltWS, // 0x0A \n cltUnknown, // 0x0B cltUnknown, // 0x0C cltWS, // 0x0D \r cltUnknown, // 0x0E cltUnknown, // 0x0F cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x17 - 0x16 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x18 - 0x1F cltWS, // 0x20 Space cltEm, // 0x21 ! cltDq, // 0x22 " cltUnknown, // 0x23 cltUnknown, // 0x24 cltPer, // 0x25 % cltAmp, // 0x26 & cltSq, // 0x27 ' cltUnknown, // 0x28 cltUnknown, // 0x29 cltUnknown, // 0x2A cltUnknown, // 0x2B cltUnknown, // 0x2C cltDash, // 0x2D - cltUnknown, // 0x2E cltSlash, // 0x2F / cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x30 - 0x37 cltUnknown, // 0x38 cltUnknown, // 0x39 cltUnknown, // 0x3A cltUnknown, // 0x3B cltLt, // 0x3C < cltEq, // 0x3D = cltGt, // 0x3E > cltQm, // 0x3F ? cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x40 - 0x47 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x48 - 0x4F cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x50 - 0x57 cltUnknown, // 0x58 cltUnknown, // 0x59 cltUnknown, // 0x5A cltOB, // 0x5B [ cltUnknown, // 0x5C cltCB, // 0x5D ] cltUnknown, // 0x5E cltUnknown, // 0x5F cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x60 - 0x67 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x68 - 0x6F cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x70 - 0x77 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x78 - 0x7F cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x80 - 0x87 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x88 - 0x8F cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x90 - 0x97 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0x98 - 0x9F cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xA0 - 0xA7 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xA8 - 0xAF cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xB0 - 0xB7 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xB8 - 0xBF cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xC0 - 0xC7 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xC8 - 0xCF cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xD0 - 0xD7 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xD8 - 0xDF cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xE0 - 0xE7 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xE8 - 0xEF cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, // 0xF0 - 0xF7 cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown, cltUnknown // 0xF8 - 0xFF};class QXmlNamespaceSupportPrivate{};class QXmlAttributesPrivate{};class QXmlInputSourcePrivate{};class QXmlParseExceptionPrivate{};class QXmlLocatorPrivate{};class QXmlDefaultHandlerPrivate{};#if defined(Q_FULL_TEMPLATE_INSTANTIATION)bool operator==( const QMap<QString, QString>, const QMap<QString, QString> ){ return FALSE;}#endif/*! \class QXmlParseException qxml.h \brief The QXmlParseException class is used to report errors with the QXmlErrorHandler interface. \module XML \sa QXmlErrorHandler*//*! \fn QXmlParseException::QXmlParseException( const QString& name, int c, int l, const QString& p, const QString& s ) Constructs a parse exception with the error string \a name in the column \a c and line \a l for the public identifier \a p and the system identifier \a s.*//*! Returns the error message.*/QString QXmlParseException::message() const{ return msg;}/*! Returns the column number the error occured.*/int QXmlParseException::columnNumber() const{ return column;}/*! Returns the line number the error occured.*/int QXmlParseException::lineNumber() const{ return line;}/*! Returns the public identifier the error occured.*/QString QXmlParseException::publicId() const{ return pub;}/*! Returns the system identifier the error occured.*/QString QXmlParseException::systemId() const{ return sys;}/*! \class QXmlLocator qxml.h \brief The QXmlLocator class provides the XML handler classes with information about the actual parsing position. \module XML The reader reports a QXmlLocator to the content handler before he starts to parse the document. This is done with the QXmlContentHandler::setDocumentLocator() function. The handler classes can now use this locator to get the actual position the reader is at.*//*! \fn QXmlLocator::QXmlLocator( QXmlSimpleReader* parent ) Constructor.*//*! \fn QXmlLocator::~QXmlLocator() Destructor.*//*! Gets the column number (starting with 1) or -1 if there is no column number available.*/int QXmlLocator::columnNumber(){ return ( reader->columnNr == -1 ? -1 : reader->columnNr + 1 );}/*! Gets the line number (starting with 1) or -1 if there is no line number available.*/int QXmlLocator::lineNumber(){ return ( reader->lineNr == -1 ? -1 : reader->lineNr + 1 );}/********************************************* * * QXmlNamespaceSupport * *********************************************//*! \class QXmlNamespaceSupport qxml.h \brief The QXmlNamespaceSupport class is a helper class for XML readers which want to include namespace support. \module XML It provides some functions that makes it easy to handle namespaces. Its main use is for subclasses of QXmlReader which want to provide namespace support. See also the <a href="xml-sax.html#namespaces">namespace description</a>.*//*! Constructs a QXmlNamespaceSupport.*/QXmlNamespaceSupport::QXmlNamespaceSupport(){ reset();}/*! Destructs a QXmlNamespaceSupport.*/QXmlNamespaceSupport::~QXmlNamespaceSupport(){}/*! This function declares a prefix in the current namespace context; the prefix will remain in force until this context is popped, unless it is shadowed in a descendant context. Note that there is an asymmetry in this library: while prefix() will not return the default "" prefix, even if you have declared one; to check for a default prefix, you have to look it up explicitly using uri(). This asymmetry exists to make it easier to look up prefixes for attribute names, where the default prefix is not allowed.*/void QXmlNamespaceSupport::setPrefix( const QString& pre, const QString& uri ){ if( pre.isNull() ) { ns.insert( "", uri ); } else { ns.insert( pre, uri ); }}/*! Returns one of the prefixes mapped to a namespace URI. If more than one prefix is currently mapped to the same URI, this function will make an arbitrary selection; if you want all of the prefixes, use the prefixes() function instead. Note: this will never return the empty (default) prefix; to check for a default prefix, use the uri() function with an argument of "".*/QString QXmlNamespaceSupport::prefix( const QString& uri ) const{ QMap<QString, QString>::ConstIterator itc, it = ns.begin(); while ( (itc=it) != ns.end() ) { ++it; if ( itc.data() == uri && !itc.key().isEmpty() ) return itc.key(); } return "";}/*! Looks up a prefix in the current context and returns the currently-mapped namespace URI. Use the empty string ("") for the default namespace.*/QString QXmlNamespaceSupport::uri( const QString& prefix ) const{ const QString& returi = ns[ prefix ]; return returi;}/*! Splits the name at the ':' and returns the prefix and the local name.*/void QXmlNamespaceSupport::splitName( const QString& qname, QString& prefix, QString& localname ) const{ uint pos; // search the ':' for( pos=0; pos<qname.length(); pos++ ) { if ( qname.at(pos) == ':' ) break; } // and split prefix = qname.left( pos ); localname = qname.mid( pos+1 );}/*! Processes a raw XML 1.0 name in the current context by removing the prefix and looking it up among the prefixes currently declared. First parameter is the raw XML 1.0 name to be processed. The second parameter is a flag wheter the name is the name of an attribute (TRUE) or not (FALSE). The return values will be stored in the last two parameters as follows: <ul> <li> The namespace URI, or an empty string if none is in use. <li> The local name (without prefix). </ul> If the raw name has a prefix that has not been declared, then the return value will be empty. Note that attribute names are processed differently than element names: an unprefixed element name will received the default namespace (if any), while an unprefixed element name will not*/void QXmlNamespaceSupport::processName( const QString& qname, bool isAttribute, QString& nsuri, QString& localname ) const{ uint pos; // search the ':' for( pos=0; pos<qname.length(); pos++ ) { if ( qname.at(pos) == ':' ) break; } if ( pos < qname.length() ) { // there was a ':' nsuri = uri( qname.left( pos ) ); localname = qname.mid( pos+1 ); } else { // there was no ':' if ( isAttribute ) { nsuri = ""; // attributes don't take default namespace } else { nsuri = uri( "" ); // get default namespace } localname = qname; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -