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>
23 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
24 #include <dali-toolkit/internal/builder/json-parser-state.h>
38 const char ERROR_DESCRIPTION_NONE[] = "No Error";
40 template <typename IteratorType,typename EndIteratorType>
41 inline IteratorType Advance(IteratorType& iter, EndIteratorType& end, int n)
43 for(int i =0; i < n; ++i)
53 JsonParser::JsonParser()
55 mErrorDescription(ERROR_DESCRIPTION_NONE),
64 JsonParser::JsonParser(const TreeNode& tree)
66 mErrorDescription(ERROR_DESCRIPTION_NONE),
73 mRoot = TreeNodeManipulator::Copy( tree, mNumberOfNodes, mNumberOfChars );
75 mSources.push_back( VectorChar( (sizeof(char) * mNumberOfChars) ) );
77 VectorChar& buffer = mSources.back();
79 VectorCharIter start = buffer.begin();
81 TreeNodeManipulator modify(mRoot);
83 modify.MoveStrings(start, buffer.end());
86 JsonParser::~JsonParser()
90 TreeNodeManipulator modify(mRoot);
91 modify.RemoveChildren();
97 int JsonParser::Parse(const std::string& source)
99 mSources.push_back( VectorChar(source.begin(), source.end()) );
101 JsonParserState parserState(mRoot);
103 if( parserState.ParseJson(mSources.back()) )
105 mRoot = parserState.GetRoot();
107 mNumberOfChars += parserState.GetParsedStringSize();
108 mNumberOfNodes += parserState.GetCreatedNodeCount();
110 mErrorDescription = ERROR_DESCRIPTION_NONE;
117 mErrorDescription = parserState.GetErrorDescription();
118 if(NULL == mErrorDescription)
120 mErrorDescription = ERROR_DESCRIPTION_NONE;
122 mErrorPosition = parserState.GetErrorPosition();
123 mErrorLine = parserState.GetErrorLineNumber();
124 mErrorColumn = parserState.GetErrorColumn();
127 return mRoot != NULL;
131 const TreeNode* JsonParser::GetRoot() const
136 bool JsonParser::ParseError() const
138 return mErrorDescription != ERROR_DESCRIPTION_NONE;
141 int JsonParser::GetErrorPosition() const
143 return mErrorPosition;
146 std::string JsonParser::GetErrorDescription() const
148 return std::string(mErrorDescription);
151 int JsonParser::GetErrorLineNumber() const
156 int JsonParser::GetErrorColumn() const
161 void JsonParser::Pack(void)
163 mSources.push_back( VectorChar( (sizeof(char) * mNumberOfChars) ) );
165 VectorChar& buffer = mSources.back();
167 VectorCharIter start = buffer.begin();
169 TreeNodeManipulator modify(mRoot);
171 modify.MoveStrings(start, buffer.end());
173 mSources.erase( mSources.begin(), --mSources.end() );
176 void JsonParser::Write(std::ostream& output, int indent) const
178 TreeNodeManipulator modify(mRoot);
179 modify.Write(output, indent);
183 } // namespace Internal
185 } // namespace Toolkit