00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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 }