ffead.server.doc
Element.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17 * Element.cpp
18 *
19 * Created on: Sep 5, 2009
20 * Author: sumeet
21 */
22 #include "Element.h"
23 #include "StringUtil.h"
24 bool Element::operator == (Element ele)
25 {
26  if(this->tagName == ele.tagName
27  && this->attributes == ele.attributes
28  && this->attributes == ele.attributes)
29  return true;
30  else
31  return false;
32 }
33 
34 bool Element::operator == (Element *ele)
35 {
36  if(this->tagName == ele->tagName
37  && this->attributes == ele->attributes
38  && this->attributes == ele->attributes)
39  return true;
40  else
41  return false;
42 }
43 
44 void Element::addElement(Element element)
45 {
46  this->elements.push_back(element);
47  if(&(this->mapOfEle[element.getTagName()])!=NULL)
48  this->mapOfEle[element.getTagName()] = element;
49 }
50 void Element::removeElement(Element element)
51 {
52  for(unsigned int i=0;i<this->elements.size();i++)
53  {
54  Element ele = elements.at(i);
55  if(element==ele)
56  {
57  this->elements.erase(elements.begin()+i);
58  this->mapOfEle.erase(element.getTagName());
59  }
60  }
61 }
62 
63 void Element::addAttribute(string key,string value)
64 {
65  this->attributes[key] = value;
66 }
67 void Element::removeAttribute(string key)
68 {
69  this->attributes.erase(key);
70 }
71 
72 bool Element::isNull()
73 {
74  if(this->getTagName()=="&#@^_NULL_&#@^")
75  return true;
76  else
77  return false;
78 }
79 //void updateAttribute(string,string) = &addAttribute;
80 typedef map<string,string> AttributeList;
81 typedef vector<Element> ElementList;
82 AttributeList Element::getAttributes()
83 {
84  return this->attributes;
85 }
86 string Element::getAttribute(string key)
87 {
88  return this->attributes[key];
89 }
90 ElementList Element::getChildElements()
91 {
92  return this->elements;
93 }
94 string Element::getTagName()
95 {
96  return this->tagName;
97 }
98 string Element::getNameSpc()
99 {
100  return this->nameSpace;
101 }
102 string Element::getNewTagNameSpc(string tag)
103 {
104  if(this->nameSpace!="")
105  return (this->nameSpace + ":" + tag);
106  else
107  return tag;
108 }
109 
110 string Element::getTagNameSpc()
111 {
112  if(this->nameSpace!="")
113  return (this->nameSpace + ":" + this->tagName);
114  else
115  return this->tagName;
116 }
117 void Element::setTagName(string tagName)
118 {
119  vector<string> vemp;
120  StringUtil::split(vemp, tagName, (":"));
121  if(vemp.size()==2)
122  {
123  this->tagName = vemp.at(1);
124  this->nameSpace = vemp.at(0);
125  }
126  else
127  this->tagName = tagName;
128 }
129 
130 string Element::getText() const
131 {
132  return this->text;
133 }
134 
135 void Element::setText(string text)
136 {
137  this->text = text;
138 }
139 
140 bool Element::getCdata() const
141 {
142  return this->cdata;
143 }
144 
145 void Element::setCdata(bool cdata)
146 {
147  this->cdata = cdata;
148 }
149 
150 Element Element::getElementByName(string name)
151 {
152  return this->mapOfEle[name];
153 }
154 
155 ElementList Element::getElementsByName(string name)
156 {
157  ElementList list;
158  for(int i=0;i<(int)this->elements.size();i++)
159  {
160  if(this->elements.at(i).getTagName()==name)
161  list.push_back(this->elements.at(i));
162  }
163  return list;
164 }
165 
166 string Element::render()
167 {
168  string rend;
169  rend.append(generateStartOpenTag(this->getTagName()));
170  rend.append(generateAttributes(this->getAttributes()));
171  rend.append(generateEndOpenTag());
172  rend.append(this->getText());
173  ElementList elements = this->getChildElements();
174  for(unsigned int i=0;i<elements.size();i++)
175  {
176  rend.append(elements.at(i).render());
177  }
178  rend.append(generateCloseTag(this->getTagName()));
179  return rend;
180 }
181 
182 string Element::renderChildren()
183 {
184  string rend;
185  ElementList elements = this->getChildElements();
186  for(unsigned int i=0;i<elements.size();i++)
187  {
188  rend.append(elements.at(i).render());
189  }
190  return rend;
191 }