/*MT* MediaTomb - http://www.mediatomb.cc/ element.cc - this file is part of MediaTomb. Copyright (C) 2005 Gena Batyan , Sergey 'Jin' Bostandzhyan Copyright (C) 2006-2007 Gena Batyan , Sergey 'Jin' Bostandzhyan , Leonhard Wimmer MediaTomb is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. MediaTomb is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License version 2 along with MediaTomb; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. $Id: element.cc 1124 2007-02-17 21:49:15Z lww $ */ /// \file element.cc #ifdef HAVE_CONFIG_H #include "autoconfig.h" #endif #include "element.h" #include using namespace zmm; using namespace mxml; Element::Element(String name) : Object() { this->name = name; } Element::Element(String name, Ref context) : Object() { this->name = name; this->context = context; } String Element::getAttribute(String name) { if(attributes == nil) return nil; int len = attributes->size(); for(int i = 0; i < len; i++) { Ref attr = attributes->get(i); if(attr->name == name) return attr->value; } return nil; } void Element::addAttribute(String name, String value) { Ref attr = Ref(new Attribute(name, value)); addAttribute(attr); } void Element::addAttribute(Ref attr) { if (attributes == nil) attributes = Ref >(new Array()); attributes->append(attr); } void Element::setAttribute(String name, String value) { if (attributes == nil) attributes = Ref >(new Array()); int len = attributes->size(); for(int i = 0; i < len; i++) { Ref attr = attributes->get(i); if(attr->name == name) { attr->setValue(value); return; } } addAttribute(name, value); } String Element::getText() { return text; } int Element::childCount() { if (children == nil) return 0; return children->size(); } int Element::attributeCount() { if (attributes == nil) return 0; return attributes->size(); } Ref Element::getChild(int index) { if (children == nil) return nil; if (index >= children->size()) return nil; return children->get(index); } Ref Element::getAttribute(int index) { if (attributes == nil) return nil; if (index >= attributes->size()) return nil; return attributes->get(index); } Ref Element::getFirstChild() { return children->get(0); } void Element::appendChild(Ref child) { if(children == nil) children = Ref >(new Array()); children->append(child); } void Element::setText(String text) { this->text = text; } void Element::appendTextChild(String name, String text) { Ref el = Ref(new Element(name)); el->setText(text); appendChild(el); } Ref Element::getChild(String name) { if(children == nil) return nil; for(int i = 0; i < children->size(); i++) { Ref el = children->get(i); if(el->name == name) return el; } return nil; } String Element::getChildText(String name) { Ref el = getChild(name); if(el == nil) return nil; return el->getText(); } String Element::getName() { return name; } void Element::setName(String name) { this->name = name; } String Element::print() { Ref buf(new StringBuffer()); print(buf, 0); return buf->toString(); } void Element::print(Ref buf, int indent) { static char *ind_str = " "; static char *ind = ind_str + strlen(ind_str); char *ptr = ind - indent * 2; *buf << ptr; int i; *buf << "<" << name; if(attributes != nil) { for(i = 0; i < attributes->size(); i++) { *buf << ' '; Ref attr = attributes->get(i); *buf << attr->name << "=\"" << escape(attr->value) << '"'; } } if(text != nil) { *buf << '>' << escape(text) << "\n"; } else if(children != nil && children->size()) { *buf << ">\n"; for(i = 0; i < children->size(); i++) { children->get(i)->print(buf, indent + 1); } *buf << ptr << "\n"; } else { *buf << "/>\n"; } } String Element::escape(String str) { Ref buf(new StringBuffer(str.length())); signed char *ptr = (signed char *)str.c_str(); while (*ptr) { switch (*ptr) { case '<' : *buf << "<"; break; case '>' : *buf << ">"; break; case '&' : *buf << "&"; break; case '"' : *buf << """; break; case '\'' : *buf << "'"; break; // handle control codes default : if (((*ptr >= 0x00) && (*ptr <= 0x1f) && (*ptr != 0x09) && (*ptr != 0x0d) && (*ptr != 0x0a)) || (*ptr == 0x7f)) { *buf << '.'; } else *buf << *ptr; break; } ptr++; } return buf->toString(); }