XMLObject.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2006 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "XMLObject.h"
00040 #include "util/StringBuffer.h"
00041 
00042 namespace oasys {
00043 
00044 //----------------------------------------------------------------------
00045 XMLObject::XMLObject(const std::string& tag)
00046     : tag_(tag), parent_(NULL)
00047 {
00048 }
00049 
00050 //----------------------------------------------------------------------
00051 XMLObject::~XMLObject()
00052 {
00053     Elements::iterator i;
00054     for (i = elements_.begin(); i != elements_.end(); ++i) {
00055         delete *i;
00056     }
00057 }
00058 
00059 //----------------------------------------------------------------------
00060 void
00061 XMLObject::add_attr(const std::string& attr, const std::string& val)
00062 {
00063     attrs_.push_back(attr);
00064     attrs_.push_back(val);
00065 }
00066 
00067 //----------------------------------------------------------------------
00068 void
00069 XMLObject::add_proc_inst(const std::string& target,
00070                          const std::string& data)
00071 {
00072     proc_insts_.push_back(target);
00073     proc_insts_.push_back(data);
00074 }
00075     
00076 //----------------------------------------------------------------------
00077 void
00078 XMLObject::add_element(XMLObject* child)
00079 {
00080     elements_.push_back(child);
00081     child->parent_ = this;
00082 }
00083 
00084 //----------------------------------------------------------------------
00085 void
00086 XMLObject::add_text(const char* text, size_t len)
00087 {
00088     if (len == 0) {
00089         len = strlen(text);
00090     }
00091     
00092     text_.append(text, len);
00093 }
00094 
00095 //----------------------------------------------------------------------
00096 void
00097 XMLObject::to_string(StringBuffer* buf, int indent, int cur_indent) const
00098 {
00099     static const char* space = "                                        "
00100                                "                                        ";
00101     
00102     buf->appendf("%.*s<%s", cur_indent, space, tag_.c_str());
00103     for (unsigned int i = 0; i < attrs_.size(); i += 2)
00104     {
00105         buf->appendf(" %s=\"%s\"", attrs_[i].c_str(), attrs_[i+1].c_str());
00106     }
00107 
00108     // shorthand for attribute-only tags
00109     if (proc_insts_.empty() && elements_.empty() && text_.size() == 0)
00110     {
00111         buf->appendf("/>");
00112         return;
00113     }
00114     else
00115     {
00116         buf->appendf(">%s", (indent == -1) ? "" : "\n");
00117 
00118     }
00119     
00120     for (unsigned int i = 0; i < proc_insts_.size(); i += 2)
00121     {
00122         buf->appendf("<?%s %s?>%s",
00123                      proc_insts_[i].c_str(), proc_insts_[i+1].c_str(),
00124                      (indent == -1) ? "" : "\n");
00125     }
00126     
00127     for (unsigned int i = 0; i < elements_.size(); ++i)
00128     {
00129         elements_[i]->to_string(buf, indent, (indent > 0) ? cur_indent + indent : 0);
00130     }
00131 
00132     buf->append(text_);
00133 
00134     buf->appendf("%.*s</%s>", cur_indent, space, tag_.c_str());
00135 }
00136 
00137 } // namespace oasys

Generated on Fri Dec 22 14:48:01 2006 for DTN Reference Implementation by  doxygen 1.5.1