XMLSerialize.cc

Go to the documentation of this file.
00001 /*
00002  * License Agreement
00003  * 
00004  * NOTICE
00005  * This software (or technical data) was produced for the U. S.
00006  * Government under contract W15P7T-05-C-F600, and is
00007  * subject to the Rights in Data-General Clause 52.227-14 (JUNE 1987)
00008  * 
00009  * Copyright (C) 2006. The MITRE Corporation (http://www.mitre.org/).
00010  * All Rights Reserved.
00011  * 
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions
00014  * are met:
00015  * 
00016  * * Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  * 
00019  * * Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  * 
00023  * * The US Government will not be charged any license fee and/or
00024  * royalties related to this software.
00025  * 
00026  * * Neither name of The MITRE Corporation; nor the names of its
00027  * contributors may be used to endorse or promote products derived from
00028  * this software without specific prior written permission.
00029  * 
00030  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00031  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00032  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00033  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
00034  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00035  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00036  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00037  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00038  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00039  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00040  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00041  */
00042 
00043 #include "XMLSerialize.h"
00044 
00045 #include <config.h>
00046 #ifdef XERCES_C_ENABLED
00047 #include <xercesc/util/Base64.hpp>
00048 #include <xercesc/util/XMLString.hpp>
00049 #endif
00050 
00051 namespace oasys {
00052 
00053 XMLMarshal::XMLMarshal(ExpandableBuffer *buf, const char *root_tag)
00054     : SerializeAction(Serialize::MARSHAL, Serialize::CONTEXT_UNKNOWN),
00055       buf_(buf, false)
00056 {
00057     XMLObject *root_node = new XMLObject(root_tag);
00058     doc_.set_root(root_node);
00059     current_node_ = root_node;
00060 }
00061 
00062 void
00063 XMLMarshal::end_action()
00064 {
00065     doc_.to_string( &buf_, -1 );
00066 }
00067 
00068 void
00069 XMLMarshal::process(const char *name, SerializableObject* object)
00070 {
00071     if (! object) return;
00072 
00073     XMLObject *parent_node = current_node_;
00074     XMLObject *new_node = new XMLObject(name);
00075     current_node_->add_element(new_node);
00076     current_node_ = new_node;
00077 
00078     object->serialize(this);
00079 
00080     current_node_ = parent_node;
00081 }
00082 
00083 void
00084 XMLMarshal::process(const char *name, u_int64_t *i)
00085 {
00086     StringBuffer buf;
00087     buf.appendf("%llu", U64FMT(*i));
00088     current_node_->add_attr(name, std::string(buf.data()));
00089 }
00090 
00091 void
00092 XMLMarshal::process(const char *name, u_int32_t *i)
00093 {
00094     StringBuffer buf;
00095     buf.appendf("%u", *i);
00096     current_node_->add_attr(name, std::string(buf.data()));
00097 }
00098 
00099 void
00100 XMLMarshal::process(const char *name, u_int16_t *i)
00101 {
00102     StringBuffer buf;
00103     buf.appendf("%hu", *i);
00104     current_node_->add_attr(name, std::string(buf.data()));
00105 }
00106 
00107 void
00108 XMLMarshal::process(const char *name, u_int8_t *i)
00109 {
00110     StringBuffer buf;
00111     buf.appendf("%hhu", *i);
00112     current_node_->add_attr(name, std::string(buf.data()));
00113 }
00114 
00115 void
00116 XMLMarshal::process(const char *name, bool *b)
00117 {
00118     *b ?
00119         current_node_->add_attr(std::string(name), std::string("true")) :
00120         current_node_->add_attr(std::string(name), std::string("false"));
00121 }
00122 
00123 void
00124 XMLMarshal::process(const char *name, u_char *bp, u_int32_t len)
00125 {
00126 #ifdef XERCES_C_ENABLED
00127     unsigned int elen;
00128     XMLByte *estr = xercesc::Base64::encode(bp, len, &elen);
00129     current_node_->add_attr(std::string(name),
00130         std::string(reinterpret_cast<char *>(estr), elen));
00131     xercesc::XMLString::release(&estr);
00132 #else
00133     (void) name;
00134     (void) bp;
00135     (void) len;
00136     
00137     signal_error();
00138 #endif
00139 }
00140 
00141 void
00142 XMLMarshal::process(const char *name, u_char **bp,
00143                     u_int32_t *lenp, int flags)
00144 {
00145     (void) name;
00146 
00147     ASSERT(! (lenp == 0 && ! (flags & Serialize::NULL_TERMINATED)));
00148     
00149     size_t len;
00150     if (flags & Serialize::NULL_TERMINATED) 
00151     {
00152         len = strlen(reinterpret_cast<char *>(*bp));
00153     } 
00154     else 
00155     {
00156         len = *lenp;
00157     }
00158 
00159 #ifdef XERCES_C_ENABLED
00160     unsigned int elen;
00161     XMLByte *estr = xercesc::Base64::encode(*bp, len, &elen);
00162     current_node_->add_attr(std::string(name),
00163         std::string(reinterpret_cast<char *>(estr), elen));
00164     xercesc::XMLString::release(&estr);
00165 #else
00166     signal_error();
00167 #endif
00168 }
00169 
00170 void
00171 XMLMarshal::process(const char *name, std::string *s)
00172 {
00173     current_node_->add_attr(std::string(name), *s);
00174 }
00175 
00176 } // namespace oasys

Generated on Sat Sep 8 08:43:36 2007 for DTN Reference Implementation by  doxygen 1.5.3