2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 #include <dali-toolkit/internal/builder/json-parser-impl.h>
22 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
23 #include <dali-toolkit/internal/builder/json-parser-state.h>
37 const char ERROR_DESCRIPTION_NONE[] = "No Error";
39 template <typename IteratorType,typename EndIteratorType>
40 inline IteratorType Advance(IteratorType& iter, EndIteratorType& end, int n)
42 for(int i =0; i < n; ++i)
52 JsonParser::JsonParser()
54 mErrorDescription(ERROR_DESCRIPTION_NONE),
63 JsonParser::JsonParser(const TreeNode& tree)
65 mErrorDescription(ERROR_DESCRIPTION_NONE),
72 mRoot = TreeNodeManipulator::Copy( tree, mNumberOfNodes, mNumberOfChars );
74 mSources.push_back( VectorChar( (sizeof(char) * mNumberOfChars) ) );
76 VectorChar& buffer = mSources.back();
78 VectorCharIter start = buffer.begin();
80 TreeNodeManipulator modify(mRoot);
82 modify.MoveStrings(start, buffer.end());
85 JsonParser::~JsonParser()
89 TreeNodeManipulator modify(mRoot);
90 modify.RemoveChildren();
96 int JsonParser::Parse(const std::string& source)
98 mSources.push_back( VectorChar(source.begin(), source.end()) );
100 JsonParserState parserState(mRoot);
102 if( parserState.ParseJson(mSources.back()) )
104 mRoot = parserState.GetRoot();
106 mNumberOfChars += parserState.GetParsedStringSize();
107 mNumberOfNodes += parserState.GetCreatedNodeCount();
109 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;
130 const TreeNode* JsonParser::GetRoot() const
135 bool JsonParser::ParseError() const
137 return mErrorDescription != ERROR_DESCRIPTION_NONE;
140 int JsonParser::GetErrorPosition() const
142 return mErrorPosition;
145 std::string JsonParser::GetErrorDescription() const
147 return std::string(mErrorDescription);
150 int JsonParser::GetErrorLineNumber() const
155 int JsonParser::GetErrorColumn() const
160 void JsonParser::Pack(void)
162 mSources.push_back( VectorChar( (sizeof(char) * mNumberOfChars) ) );
164 VectorChar& buffer = mSources.back();
166 VectorCharIter start = buffer.begin();
168 TreeNodeManipulator modify(mRoot);
170 modify.MoveStrings(start, buffer.end());
172 mSources.erase( mSources.begin(), --mSources.end() );
175 void JsonParser::Write(std::ostream& output, int indent) const
177 TreeNodeManipulator modify(mRoot);
178 modify.Write(output, indent);
182 } // namespace Internal
184 } // namespace Toolkit