Public Member Functions | Static Public Attributes | Private Attributes

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 Member Functions

 ACEXML_NamespaceSupport (void)
 ~ACEXML_NamespaceSupport (void)
int init (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

static const ACEXML_CharXMLNS_PREFIX = ACEXML_XMLNS_PREFIX_name
static 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 126 of file NamespaceSupport.h.


Constructor & Destructor Documentation

ACEXML_NamespaceSupport::ACEXML_NamespaceSupport ( void   ) 

Default constructor.

Definition at line 78 of file NamespaceSupport.cpp.

ACEXML_NamespaceSupport::~ACEXML_NamespaceSupport ( void   ) 

Default destructor.

Definition at line 94 of file NamespaceSupport.cpp.

{
  while (this->popContext () == 0)
    ;
}


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 101 of file NamespaceSupport.cpp.

{
  if (!prefix || !uri)
    return -1;

  // Unless predefined by w3.org(?) NS prefix can never start with
  // "xml".
  if (ACE_OS::strcmp (ACEXML_TABOO_NS_PREFIX, prefix) == 0)
    return -1;

  ACEXML_String ns_prefix (prefix, 0, false);
  ACEXML_String ns_uri (uri, 0, false);

  return this->effective_context_->rebind (ns_prefix, ns_uri);
}

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 119 of file NamespaceSupport.cpp.

{
  ACEXML_NS_CONTEXT_ENTRY *entry = 0;

  // The prefix for default namespace (empty string) is included in
  // the return list.
  for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
       iter.next (entry) != 0;
       iter.advance ())
    prefixes.enqueue_tail (entry->ext_id_.c_str ());

  return 0;
}

const ACEXML_Char * ACEXML_NamespaceSupport::getPrefix ( const ACEXML_Char uri  )  const

Return one of the prefixes mapped to a Namespace URI.

Definition at line 134 of file NamespaceSupport.cpp.

{
  if (!uri || *uri == 0)
    return 0;

  ACEXML_NS_CONTEXT_ENTRY *entry = 0;

  for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
       iter.next (entry) != 0;
       iter.advance ())
    if (entry->int_id_ == ACEXML_String (uri, 0, false))
      return entry->ext_id_.c_str ();

  return 0;                     // Nothing found.
}

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 151 of file NamespaceSupport.cpp.

{
  ACEXML_NS_CONTEXT_ENTRY *entry = 0;

  // The prefix for default namespace (empty string) is not included
  // in the return list.
  for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
       iter.next (entry) != 0;
       iter.advance ())
    prefixes.enqueue_tail (entry->ext_id_.c_str ());
  return 0;
}

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 165 of file NamespaceSupport.cpp.

{
  if (!uri)
    return -1;

  ACEXML_NS_CONTEXT_ENTRY *entry = 0;

  for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
       iter.next (entry) != 0;
       iter.advance ())
    if (entry->int_id_ == ACEXML_String (uri, 0, false) &&
        entry->ext_id_ != ACEXML_String (ACEXML_DEFAULT_NS_PREFIX, 0, false))
      prefixes.enqueue_tail (entry->ext_id_.c_str ());
    else
      continue;

  return 0;                     // Nothing found.
}

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 186 of file NamespaceSupport.cpp.

{
  if (!prefix)
    return 0;

  ACEXML_NS_CONTEXT_ENTRY *entry = 0;

  if (this->effective_context_->find (ACEXML_String (prefix, 0, false),
                                      entry) == 0)
    return entry->int_id_.c_str ();
  return 0;
}

int ACEXML_NamespaceSupport::init ( void   ) 

Initialize the namespace support object

Definition at line 84 of file NamespaceSupport.cpp.

{
  // @@ No way to tell if the new fails.
  ACE_NEW_RETURN (effective_context_, ACEXML_NS_CONTEXT(), -1);

  ACEXML_String prefix (ACEXML_TABOO_NS_PREFIX, 0, false);
  ACEXML_String uri (ACEXML_XMLNS_URI_name, 0, false);
  return this->effective_context_->bind (prefix, uri);
}

int ACEXML_NamespaceSupport::popContext ( void   ) 

Revert to the previous namespace context.

Definition at line 49 of file NamespaceSupport.cpp.

{
  delete this->effective_context_;

  if ((this->effective_context_ = this->ns_stack_.pop ()) == 0)
    return -1;
  return 0;
}

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 200 of file NamespaceSupport.cpp.

{
  int qlen = static_cast<int> (ACE_OS::strlen (qName));
  int len = -1;
  for (int i = 0; i < qlen; ++i)
    if (qName [i] == ':')
      {
        len = i;
        break;
      }

  ACEXML_String prefix (ACE_TEXT (""), 0, false);
  if (len == -1)
      name = qName;
  else
    {
      prefix.set (qName, len, 1);
      name = qName + len + 1;
    }

  if (is_attribute && len == -1) {
    uri = ACEXML_DEFAULT_NS_PREFIX;
    return 0;
  }

  ACEXML_NS_CONTEXT_ENTRY *entry;

  if (this->effective_context_->find (prefix, entry) == 0)
    uri = entry->int_id_.c_str ();
  else
    {
      uri = ACEXML_DEFAULT_NS_PREFIX;
      return -1;
    }
  return 0;
}

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 59 of file NamespaceSupport.cpp.

{
  ACEXML_NS_CONTEXT *temp = this->effective_context_;
  ACE_NEW_RETURN (this->effective_context_,
                  ACEXML_NS_CONTEXT (),
                  -1);

  // @@ Copy everything from the old context to the new one.
  ACEXML_NS_CONTEXT_ENTRY *entry = 0;

  for (ACEXML_NS_CONTEXT_ITER iter (*temp);
       iter.next (entry) != 0;
       iter.advance ())
    this->effective_context_->bind (entry->ext_id_,
                                    entry->int_id_);
  this->ns_stack_.push (temp);
  return 0;
}

int ACEXML_NamespaceSupport::reset ( void   ) 

Reset this Namespace support object for reuse.

Definition at line 241 of file NamespaceSupport.cpp.

{
  while (this->popContext() != -1)
    ;
  return 0;
}


Member Data Documentation

The effective namespace context.

Definition at line 230 of file NamespaceSupport.h.

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

Definition at line 225 of file NamespaceSupport.h.

Definition at line 148 of file NamespaceSupport.h.

XMLNS default prefix and URI strings.

Definition at line 147 of file NamespaceSupport.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines