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