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/tree-node-manipulator.h>
26 #include <dali-toolkit/internal/builder/json-parser-state.h>
40 const char ERROR_DESCRIPTION_NONE[] = "No Error";
42 template <typename IteratorType,typename EndIteratorType>
43 inline IteratorType Advance(IteratorType& iter, EndIteratorType& end, int n)
45 for(int i =0; i < n; ++i)
55 JsonParser::JsonParser()
57 mErrorDescription(ERROR_DESCRIPTION_NONE),
66 JsonParser::JsonParser(const TreeNode& tree)
68 mErrorDescription(ERROR_DESCRIPTION_NONE),
75 mRoot = TreeNodeManipulator::Copy( tree, mNumberOfNodes, mNumberOfChars );
77 mSources.push_back( VectorChar( (sizeof(char) * mNumberOfChars) ) );
79 VectorChar& buffer = mSources.back();
81 VectorCharIter start = buffer.begin();
83 TreeNodeManipulator modify(mRoot);
85 modify.MoveStrings(start, buffer.end());
88 JsonParser::~JsonParser()
92 TreeNodeManipulator modify(mRoot);
93 modify.RemoveChildren();
99 bool JsonParser::Parse(const std::string& source)
101 mSources.push_back( VectorChar(source.begin(), source.end()) );
103 JsonParserState parserState(mRoot);
105 if( parserState.ParseJson(mSources.back()) )
107 mRoot = parserState.GetRoot();
109 mNumberOfChars += parserState.GetParsedStringSize();
110 mNumberOfNodes += parserState.GetCreatedNodeCount();
112 mErrorDescription = ERROR_DESCRIPTION_NONE;
121 mErrorDescription = parserState.GetErrorDescription();
122 if(NULL == mErrorDescription)
124 mErrorDescription = ERROR_DESCRIPTION_NONE;
126 mErrorPosition = parserState.GetErrorPosition();
127 mErrorLine = parserState.GetErrorLineNumber();
128 mErrorColumn = parserState.GetErrorColumn();
131 return mRoot != NULL;
135 const TreeNode* JsonParser::GetRoot() const
140 bool JsonParser::ParseError() const
142 return mErrorDescription != ERROR_DESCRIPTION_NONE;
145 int JsonParser::GetErrorPosition() const
147 return mErrorPosition;
150 std::string JsonParser::GetErrorDescription() const
152 return std::string(mErrorDescription);
155 int JsonParser::GetErrorLineNumber() const
160 int JsonParser::GetErrorColumn() const
165 void JsonParser::Pack(void)
167 mSources.push_back( VectorChar( (sizeof(char) * mNumberOfChars) ) );
169 VectorChar& buffer = mSources.back();
171 VectorCharIter start = buffer.begin();
173 TreeNodeManipulator modify(mRoot);
175 modify.MoveStrings(start, buffer.end());
177 mSources.erase( mSources.begin(), --mSources.end() );
180 void JsonParser::Write(std::ostream& output, int indent) const
182 TreeNodeManipulator modify(mRoot);
183 modify.Write(output, indent);
187 } // namespace Internal
189 } // namespace Toolkit