XMLObject.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "XMLObject.h"
00019 #include "util/StringBuffer.h"
00020 
00021 namespace oasys {
00022 
00023 //----------------------------------------------------------------------
00024 XMLObject::XMLObject(const std::string& tag)
00025     : tag_(tag), parent_(NULL)
00026 {
00027 }
00028 
00029 //----------------------------------------------------------------------
00030 XMLObject::~XMLObject()
00031 {
00032     Elements::iterator i;
00033     for (i = elements_.begin(); i != elements_.end(); ++i) {
00034         delete *i;
00035     }
00036 }
00037 
00038 //----------------------------------------------------------------------
00039 void
00040 XMLObject::add_attr(const std::string& attr, const std::string& val)
00041 {
00042     attrs_.push_back(attr);
00043     attrs_.push_back(val);
00044 }
00045 
00046 //----------------------------------------------------------------------
00047 void
00048 XMLObject::add_proc_inst(const std::string& target,
00049                          const std::string& data)
00050 {
00051     proc_insts_.push_back(target);
00052     proc_insts_.push_back(data);
00053 }
00054     
00055 //----------------------------------------------------------------------
00056 void
00057 XMLObject::add_element(XMLObject* child)
00058 {
00059     elements_.push_back(child);
00060     child->parent_ = this;
00061 }
00062 
00063 //----------------------------------------------------------------------
00064 void
00065 XMLObject::add_text(const char* text, size_t len)
00066 {
00067     if (len == 0) {
00068         len = strlen(text);
00069     }
00070     
00071     text_.append(text, len);
00072 }
00073 
00074 //----------------------------------------------------------------------
00075 void
00076 XMLObject::to_string(StringBuffer* buf, int indent, int cur_indent) const
00077 {
00078     static const char* space = "                                        "
00079                                "                                        ";
00080     
00081     buf->appendf("%.*s<%s", cur_indent, space, tag_.c_str());
00082     for (unsigned int i = 0; i < attrs_.size(); i += 2)
00083     {
00084         buf->appendf(" %s=\"%s\"", attrs_[i].c_str(), attrs_[i+1].c_str());
00085     }
00086 
00087     // shorthand for attribute-only tags
00088     if (proc_insts_.empty() && elements_.empty() && text_.size() == 0)
00089     {
00090         buf->appendf("/>");
00091         return;
00092     }
00093     else
00094     {
00095         buf->appendf(">%s", (indent == -1) ? "" : "\n");
00096 
00097     }
00098     
00099     for (unsigned int i = 0; i < proc_insts_.size(); i += 2)
00100     {
00101         buf->appendf("<?%s %s?>%s",
00102                      proc_insts_[i].c_str(), proc_insts_[i+1].c_str(),
00103                      (indent == -1) ? "" : "\n");
00104     }
00105     
00106     for (unsigned int i = 0; i < elements_.size(); ++i)
00107     {
00108         elements_[i]->to_string(buf, indent, (indent > 0) ? cur_indent + indent : 0);
00109     }
00110 
00111     buf->append(text_);
00112 
00113     buf->appendf("%.*s</%s>", cur_indent, space, tag_.c_str());
00114 }
00115 
00116 } // namespace oasys

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