Public Member Functions | Private Attributes | 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 Member Functions

 QXmlNamespaceSupport ()
 
 ~QXmlNamespaceSupport ()
 
void setPrefix (const QString &, const QString &)
 
QString prefix (const QString &) const
 
QString uri (const QString &) const
 
void splitName (const QString &, QString &, QString &) const
 
void processName (const QString &, bool, QString &, QString &) const
 
QStringList prefixes () const
 
QStringList prefixes (const QString &) const
 
void pushContext ()
 
void popContext ()
 
void reset ()
 

Private Attributes

QValueStack< QMap< QString, QString > > nsStack
 
QMap< QString, QStringns
 

Detailed Description

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

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 namespace description.

Definition at line 98 of file qxml.h.

Constructor & Destructor Documentation

QXmlNamespaceSupport::QXmlNamespaceSupport ( )

Constructs a QXmlNamespaceSupport.

Definition at line 321 of file qxml.cpp.

322 {
323  reset();
324 }
QXmlNamespaceSupport::~QXmlNamespaceSupport ( )

Destructs a QXmlNamespaceSupport.

Definition at line 329 of file qxml.cpp.

330 {
331 }

Member Function Documentation

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.

Definition at line 510 of file qxml.cpp.

511 {
512  if( !nsStack.isEmpty() )
513  ns = nsStack.pop();
514 }
QMap< QString, QString > ns
Definition: qxml.h:118
QValueStack< QMap< QString, QString > > nsStack
Definition: qxml.h:117
bool isEmpty() const
Definition: qvaluelist.h:368
QString QXmlNamespaceSupport::prefix ( const QString uri) const

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 "".

Definition at line 363 of file qxml.cpp.

364 {
366  while ( (itc=it) != ns.end() ) {
367  ++it;
368  if ( itc.data() == uri && !itc.key().isEmpty() )
369  return itc.key();
370  }
371  return "";
372 }
QMap< QString, QString > ns
Definition: qxml.h:118
bool isEmpty() const
Definition: qmap.h:543
Iterator end()
Definition: qmap.h:523
Iterator begin()
Definition: qmap.h:522
Definition: qmap.h:501
QStringList QXmlNamespaceSupport::prefixes ( ) const

Returns an enumeration of all prefixes currently declared.

Note: if there is a default prefix, it will not be returned in this enumeration; check for the default prefix using uri() with an argument of "".

Definition at line 453 of file qxml.cpp.

454 {
455  QStringList list;
456 
458  while ( (itc=it) != ns.end() ) {
459  ++it;
460  if ( !itc.key().isEmpty() )
461  list.append( itc.key() );
462  }
463  return list;
464 }
QMap< QString, QString > ns
Definition: qxml.h:118
bool isEmpty() const
Definition: qmap.h:543
Iterator append(const T &x)
Definition: qvaluelist.h:372
Iterator end()
Definition: qmap.h:523
Iterator begin()
Definition: qmap.h:522
A list of strings.
Definition: qstringlist.h:51
Definition: qmap.h:501
QStringList QXmlNamespaceSupport::prefixes ( const QString uri) const

Returns a list of all prefixes currently declared for a URI.

The xml: prefix will be included. If you want only one prefix that's 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 enumeration; to check for the presence of a default namespace, use uri() with an argument of "".

Definition at line 477 of file qxml.cpp.

478 {
479  QStringList list;
480 
482  while ( (itc=it) != ns.end() ) {
483  ++it;
484  if ( itc.data() == uri && !itc.key().isEmpty() )
485  list.append( itc.key() );
486  }
487  return list;
488 }
QMap< QString, QString > ns
Definition: qxml.h:118
bool isEmpty() const
Definition: qmap.h:543
Iterator append(const T &x)
Definition: qvaluelist.h:372
Iterator end()
Definition: qmap.h:523
Iterator begin()
Definition: qmap.h:522
A list of strings.
Definition: qstringlist.h:51
Definition: qmap.h:501
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.

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:

  • The namespace URI, or an empty string if none is in use.
  • The local name (without prefix).

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

Definition at line 421 of file qxml.cpp.

424 {
425  uint pos;
426  // search the ':'
427  for( pos=0; pos<qname.length(); pos++ ) {
428  if ( qname.at(pos) == ':' )
429  break;
430  }
431  if ( pos < qname.length() ) {
432  // there was a ':'
433  nsuri = uri( qname.left( pos ) );
434  localname = qname.mid( pos+1 );
435  } else {
436  // there was no ':'
437  if ( isAttribute ) {
438  nsuri = ""; // attributes don't take default namespace
439  } else {
440  nsuri = uri( "" ); // get default namespace
441  }
442  localname = qname;
443  }
444 }
QString mid(uint index, uint len=0xffffffff) const
Definition: qstring.cpp:13265
QString uri(const QString &) const
Definition: qxml.cpp:378
QChar at(uint i) const
Definition: qstring.h:492
QString left(uint len) const
Definition: qstring.cpp:13199
uint length() const
Definition: qstring.h:679
unsigned uint
Definition: qglobal.h:351
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 will automatically inherit the declarations of its parent context, but it will also keep track of which declarations were made within this context.

Definition at line 498 of file qxml.cpp.

499 {
500  nsStack.push( ns );
501 }
QMap< QString, QString > ns
Definition: qxml.h:118
void push(const T &d)
Definition: qvaluestack.h:52
QValueStack< QMap< QString, QString > > nsStack
Definition: qxml.h:117
void QXmlNamespaceSupport::reset ( )

Resets this namespace support object for reuse.

Definition at line 519 of file qxml.cpp.

520 {
521  nsStack.clear();
522  ns.clear();
523  ns.insert( "xml", "http://www.w3.org/XML/1998/namespace" ); // the XML namespace
524 }
QMap< QString, QString > ns
Definition: qxml.h:118
void clear()
Definition: qvaluelist.h:396
void clear()
Definition: qmap.h:565
Iterator insert(const Key &key, const T &value)
Definition: qmap.h:545
QValueStack< QMap< QString, QString > > nsStack
Definition: qxml.h:117
void QXmlNamespaceSupport::setPrefix ( const QString pre,
const QString uri 
)

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.

Definition at line 344 of file qxml.cpp.

345 {
346  if( pre.isNull() ) {
347  ns.insert( "", uri );
348  } else {
349  ns.insert( pre, uri );
350  }
351 }
QMap< QString, QString > ns
Definition: qxml.h:118
Iterator insert(const Key &key, const T &value)
Definition: qmap.h:545
bool isNull() const
Definition: qstring.h:379
void QXmlNamespaceSupport::splitName ( const QString qname,
QString prefix,
QString localname 
) const

Splits the name at the ':' and returns the prefix and the local name.

Definition at line 387 of file qxml.cpp.

389 {
390  uint pos;
391  // search the ':'
392  for( pos=0; pos<qname.length(); pos++ ) {
393  if ( qname.at(pos) == ':' )
394  break;
395  }
396  // and split
397  prefix = qname.left( pos );
398  localname = qname.mid( pos+1 );
399 }
QString mid(uint index, uint len=0xffffffff) const
Definition: qstring.cpp:13265
QChar at(uint i) const
Definition: qstring.h:492
QString left(uint len) const
Definition: qstring.cpp:13199
uint length() const
Definition: qstring.h:679
unsigned uint
Definition: qglobal.h:351
QString QXmlNamespaceSupport::uri ( const QString prefix) const

Looks up a prefix in the current context and returns the currently-mapped namespace URI. Use the empty string ("") for the default namespace.

Definition at line 378 of file qxml.cpp.

379 {
380  const QString& returi = ns[ prefix ];
381  return returi;
382 }
QMap< QString, QString > ns
Definition: qxml.h:118
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
QString prefix(const QString &) const
Definition: qxml.cpp:363

Member Data Documentation

QMap<QString, QString> QXmlNamespaceSupport::ns
private

Definition at line 118 of file qxml.h.

QValueStack<QMap<QString, QString> > QXmlNamespaceSupport::nsStack
private

Definition at line 117 of file qxml.h.


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