ffead.server.doc
Document.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  * Document.cpp
18  *
19  * Created on: Sep 24, 2009
20  * Author: sumeet
21  */
22 
23 #include "Document.h"
24 
25 Document::Document() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 Document::~Document() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 Element Document::getRootElement()
35 {
36  return this->root;
37 }
38 
39 void Document::setRootElement(Element root)
40 {
41  this->root = root;
42 }
43 
44 
45 string Document::getDocType() const
46 {
47  return docType;
48 }
49 
50 void Document::setDocType(string docType)
51 {
52  this->docType = docType;
53 }
54 Element Document::getElementByName(string name)
55 {
56  Element ele = getElementByName(name,this->root);
57  return ele;
58 }
59 Element Document::getElementByName(string name,Element ele)
60 {
61  if(ele.getTagName()==name)
62  return ele;
63  else
64  {
65  typedef vector<Element> ElementList;
66  ElementList chi = ele.getChildElements();
67  for(unsigned int i=0;i<chi.size();i++)
68  {
69  Element ele1 = getElementByName(name,chi.at(i));
70  if(!ele1.isNull())
71  return ele1;
72  }
73  }
74  Element nullele;
75  nullele.setTagName("&#@^_NULL_&#@^");
76  return nullele;
77 }
78 string Document::render()
79 {
80  string rend = this->docType;
81  Element element = this->getRootElement();
82  rend.append(generateStartOpenTag(element.getTagName()));
83  rend.append(generateAttributes(element.getAttributes()));
84  rend.append(generateEndOpenTag());
85  rend.append(element.getText());
86  ElementList elements = element.getChildElements();
87  for(unsigned int i=0;i<elements.size();i++)
88  {
89  rend.append(elements.at(i).render());
90  }
91  rend.append(generateCloseTag(element.getTagName()));
92  return rend;
93 }