2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/builder/json-parser-impl.h>
25 #include <dali-toolkit/internal/builder/json-parser-state.h>
26 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
36 const char ERROR_DESCRIPTION_NONE[] = "No Error";
38 template<typename IteratorType, typename EndIteratorType>
39 inline IteratorType Advance(IteratorType& iter, EndIteratorType& end, int n)
41 for(int i = 0; i < n; ++i)
50 JsonParser::JsonParser()
52 mErrorDescription(ERROR_DESCRIPTION_NONE),
61 JsonParser::JsonParser(const TreeNode& tree)
63 mErrorDescription(ERROR_DESCRIPTION_NONE),
70 mRoot = TreeNodeManipulator::Copy(tree, mNumberOfNodes, mNumberOfChars);
72 mSources.push_back(VectorChar((sizeof(char) * mNumberOfChars)));
74 VectorChar& buffer = mSources.back();
76 VectorCharIter start = buffer.begin();
78 TreeNodeManipulator modify(mRoot);
80 modify.MoveStrings(start, buffer.end());
83 JsonParser::~JsonParser()
87 TreeNodeManipulator modify(mRoot);
88 modify.RemoveChildren();
94 bool JsonParser::Parse(const std::string& source)
96 mSources.push_back(VectorChar(source.begin(), source.end()));
98 JsonParserState parserState(mRoot);
100 if(parserState.ParseJson(mSources.back()))
102 mRoot = parserState.GetRoot();
104 mNumberOfChars += parserState.GetParsedStringSize();
105 mNumberOfNodes += parserState.GetCreatedNodeCount();
107 mErrorDescription = ERROR_DESCRIPTION_NONE;
116 mErrorDescription = parserState.GetErrorDescription();
117 if(NULL == mErrorDescription)
119 mErrorDescription = ERROR_DESCRIPTION_NONE;
121 mErrorPosition = parserState.GetErrorPosition();
122 mErrorLine = parserState.GetErrorLineNumber();
123 mErrorColumn = parserState.GetErrorColumn();
126 return mRoot != NULL;
129 const TreeNode* JsonParser::GetRoot() const
134 bool JsonParser::ParseError() const
136 return mErrorDescription != ERROR_DESCRIPTION_NONE;
139 int JsonParser::GetErrorPosition() const
141 return mErrorPosition;
144 std::string JsonParser::GetErrorDescription() const
146 return std::string(mErrorDescription);
149 int JsonParser::GetErrorLineNumber() const
154 int JsonParser::GetErrorColumn() const
159 void JsonParser::Pack(void)
161 mSources.push_back(VectorChar((sizeof(char) * mNumberOfChars)));
163 VectorChar& buffer = mSources.back();
165 VectorCharIter start = buffer.begin();
167 TreeNodeManipulator modify(mRoot);
169 modify.MoveStrings(start, buffer.end());
171 mSources.erase(mSources.begin(), --mSources.end());
174 void JsonParser::Write(std::ostream& output, int indent) const
176 TreeNodeManipulator modify(mRoot);
177 modify.Write(output, indent);
180 } // namespace Internal
182 } // namespace Toolkit