Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsTreeDeserializer.cpp
Go to the documentation of this file.
1 
14 #include "WsTreeDeserializer.h"
15 using namespace Json;
16 
17 WsTreeDeserializer::WsTreeDeserializer(const string& contents): m_contents(contents)
18 {
19 }
20 
22 {
23  if (parse() == ErrorCode::Failure)
24  return ErrorCode::Failure;
25  /* Get the rootPath */
26  m_rootPath = m_root["root_path"].asString();
27  m_stamp = m_root["stamp"].asString();
28  /* Create the root node */
30  path basePath(WsGlobalProperties::instance()->get("global", "root_path", "/"));
31  m_rootNode.get()->setProperties(NodePropertiesPtr(new WsNodeProperties(m_root["nodes"][basePath.string()]["properties"].toStyledString())));
32  /* Add all the children of the root */
33  return addSub(m_root["nodes"][m_rootPath.string()]["children"], m_rootNode);
34 }
35 
37 {
38  /* Iterate on nodes of current level in Json file and add it as children of node n */
39  for (ValueIterator itr = v.begin() ; itr != v.end() ; ++itr) {
40  Value v1 = *itr;
41  path p(itr.key().asString());
42  if (v1["type"].asString() == "DIRECTORY") {
43  /* Create node */
44  NodePtr temp = NodePtr(new WsDirNode(p, m_rootPath));
45  n.get()->addChildDirectory(temp);
46  /* Set its properties */
47  temp.get()->setProperties(NodePropertiesPtr(new WsNodeProperties(v1["properties"].toStyledString())));
48  temp.get()->setModifyDate(v1["modifdate"].asDouble());
49  temp.get()->setCreateDate(v1["creatdate"].asDouble());
50  /* No children */
51  if (v1["children"] == Value::null) {
52  continue;
53  }
54  /* Add children because current node is directory*/
55  addSub(v1["children"], temp);
56  } else {
57  /* current node is file, add only children to parent (n) */
58  NodePtr temp = NodePtr(new WsFileNode(p, m_rootPath));
59  n.get()->addChildFile(temp);
60  temp.get()->setProperties(NodePropertiesPtr(new WsNodeProperties(v1["properties"].toStyledString())));
61  temp.get()->setModifyDate(v1["modifdate"].asDouble());
62  temp.get()->setCreateDate(v1["creatdate"].asDouble());
63  temp.get()->setSize(v1["size"].asDouble());
64  }
65  }
66  return ErrorCode::Success;
67 }
68 
70 {
71  return m_stamp;
72 }
73 
74 
76 {
77  if (!m_reader.parse(m_contents, m_root, false)) {
78  LOG(ERROR) << "WsTreeDeserializer::parse() : Could not parse received input" << endl;
79  return ErrorCode::Failure;
80  }
81  return ErrorCode::Success;
82 }
83 
85 {
86  return m_rootNode;
87 }
WsTreeDeserializer(const std::string &contents)
Constructor.
boost::shared_ptr< WsAbstractNode > NodePtr
std::string m_contents
Serialized data that will be deserialized.
Reprensents a Directory on disk.
Definition: WsDirNode.h:25
const int Failure
const std::string & getStamp()
Get the Stamp of the serialized tree.
NodePtr m_rootNode
Root Node.
Reprensents a File on disk.
Definition: WsFileNode.h:23
Json::Value m_root
root of Json Tree
Properties of a WsNode.
boost::filesystem::path m_rootPath
The root path.
#define LOG
Definition: WsLogger.h:22
int deserialize()
deserialize the Json input
NodePtr getMenuRoot()
returns the menu root
int parse()
parse the Json tree
std::string m_stamp
stamp of the tree
Deserializes received contents on network.
const std::string Value
Definition: WsRequestType.h:39
int addSub(const Json::Value &v, NodePtr n)
adds a SubNode to the tree corresponding to the Json entry
boost::shared_ptr< WsNodeProperties > NodePropertiesPtr
static WsGlobalProperties * instance()
Json::Reader m_reader
Json reader.
const int Success
#define ERROR
Definition: WsLogger.h:42