src/misc/xmlwrapp/namespace.cpp

Go to the documentation of this file.
00001 /*
00002  * Redistribution and use in source and binary forms, with or without
00003  * modification, are permitted provided that the following conditions
00004  * are met:
00005  *
00006  * 1. Redistributions of source code must retain the above copyright
00007  *    notice, this list of conditions and the following disclaimer.
00008  * 2. Redistributions in binary form must reproduce the above copyright
00009  *    notice, this list of conditions and the following disclaimer in
00010  *    the documentation and/or other materials provided with the
00011  *    distribution.
00012  * 3. Neither the name of the Author nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
00017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00018  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00019  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
00020  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00021  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00023  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00024  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00025  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00026  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * $Id: namespace.cpp 158146 2009-04-22 15:56:43Z satskyse $
00032  */
00033 
00034 /** @file
00035  * This file contains the implementation of the xml::ns class.
00036 **/
00037 
00038 // xmlwrapp includes
00039 #include <misc/xmlwrapp/namespace.hpp>
00040 
00041 // standard includes
00042 #include <stdexcept>
00043 #include <string.h>
00044 
00045 // libxml includes
00046 #include <libxml/tree.h>
00047 
00048 using namespace xml;
00049 
00050 
00051 xml::ns::ns (enum ns::ns_type ) : prefix_(), uri_(),
00052                                   unsafe_ns_(NULL),
00053                                   safety_(ns::type_safe_ns)
00054 {
00055 }
00056 
00057 
00058 xml::ns::ns (const char *  prefix, const char *  uri) : prefix_(prefix ? prefix : ""),
00059                                                         uri_(uri ? uri : ""),
00060                                                         unsafe_ns_(NULL),
00061                                                         safety_(ns::type_safe_ns)
00062 {
00063     if (uri_.empty())
00064         throw std::runtime_error("xml::ns can't have empty uri");
00065 }
00066 
00067 
00068 xml::ns::ns (void * rawLibXML2Namespace) : prefix_(), uri_(),
00069                                            unsafe_ns_(rawLibXML2Namespace),
00070                                            safety_(ns::type_unsafe_ns)
00071 {
00072 }
00073 
00074 
00075 const char *  xml::ns::get_prefix (void) const
00076 {
00077     if (safety_ == ns::type_safe_ns)
00078         return prefix_.c_str();
00079 
00080     if (!unsafe_ns_)
00081         return prefix_.c_str();
00082 
00083     return reinterpret_cast<xmlNs*>(unsafe_ns_)->prefix
00084            ? reinterpret_cast<const char*>(reinterpret_cast<xmlNs*>(unsafe_ns_)->prefix)
00085            : prefix_.c_str();
00086 }
00087 
00088 
00089 const char *  xml::ns::get_uri (void) const
00090 {
00091     if (safety_ == ns::type_safe_ns)
00092         return uri_.c_str();
00093 
00094     if (!unsafe_ns_)
00095         return uri_.c_str();
00096 
00097     return reinterpret_cast<xmlNs*>(unsafe_ns_)->href
00098            ? reinterpret_cast<const char*>(reinterpret_cast<xmlNs*>(unsafe_ns_)->href)
00099            : uri_.c_str();
00100 }
00101 
00102 
00103 bool xml::ns::is_void (void) const
00104 {
00105     if (safety_ == ns::type_safe_ns)
00106         return uri_.empty();
00107 
00108     if (!unsafe_ns_)
00109         return true;
00110 
00111     return reinterpret_cast<xmlNs*>(unsafe_ns_)->href == NULL;
00112 }
00113 
00114 
00115 void xml::ns::make_safe (void)
00116 {
00117     if (safety_ == ns::type_safe_ns)
00118         return;
00119 
00120     if (unsafe_ns_)
00121     {
00122         uri_ = reinterpret_cast<xmlNs*>(unsafe_ns_)->href
00123                ? std::string(reinterpret_cast<const char*>(reinterpret_cast<xmlNs*>(unsafe_ns_)->href))
00124                : std::string();
00125         prefix_ = reinterpret_cast<xmlNs*>(unsafe_ns_)->prefix
00126                   ? std::string(reinterpret_cast<const char*>(reinterpret_cast<xmlNs*>(unsafe_ns_)->prefix))
00127                   : std::string();
00128     }
00129     unsafe_ns_ = NULL;
00130     safety_ = ns::type_safe_ns;
00131 }
00132 
00133 
00134 bool xml::ns::is_safe (void) const
00135 {
00136     return safety_ == ns::type_safe_ns;
00137 }
00138 
00139 
00140 bool xml::ns::operator==(const ns &  other) const
00141 {
00142     return (strcmp(this->get_prefix(), other.get_prefix()) == 0) &&
00143            (strcmp(this->get_uri(), other.get_uri()) == 0);
00144 }
00145 
00146 
00147 

Generated on Wed Dec 9 04:43:10 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Wed Dec 09 08:18:05 2009 by modify_doxy.py rev. 173732