Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACEXML_NamespaceSupport Class Reference

ACEXML_NamespaceSupport provides namespace management operation for an XML parser. More...

#include "ACEXML/common/NamespaceSupport.h"

Collaboration diagram for ACEXML_NamespaceSupport:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACEXML_NamespaceSupport (void)
 ~ACEXML_NamespaceSupport (void)
int declarePrefix (const ACEXML_Char *prefix, const ACEXML_Char *uri)
int getDeclaredPrefixes (ACEXML_STR_LIST &prefixes) const
const ACEXML_ChargetPrefix (const ACEXML_Char *uri) const
int getPrefixes (ACEXML_STR_LIST &prefixes) const
int getPrefixes (const ACEXML_Char *uri, ACEXML_STR_LIST &prefixes) const
const ACEXML_ChargetURI (const ACEXML_Char *prefix) const
int popContext (void)
int processName (const ACEXML_Char *qName, const ACEXML_Char *&uri, const ACEXML_Char *&name, int is_attribute) const
int pushContext (void)
int reset (void)

Static Public Attributes

const ACEXML_CharXMLNS_PREFIX = ACEXML_XMLNS_PREFIX_name
const ACEXML_CharXMLNS = ACEXML_XMLNS_URI_name

Private Attributes

ACEXML_Namespace_Context_Stack ns_stack_
ACEXML_NS_CONTEXTeffective_context_

Detailed Description

ACEXML_NamespaceSupport provides namespace management operation for an XML parser.

This class encapsulates the logic of Namespace processing: it tracks the declarations currently in force for each context and automatically processes qualified XML 1.0 names into their Namespace parts; it can also be used in reverse for generating XML 1.0 from Namespaces.

Namespace support objects are reusable, but the reset method must be invoked between each session.

Here is a simple session (in Java :-p):

  String parts[] = new String[3];
  NamespaceSupport support = new NamespaceSupport();

  support.pushContext();
  support.declarePrefix("", "http://www.w3.org/1999/xhtml");
  support.declarePrefix("dc", "http://www.purl.org/dc#");

  String parts[] = support.processName("p", parts, false);
  System.out.println("Namespace URI: " + parts[0]);
  System.out.println("Local name: " + parts[1]);
  System.out.println("Raw name: " + parts[2]);

  String parts[] = support.processName("dc:title", parts, false);
  System.out.println("Namespace URI: " + parts[0]);
  System.out.println("Local name: " + parts[1]);
  System.out.println("Raw name: " + parts[2]);

  support.popContext();

Note that this class is optimized for the use case where most elements do not contain Namespace declarations: if the same prefix/URI mapping is repeated for each context (for example), this class will be somewhat less efficient.

See also:
ACEXML_Exception

Definition at line 130 of file NamespaceSupport.h.


Constructor & Destructor Documentation

ACEXML_NamespaceSupport::ACEXML_NamespaceSupport void   
 

Default constructor.

Definition at line 62 of file NamespaceSupport.cpp.

References ACE_NEW, ACEXML_String, ACEXML_TABOO_NS_PREFIX, ACEXML_XMLNS_URI_name, ACE_Hash_Map_Manager_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::bind, and effective_context_.

00063   : ns_stack_ (),
00064     effective_context_ (0)
00065 {
00066   // @@ No way to tell if the new fails.
00067   ACE_NEW (effective_context_,
00068            ACEXML_NS_CONTEXT ());
00069 
00070   ACEXML_String prefix (ACEXML_TABOO_NS_PREFIX, 0, 0);
00071   ACEXML_String uri (ACEXML_XMLNS_URI_name, 0, 0);
00072   this->effective_context_->bind (prefix, uri);
00073 }

ACEXML_NamespaceSupport::~ACEXML_NamespaceSupport void   
 

Default destructor.

Definition at line 75 of file NamespaceSupport.cpp.

References popContext.

00076 {
00077   while (this->popContext () == 0)
00078     ;
00079 }


Member Function Documentation

int ACEXML_NamespaceSupport::declarePrefix const ACEXML_Char   prefix,
const ACEXML_Char   uri
 

Declare a Namespace prefix. Return -1 if the prefix was illegal or an internal error occured. Return 0 if the prefix gets declared successfully, 1 if the prefix replaces an existing prefix definition.

Definition at line 82 of file NamespaceSupport.cpp.

References ACEXML_Char, ACEXML_String, ACEXML_TABOO_NS_PREFIX, ACE_Hash_Map_Manager_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::bind, effective_context_, and ACE_OS_String::strcmp.

Referenced by ACE_TMAIN.

00084 {
00085   // Unless predefined by w3.org(?) NS prefix can never start with
00086   // "xml".
00087   if (ACE_OS_String::strcmp (ACEXML_TABOO_NS_PREFIX, prefix) == 0)
00088     return -1;
00089 
00090   ACEXML_String ns_prefix (prefix, 0, 0);
00091   ACEXML_String ns_uri (uri, 0, 0);
00092 
00093   return this->effective_context_->bind (ns_prefix,
00094                                          ns_uri);
00095 }

int ACEXML_NamespaceSupport::getDeclaredPrefixes ACEXML_STR_LIST   prefixes const
 

Return all prefixes declared in current context in the user-supplied list prefixes. It is user's reponsibility to ensure the list was empty originally.

Definition at line 98 of file NamespaceSupport.cpp.

References ACE_Hash_Map_Iterator_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::advance, ACE_Unbounded_Queue::enqueue_tail, and ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::ext_id_.

Referenced by ACE_TMAIN.

00099 {
00100   ACEXML_NS_CONTEXT_ENTRY *entry;
00101 
00102   // The prefix for default namespace (empty string) is included in
00103   // the return list.
00104   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00105        iter.next (entry) != 0;
00106        iter.advance ())
00107     prefixes.enqueue_tail (entry->ext_id_.c_str ());
00108 
00109   return 0;
00110 }

const ACEXML_Char * ACEXML_NamespaceSupport::getPrefix const ACEXML_Char   uri const
 

Return one of the prefixes mapped to a Namespace URI.

Definition at line 113 of file NamespaceSupport.cpp.

References ACEXML_Char, ACEXML_DEFAULT_NS_PREFIX, ACEXML_String, ACE_Hash_Map_Iterator_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::advance, ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::ext_id_, and ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::int_id_.

Referenced by ACE_TMAIN.

00114 {
00115   ACEXML_NS_CONTEXT_ENTRY *entry;
00116 
00117   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00118        iter.next (entry) != 0;
00119        iter.advance ())
00120     if (entry->int_id_ == ACEXML_String (uri, 0, 0) &&
00121         entry->ext_id_ != ACEXML_String (ACEXML_DEFAULT_NS_PREFIX, 0, 0))
00122       return entry->ext_id_.c_str ();
00123 
00124   return 0;                     // Nothing found.
00125 }

int ACEXML_NamespaceSupport::getPrefixes const ACEXML_Char   uri,
ACEXML_STR_LIST   prefixes
const
 

Return all prefixes currently declared for a URI in the user-supplied list.

Definition at line 146 of file NamespaceSupport.cpp.

References ACEXML_Char, ACEXML_DEFAULT_NS_PREFIX, ACEXML_String, ACE_Hash_Map_Iterator_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::advance, ACE_Unbounded_Queue::enqueue_tail, ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::ext_id_, and ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::int_id_.

00148 {
00149   ACEXML_NS_CONTEXT_ENTRY *entry;
00150 
00151   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00152        iter.next (entry) != 0;
00153        iter.advance ())
00154     if (entry->int_id_ == ACEXML_String (uri, 0, 0) &&
00155         entry->ext_id_ != ACEXML_String (ACEXML_DEFAULT_NS_PREFIX, 0, 0))
00156       prefixes.enqueue_tail (entry->ext_id_.c_str ());
00157     else
00158       continue;
00159 
00160   return 0;                     // Nothing found.
00161 }

int ACEXML_NamespaceSupport::getPrefixes ACEXML_STR_LIST   prefixes const
 

Return all prefixes currently declared in the user-supplied list. @ Known bug: This function should only return user-defined prefixes.

Definition at line 128 of file NamespaceSupport.cpp.

References ACEXML_DEFAULT_NS_PREFIX, ACEXML_String, ACE_Hash_Map_Iterator_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::advance, ACE_Unbounded_Queue::enqueue_tail, and ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::ext_id_.

Referenced by ACE_TMAIN.

00129 {
00130   ACEXML_NS_CONTEXT_ENTRY *entry;
00131 
00132   // The prefix for default namespace (empty string) is not included
00133   // in the return list.
00134   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00135        iter.next (entry) != 0;
00136        iter.advance ())
00137     if (entry->ext_id_ != ACEXML_String(ACEXML_DEFAULT_NS_PREFIX, 0, 0))
00138       prefixes.enqueue_tail (entry->ext_id_.c_str ());
00139     else
00140       continue;
00141 
00142   return 0;
00143 }

const ACEXML_Char * ACEXML_NamespaceSupport::getURI const ACEXML_Char   prefix const
 

Look up a prefix and get the currently-mapped Namespace URI.

Definition at line 164 of file NamespaceSupport.cpp.

References ACEXML_Char, ACEXML_String, effective_context_, ACE_Hash_Map_Manager_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::find, and ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::int_id_.

Referenced by ACE_TMAIN.

00165 {
00166   ACEXML_NS_CONTEXT_ENTRY *entry;
00167 
00168   if (this->effective_context_->find (ACEXML_String (prefix, 0, 0),
00169                                       entry) == 0)
00170     return entry->int_id_.c_str ();
00171   return 0;
00172 }

int ACEXML_NamespaceSupport::popContext void   
 

Revert to the previous namespace context.

Definition at line 175 of file NamespaceSupport.cpp.

References effective_context_, ns_stack_, and ACEXML_Namespace_Context_Stack::pop.

Referenced by ACE_TMAIN, and ~ACEXML_NamespaceSupport.

00176 {
00177   delete this->effective_context_;
00178 
00179   if ((this->effective_context_ = this->ns_stack_.pop ()) == 0)
00180     return -1;
00181   return 0;
00182 }

int ACEXML_NamespaceSupport::processName const ACEXML_Char   qName,
const ACEXML_Char *&    uri,
const ACEXML_Char *&    name,
int    is_attribute
const
 

Process a raw XML 1.0 name. qName is the raw XML name we want to parse, uri contains the URI string of the raw name. It points to a null string if the namespace is not valid or there's no namespace defined. name contains the original name without the prefix. is_attribute specifies whether the name is an attribute or not. Attributes have different scoping rules from elements.

Definition at line 206 of file NamespaceSupport.cpp.

References ACEXML_Char, ACEXML_DEFAULT_NS_PREFIX, ACEXML_String, effective_context_, ACE_Hash_Map_Manager_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::find, ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::int_id_, and ACE_OS_String::strlen.

Referenced by ACE_TMAIN.

00210 {
00211   size_t qlen = ACE_OS_String::strlen (qName);
00212   int len = -1;
00213   for (size_t i = 0; i < qlen; ++i)
00214     if (qName [i] == ':')
00215       {
00216         len = ACE_static_cast(int, i);
00217         break;
00218       }
00219 
00220   ACEXML_String prefix;
00221 
00222   if (len == -1)
00223     {
00224       name = qName;
00225     }
00226   else
00227     {
00228       prefix.set (qName, len, 1);
00229       name = qName + len + 1;
00230     }
00231 
00232   if (is_attribute && len == -1) {
00233     uri = ACEXML_DEFAULT_NS_PREFIX;
00234     return 0;
00235   }
00236 
00237   ACEXML_NS_CONTEXT_ENTRY *entry;
00238 
00239   if (this->effective_context_->find (prefix, entry) == 0)
00240     uri = entry->int_id_.c_str ();
00241   else
00242     {
00243       uri = ACEXML_DEFAULT_NS_PREFIX;
00244       return -1;
00245     }
00246 
00247   return 0;
00248 }

int ACEXML_NamespaceSupport::pushContext void   
 

Start a new Namespace context. Prefixes defined in previous context are copied over to the new context.

Definition at line 185 of file NamespaceSupport.cpp.

References ACE_NEW_RETURN, ACE_Hash_Map_Iterator_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::advance, ACE_Hash_Map_Manager_Ex< ACEXML_String, ACEXML_String, ACE_Hash< ACEXML_String >, ACE_Equal_To< ACEXML_String >, ACE_Null_Mutex >::bind, effective_context_, ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::ext_id_, ACE_Hash_Map_Entry< ACEXML_String, ACEXML_String >::int_id_, ns_stack_, and ACEXML_Namespace_Context_Stack::push.

Referenced by ACE_TMAIN.

00186 {
00187   ACEXML_NS_CONTEXT *temp = this->effective_context_;
00188   ACE_NEW_RETURN (this->effective_context_,
00189                   ACEXML_NS_CONTEXT (),
00190                   -1);
00191 
00192   // @@ Copy everything from the old context to the new one.
00193   ACEXML_NS_CONTEXT_ENTRY *entry;
00194 
00195   for (ACEXML_NS_CONTEXT_ITER iter (*temp);
00196        iter.next (entry) != 0;
00197        iter.advance ())
00198     this->effective_context_->bind (entry->ext_id_,
00199                                     entry->int_id_);
00200   this->ns_stack_.push (temp);
00201   return 0;
00202 }

int ACEXML_NamespaceSupport::reset void   
 

Reset this Namespace support object for reuse.

Todo:
Not implemented.

Definition at line 251 of file NamespaceSupport.cpp.

00252 {
00253   // Not implemented.
00254   return 0;
00255 }


Member Data Documentation

ACEXML_NS_CONTEXT* ACEXML_NamespaceSupport::effective_context_ [private]
 

The effective namespace context.

Definition at line 229 of file NamespaceSupport.h.

Referenced by ACEXML_NamespaceSupport, declarePrefix, getURI, popContext, processName, and pushContext.

ACEXML_Namespace_Context_Stack ACEXML_NamespaceSupport::ns_stack_ [private]
 

Namespace Context stack. When we entering a new namespace context, the old context is duplicated and pushed into this stack.

Definition at line 224 of file NamespaceSupport.h.

Referenced by popContext, and pushContext.

const ACEXML_Char * ACEXML_NamespaceSupport::XMLNS = ACEXML_XMLNS_URI_name [static]
 

Definition at line 17 of file NamespaceSupport.cpp.

const ACEXML_Char * ACEXML_NamespaceSupport::XMLNS_PREFIX = ACEXML_XMLNS_PREFIX_name [static]
 

XMLNS default prefix and URI strings.

Definition at line 6 of file NamespaceSupport.cpp.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 13:27:19 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002