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