00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
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 }