NamespaceSupport.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-  NamespaceSupport.cpp,v 1.21 2006/04/20 09:55:37 jwillemsen Exp
00002 
00003 #include "ACEXML/common/NamespaceSupport.h"
00004 #include "ace/OS_NS_string.h"
00005 
00006 static const ACEXML_Char ACEXML_XMLNS_PREFIX_name[] = ACE_TEXT ("xmlns");
00007 
00008 const ACEXML_Char *ACEXML_NamespaceSupport::XMLNS_PREFIX = ACEXML_XMLNS_PREFIX_name;
00009 
00010 static const ACEXML_Char ACEXML_DEFAULT_NS_PREFIX[] = {0};
00011 
00012 static const ACEXML_Char ACEXML_TABOO_NS_PREFIX[] = ACE_TEXT ("xml");
00013 
00014 static const ACEXML_Char ACEXML_XMLNS_URI_name[] = ACE_TEXT ("http://www.w3.org/XML/1998/namespace");
00015 const ACEXML_Char *ACEXML_NamespaceSupport::XMLNS = ACEXML_XMLNS_URI_name;
00016 
00017 ACEXML_Namespace_Context_Stack::ACEXML_Namespace_Context_Stack (void)
00018 {
00019 }
00020 
00021 ACEXML_Namespace_Context_Stack::~ACEXML_Namespace_Context_Stack (void)
00022 {
00023   // Clean up stuff.
00024 }
00025 
00026 int
00027 ACEXML_Namespace_Context_Stack::push (ACEXML_NS_CONTEXT *nsc)
00028 {
00029   return (this->stack_.push (nsc) < 0);
00030 }
00031 
00032 ACEXML_NS_CONTEXT *
00033 ACEXML_Namespace_Context_Stack::pop (void)
00034 {
00035   if (this->stack_.size() <= 0)
00036     return 0;
00037 
00038   ACEXML_NS_CONTEXT* temp = 0;
00039   int retval = this->stack_.pop (temp);
00040   if (retval != 0)
00041     {
00042       ACE_ERROR ((LM_ERROR, "Unable to pop Namespace context from stack\n"));
00043       return 0;
00044     }
00045   return temp;
00046 }
00047 
00048 int
00049 ACEXML_NamespaceSupport::popContext (void)
00050 {
00051   delete this->effective_context_;
00052 
00053   if ((this->effective_context_ = this->ns_stack_.pop ()) == 0)
00054     return -1;
00055   return 0;
00056 }
00057 
00058 int
00059 ACEXML_NamespaceSupport::pushContext (void)
00060 {
00061   ACEXML_NS_CONTEXT *temp = this->effective_context_;
00062   ACE_NEW_RETURN (this->effective_context_,
00063                   ACEXML_NS_CONTEXT (),
00064                   -1);
00065 
00066   // @@ Copy everything from the old context to the new one.
00067   ACEXML_NS_CONTEXT_ENTRY *entry = 0;
00068 
00069   for (ACEXML_NS_CONTEXT_ITER iter (*temp);
00070        iter.next (entry) != 0;
00071        iter.advance ())
00072     this->effective_context_->bind (entry->ext_id_,
00073                                     entry->int_id_);
00074   this->ns_stack_.push (temp);
00075   return 0;
00076 }
00077 
00078 ACEXML_NamespaceSupport::ACEXML_NamespaceSupport (void)
00079   : ns_stack_ (),
00080     effective_context_ (0)
00081 {}
00082 
00083 int
00084 ACEXML_NamespaceSupport::init (void)
00085 {
00086   // @@ No way to tell if the new fails.
00087   ACE_NEW_RETURN (effective_context_, ACEXML_NS_CONTEXT(), -1);
00088 
00089   ACEXML_String prefix (ACEXML_TABOO_NS_PREFIX, 0, 0);
00090   ACEXML_String uri (ACEXML_XMLNS_URI_name, 0, 0);
00091   return this->effective_context_->bind (prefix, uri);
00092 }
00093 
00094 ACEXML_NamespaceSupport::~ACEXML_NamespaceSupport (void)
00095 {
00096   while (this->popContext () == 0)
00097     ;
00098 }
00099 
00100 int
00101 ACEXML_NamespaceSupport::declarePrefix (const ACEXML_Char *prefix,
00102                                         const ACEXML_Char *uri)
00103 {
00104   if (!prefix || !uri)
00105     return -1;
00106 
00107   // Unless predefined by w3.org(?) NS prefix can never start with
00108   // "xml".
00109   if (ACE_OS::strcmp (ACEXML_TABOO_NS_PREFIX, prefix) == 0)
00110     return -1;
00111 
00112   ACEXML_String ns_prefix (prefix, 0, 0);
00113   ACEXML_String ns_uri (uri, 0, 0);
00114 
00115   return this->effective_context_->rebind (ns_prefix, ns_uri);
00116 }
00117 
00118 int
00119 ACEXML_NamespaceSupport::getDeclaredPrefixes (ACEXML_STR_LIST &prefixes) const
00120 {
00121   ACEXML_NS_CONTEXT_ENTRY *entry = 0;
00122 
00123   // The prefix for default namespace (empty string) is included in
00124   // the return list.
00125   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00126        iter.next (entry) != 0;
00127        iter.advance ())
00128     prefixes.enqueue_tail (entry->ext_id_.c_str ());
00129 
00130   return 0;
00131 }
00132 
00133 const ACEXML_Char *
00134 ACEXML_NamespaceSupport::getPrefix (const ACEXML_Char *uri) const
00135 {
00136   if (!uri || *uri == 0)
00137     return 0;
00138 
00139   ACEXML_NS_CONTEXT_ENTRY *entry = 0;
00140 
00141   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00142        iter.next (entry) != 0;
00143        iter.advance ())
00144     if (entry->int_id_ == ACEXML_String (uri, 0, 0))
00145       return entry->ext_id_.c_str ();
00146 
00147   return 0;                     // Nothing found.
00148 }
00149 
00150 int
00151 ACEXML_NamespaceSupport::getPrefixes (ACEXML_STR_LIST &prefixes) const
00152 {
00153   ACEXML_NS_CONTEXT_ENTRY *entry = 0;
00154 
00155   // The prefix for default namespace (empty string) is not included
00156   // in the return list.
00157   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00158        iter.next (entry) != 0;
00159        iter.advance ())
00160     prefixes.enqueue_tail (entry->ext_id_.c_str ());
00161   return 0;
00162 }
00163 
00164 int
00165 ACEXML_NamespaceSupport::getPrefixes (const ACEXML_Char *uri,
00166                                       ACEXML_STR_LIST &prefixes) const
00167 {
00168   if (!uri)
00169     return -1;
00170 
00171   ACEXML_NS_CONTEXT_ENTRY *entry = 0;
00172 
00173   for (ACEXML_NS_CONTEXT_ITER iter (*this->effective_context_);
00174        iter.next (entry) != 0;
00175        iter.advance ())
00176     if (entry->int_id_ == ACEXML_String (uri, 0, 0) &&
00177         entry->ext_id_ != ACEXML_String (ACEXML_DEFAULT_NS_PREFIX, 0, 0))
00178       prefixes.enqueue_tail (entry->ext_id_.c_str ());
00179     else
00180       continue;
00181 
00182   return 0;                     // Nothing found.
00183 }
00184 
00185 const ACEXML_Char *
00186 ACEXML_NamespaceSupport::getURI (const ACEXML_Char *prefix) const
00187 {
00188   if (!prefix)
00189     return 0;
00190 
00191   ACEXML_NS_CONTEXT_ENTRY *entry = 0;
00192 
00193   if (this->effective_context_->find (ACEXML_String (prefix, 0, 0),
00194                                       entry) == 0)
00195     return entry->int_id_.c_str ();
00196   return 0;
00197 }
00198 
00199 int
00200 ACEXML_NamespaceSupport::processName (const ACEXML_Char *qName,
00201                                       const ACEXML_Char *&uri,
00202                                       const ACEXML_Char *&name,
00203                                       int is_attribute) const
00204 {
00205   int qlen = static_cast<int> (ACE_OS::strlen (qName));
00206   int len = -1;
00207   for (int i = 0; i < qlen; ++i)
00208     if (qName [i] == ':')
00209       {
00210         len = i;
00211         break;
00212       }
00213 
00214   ACEXML_String prefix (ACE_TEXT(""),0,0);
00215   if (len == -1)
00216       name = qName;
00217   else
00218     {
00219       prefix.set (qName, len, 1);
00220       name = qName + len + 1;
00221     }
00222 
00223   if (is_attribute && len == -1) {
00224     uri = ACEXML_DEFAULT_NS_PREFIX;
00225     return 0;
00226   }
00227 
00228   ACEXML_NS_CONTEXT_ENTRY *entry;
00229 
00230   if (this->effective_context_->find (prefix, entry) == 0)
00231     uri = entry->int_id_.c_str ();
00232   else
00233     {
00234       uri = ACEXML_DEFAULT_NS_PREFIX;
00235       return -1;
00236     }
00237   return 0;
00238 }
00239 
00240 int
00241 ACEXML_NamespaceSupport::reset (void)
00242 {
00243   while (this->popContext() != -1)
00244     ;
00245   return 0;
00246 }
00247 

Generated on Thu Nov 9 11:45:37 2006 for ACEXML by doxygen 1.3.6