ffead.server.doc
View.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  * View.cpp
18  *
19  * Created on: Sep 5, 2009
20  * Author: sumeet
21  */
22 #include "View.h"
23 typedef map<string,string> AttributeList;
24 typedef vector<Element> ElementList;
25 View::View()
26 {
27 
28 }
29 View::~View()
30 {
31 
32 }
33 
34 string generateStartOpenTag(string tagName)
35 {
36  string str = "<" + tagName;
37  return str;
38 }
39 string generateEndOpenTag()
40 {
41  string str = ">\n";
42  return str;
43 }
44 string generateCloseTag(string tagName)
45 {
46  string str = "</" + tagName + ">\n";
47  return str;
48 }
49 string generateAttributes(AttributeList attributes)
50 {
51  string str;
52  AttributeList::iterator itr;
53  for(itr = attributes.begin();itr!=attributes.end();itr++)
54  {
55  str += (" " + itr->first + "=\"" + itr->second + "\" ");
56  }
57  return str;
58 }
59 
60 void traverseElement(string *ss,Element element)
61 {
62  ss->append(generateStartOpenTag(element.getTagName()));
63  ss->append(generateAttributes(element.getAttributes()));
64  ss->append(generateEndOpenTag());
65  ss->append(element.getText());
66  ElementList elements = element.getChildElements();
67  for(unsigned int i=0;i<elements.size();i++)
68  {
69  traverseElement(ss,elements.at(i));
70  }
71  ss->append(generateCloseTag(element.getTagName()));
72 }
73 
74 string View::generateDocument(Document document)
75 {
76  Element root = document.getRootElement();
77  string ss;
78  traverseElement(&ss,root);
79  return ss;
80 }
81 
82