Add post processor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / json-parser-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include <dali-toolkit/internal/builder/json-parser-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/builder/json-parser-state.h>
26 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 namespace
35 {
36 const char ERROR_DESCRIPTION_NONE[] = "No Error";
37
38 template<typename IteratorType, typename EndIteratorType>
39 inline IteratorType Advance(IteratorType& iter, EndIteratorType& end, int n)
40 {
41   for(int i = 0; i < n; ++i)
42   {
43     ++iter;
44   }
45   return iter;
46 }
47
48 } // namespace
49
50 JsonParser::JsonParser()
51 : mRoot(NULL),
52   mErrorDescription(ERROR_DESCRIPTION_NONE),
53   mErrorPosition(0),
54   mErrorLine(0),
55   mErrorColumn(0),
56   mNumberOfChars(0),
57   mNumberOfNodes(0)
58 {
59 }
60
61 JsonParser::JsonParser(const TreeNode& tree)
62 : mRoot(NULL),
63   mErrorDescription(ERROR_DESCRIPTION_NONE),
64   mErrorPosition(0),
65   mErrorLine(0),
66   mErrorColumn(0),
67   mNumberOfChars(0),
68   mNumberOfNodes(0)
69 {
70   mRoot = TreeNodeManipulator::Copy(tree, mNumberOfNodes, mNumberOfChars);
71
72   mSources.push_back(VectorChar((sizeof(char) * mNumberOfChars)));
73
74   VectorChar& buffer = mSources.back();
75
76   VectorCharIter start = buffer.begin();
77
78   TreeNodeManipulator modify(mRoot);
79
80   modify.MoveStrings(start, buffer.end());
81 }
82
83 JsonParser::~JsonParser()
84 {
85   if(mRoot)
86   {
87     TreeNodeManipulator modify(mRoot);
88     modify.RemoveChildren();
89     delete mRoot;
90     mRoot = NULL;
91   }
92 }
93
94 bool JsonParser::Parse(const std::string& source)
95 {
96   mSources.push_back(VectorChar(source.begin(), source.end()));
97
98   JsonParserState parserState(mRoot);
99
100   if(parserState.ParseJson(mSources.back()))
101   {
102     mRoot = parserState.GetRoot();
103
104     mNumberOfChars += parserState.GetParsedStringSize();
105     mNumberOfNodes += parserState.GetCreatedNodeCount();
106
107     mErrorDescription = ERROR_DESCRIPTION_NONE;
108     mErrorPosition    = 0;
109     mErrorLine        = 0;
110     mErrorColumn      = 0;
111   }
112   else
113   {
114     mRoot = NULL;
115
116     mErrorDescription = parserState.GetErrorDescription();
117     if(NULL == mErrorDescription)
118     {
119       mErrorDescription = ERROR_DESCRIPTION_NONE;
120     }
121     mErrorPosition = parserState.GetErrorPosition();
122     mErrorLine     = parserState.GetErrorLineNumber();
123     mErrorColumn   = parserState.GetErrorColumn();
124   }
125
126   return mRoot != NULL;
127 }
128
129 const TreeNode* JsonParser::GetRoot() const
130 {
131   return mRoot;
132 }
133
134 bool JsonParser::ParseError() const
135 {
136   return mErrorDescription != ERROR_DESCRIPTION_NONE;
137 }
138
139 int JsonParser::GetErrorPosition() const
140 {
141   return mErrorPosition;
142 }
143
144 std::string JsonParser::GetErrorDescription() const
145 {
146   return std::string(mErrorDescription);
147 }
148
149 int JsonParser::GetErrorLineNumber() const
150 {
151   return mErrorLine;
152 }
153
154 int JsonParser::GetErrorColumn() const
155 {
156   return mErrorColumn;
157 }
158
159 void JsonParser::Pack(void)
160 {
161   mSources.push_back(VectorChar((sizeof(char) * mNumberOfChars)));
162
163   VectorChar& buffer = mSources.back();
164
165   VectorCharIter start = buffer.begin();
166
167   TreeNodeManipulator modify(mRoot);
168
169   modify.MoveStrings(start, buffer.end());
170
171   mSources.erase(mSources.begin(), --mSources.end());
172 }
173
174 void JsonParser::Write(std::ostream& output, int indent) const
175 {
176   TreeNodeManipulator modify(mRoot);
177   modify.Write(output, indent);
178 }
179
180 } // namespace Internal
181
182 } // namespace Toolkit
183
184 } // namespace Dali