Qt 4.8
Public Functions | Properties | Friends | List of all members
QXmlNamespaceSupport Class Reference

The QXmlNamespaceSupport class is a helper class for XML readers which want to include namespace support. More...

#include <qxml.h>

Public Functions

void popContext ()
 Reverts to the previous namespace context. More...
 
QString prefix (const QString &) const
 Returns one of the prefixes mapped to the namespace URI uri. More...
 
QStringList prefixes () const
 Returns a list of all the prefixes currently declared. More...
 
QStringList prefixes (const QString &) const
 Returns a list of all prefixes currently declared for the namespace URI uri. More...
 
void processName (const QString &, bool, QString &, QString &) const
 Processes a raw XML 1.0 name in the current context by removing the prefix and looking it up among the prefixes currently declared. More...
 
void pushContext ()
 Starts a new namespace context. More...
 
 QXmlNamespaceSupport ()
 Constructs a QXmlNamespaceSupport. More...
 
void reset ()
 Resets this namespace support object ready for reuse. More...
 
void setPrefix (const QString &, const QString &)
 This function declares a prefix pre in the current namespace context to be the namespace URI uri. More...
 
void splitName (const QString &, QString &, QString &) const
 Splits the name qname at the ':' and returns the prefix in prefix and the local name in localname. More...
 
QString uri (const QString &) const
 Looks up the prefix prefix in the current context and returns the currently-mapped namespace URI. More...
 
 ~QXmlNamespaceSupport ()
 Destroys a QXmlNamespaceSupport. More...
 

Properties

QXmlNamespaceSupportPrivated
 

Friends

class QXmlSimpleReaderPrivate
 

Detailed Description

The QXmlNamespaceSupport class is a helper class for XML readers which want to include namespace support.

Since
4.4
Note
This class or function is reentrant.
Attention
Module: QtXml

You can set the prefix for the current namespace with setPrefix(), and get the list of current prefixes (or those for a given URI) with prefixes(). The namespace URI is available from uri(). Use pushContext() to start a new namespace context, and popContext() to return to the previous namespace context. Use splitName() or processName() to split a name into its prefix and local name.

See also
{Namespace Support via Features}

Definition at line 88 of file qxml.h.

Constructors and Destructors

◆ QXmlNamespaceSupport()

QXmlNamespaceSupport::QXmlNamespaceSupport ( )

Constructs a QXmlNamespaceSupport.

Definition at line 765 of file qxml.cpp.

766 {
768 }
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108

◆ ~QXmlNamespaceSupport()

QXmlNamespaceSupport::~QXmlNamespaceSupport ( )

Destroys a QXmlNamespaceSupport.

Definition at line 773 of file qxml.cpp.

774 {
775  delete d;
776 }
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108

Functions

◆ popContext()

void QXmlNamespaceSupport::popContext ( )

Reverts to the previous namespace context.

Normally, you should pop the context at the end of each XML element. After popping the context, all namespace prefix mappings that were previously in force are restored.

See also
pushContext()

Definition at line 968 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::processElementEmptyTag(), and QXmlSimpleReaderPrivate::processElementETagBegin2().

969 {
970  d->ns.clear();
971  if(!d->nsStack.isEmpty())
972  d->ns = d->nsStack.pop();
973 }
QStack< NamespaceMap > nsStack
Definition: qxml.cpp:735
void clear()
Removes all items from the map.
Definition: qmap.h:444
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108

◆ prefix()

QString QXmlNamespaceSupport::prefix ( const QString uri) const

Returns one of the prefixes mapped to the namespace URI uri.

If more than one prefix is currently mapped to the same URI, this function makes an arbitrary selection; if you want all of the prefixes, use prefixes() instead.

Note: to check for a default prefix, use the uri() function with an argument of "".

Definition at line 809 of file qxml.cpp.

810 {
812  while ((itc=it) != d->ns.constEnd()) {
813  ++it;
814  if (*itc == uri && !itc.key().isEmpty())
815  return itc.key();
816  }
817  return QLatin1String("");
818 }
#define it(className, varName)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108
friend class const_iterator
Definition: qmap.h:369

◆ prefixes() [1/2]

QStringList QXmlNamespaceSupport::prefixes ( ) const

Returns a list of all the prefixes currently declared.

If there is a default prefix, this function does not return it in the list; check for the default prefix using uri() with an argument of "".

Definition at line 901 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::processElementEmptyTag().

902 {
903  QStringList list;
904 
906  while ((itc=it) != d->ns.constEnd()) {
907  ++it;
908  if (!itc.key().isEmpty())
909  list.append(itc.key());
910  }
911  return list;
912 }
#define it(className, varName)
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108
friend class const_iterator
Definition: qmap.h:369

◆ prefixes() [2/2]

QStringList QXmlNamespaceSupport::prefixes ( const QString uri) const

Returns a list of all prefixes currently declared for the namespace URI uri.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The "xml:" prefix is included. If you only want one prefix that is mapped to the namespace URI, and you don't care which one you get, use the prefix() function instead.

Note: The empty (default) prefix is never included in this list; to check for the presence of a default namespace, call uri() with "" as the argument.

Definition at line 931 of file qxml.cpp.

932 {
933  QStringList list;
934 
936  while ((itc=it) != d->ns.constEnd()) {
937  ++it;
938  if (*itc == uri && !itc.key().isEmpty())
939  list.append(itc.key());
940  }
941  return list;
942 }
#define it(className, varName)
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108
friend class const_iterator
Definition: qmap.h:369

◆ processName()

void QXmlNamespaceSupport::processName ( const QString qname,
bool  isAttribute,
QString nsuri,
QString localname 
) const

Processes a raw XML 1.0 name in the current context by removing the prefix and looking it up among the prefixes currently declared.

qname is the raw XML 1.0 name to be processed. isAttribute is true if the name is an attribute name.

This function stores the namespace URI in nsuri (which will be set to an empty string if the raw name has an undeclared prefix), and stores the local name (without prefix) in localname (which will be set to an empty string if no namespace is in use).

Note that attribute names are processed differently than element names: an unprefixed element name gets the default namespace (if any), while an unprefixed attribute name does not.

Definition at line 864 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::parseElement(), QXmlSimpleReaderPrivate::processElementAttribute(), QXmlSimpleReaderPrivate::processElementEmptyTag(), and QXmlSimpleReaderPrivate::processElementETagBegin2().

867 {
868  int len = qname.size();
869  const QChar *data = qname.constData();
870  for (int pos = 0; pos < len; ++pos) {
871  if (data[pos] == QLatin1Char(':')) {
872  nsuri = uri(qname.left(pos));
873  localname = qname.mid(pos + 1);
874  return;
875  }
876  }
877 
878  // there was no ':'
879  nsuri.clear();
880  // attributes don't take default namespace
881  if (!isAttribute && !d->ns.isEmpty()) {
882  /*
883  We want to access d->ns.value(""), but as an optimization
884  we use the fact that "" compares less than any other
885  string, so it's either first in the map or not there.
886  */
888  if (first.key().isEmpty())
889  nsuri = first.value(); // get default namespace
890  }
891  localname = qname;
892 }
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
static const char * data(const QByteArray &arr)
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
bool isEmpty() const
Returns true if the map contains no items; otherwise returns false.
Definition: qmap.h:203
QString uri(const QString &) const
Looks up the prefix prefix in the current context and returns the currently-mapped namespace URI...
Definition: qxml.cpp:825
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108
friend class const_iterator
Definition: qmap.h:369

◆ pushContext()

void QXmlNamespaceSupport::pushContext ( )

Starts a new namespace context.

Normally, you should push a new context at the beginning of each XML element: the new context automatically inherits the declarations of its parent context, and it also keeps track of which declarations were made within this context.

See also
popContext()

Definition at line 954 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::parseElement().

955 {
956  d->nsStack.push(d->ns);
957 }
QStack< NamespaceMap > nsStack
Definition: qxml.cpp:735
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108

◆ reset()

void QXmlNamespaceSupport::reset ( )

Resets this namespace support object ready for reuse.

Definition at line 978 of file qxml.cpp.

979 {
981  delete d;
982  d = newD;
983 }
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108

◆ setPrefix()

void QXmlNamespaceSupport::setPrefix ( const QString pre,
const QString uri 
)

This function declares a prefix pre in the current namespace context to be the namespace URI uri.

The prefix remains in force until this context is popped, unless it is shadowed in a descendant context.

Note that there is an asymmetry in this library. prefix() does not return the default "" prefix, even if you have declared one; to check for a default prefix, you must 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.

Definition at line 790 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::processElementAttribute().

791 {
792  if(pre.isNull()) {
793  d->ns.insert(QLatin1String(""), uri);
794  } else {
795  d->ns.insert(pre, uri);
796  }
797 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108

◆ splitName()

void QXmlNamespaceSupport::splitName ( const QString qname,
QString prefix,
QString localname 
) const

Splits the name qname at the ':' and returns the prefix in prefix and the local name in localname.

See also
processName()

Definition at line 836 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::processElementAttribute().

838 {
839  int pos = qname.indexOf(QLatin1Char(':'));
840  if (pos == -1)
841  pos = qname.size();
842 
843  prefix = qname.left(pos);
844  localname = qname.mid(pos+1);
845 }
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ uri()

QString QXmlNamespaceSupport::uri ( const QString prefix) const

Looks up the prefix prefix in the current context and returns the currently-mapped namespace URI.

Use the empty string ("") for the default namespace.

Definition at line 825 of file qxml.cpp.

826 {
827  return d->ns[prefix];
828 }
QString prefix(const QString &) const
Returns one of the prefixes mapped to the namespace URI uri.
Definition: qxml.cpp:809
QXmlNamespaceSupportPrivate * d
Definition: qxml.h:108

Friends and Related Functions

◆ QXmlSimpleReaderPrivate

Definition at line 110 of file qxml.h.

Properties

◆ d

QXmlNamespaceSupportPrivate* QXmlNamespaceSupport::d
private

Definition at line 108 of file qxml.h.

Referenced by QXmlSimpleReaderPrivate::processElementETagBegin2().


The documentation for this class was generated from the following files: