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