-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
// EXTERNAL INCLUDES
#include <cstring>
// INTERNAL INCLUDES
#include <dali-toolkit/internal/builder/tree-node-manipulator.h>
-#include <dali-toolkit/public-api/builder/tree-node.h>
+#include <dali-toolkit/devel-api/builder/tree-node.h>
namespace Dali
{
namespace
{
-void Indent(std::ostream& o, int indent)
+void Indent(std::ostream& o, int level, int indentWidth)
{
- for (int i = 0; i < indent; ++i)
+ for (int i = 0; i < level*indentWidth; ++i)
{
o << " ";
}
}
+std::string EscapeQuotes( const char* aString)
+{
+ std::string escapedString;
+ int length = strlen(aString);
+ escapedString.reserve(length);
+
+ const char* end = aString+length;
+ for( const char* iter = aString; iter != end ; ++iter)
+ {
+ if(*iter != '\"')
+ {
+ escapedString.push_back(*iter);
+ }
+ else
+ {
+ escapedString.append("\\\"");
+ }
+ }
+ return escapedString;
}
+} // anonymous namespace
+
TreeNodeManipulator::TreeNodeManipulator(TreeNode* node)
: mNode(node)
{
if(tree.mName)
{
- numberChars += std::strlen(tree.mName);
+ numberChars += std::strlen(tree.mName) + 1;
}
if(TreeNode::STRING == tree.mType)
{
- numberChars += std::strlen(tree.mStringValue);
+ numberChars += std::strlen(tree.mStringValue) + 1;
}
++numberNodes;
}
}
}
+ else if( TreeNode::ARRAY == mNode->mType )
+ {
+ if( mNode->mFirstChild != NULL )
+ {
+ TreeNode::NodeType type = mNode->mFirstChild->GetType();
+
+ if( TreeNode::FLOAT == type || TreeNode::INTEGER == type )
+ {
+ // Arrays of numbers should be replaced, not appended to.
+ RemoveChildren();
+ }
+ }
+ }
}
void TreeNodeManipulator::SetName( const char* name )
void TreeNodeManipulator::Write(std::ostream& output, int indent) const
{
DALI_ASSERT_DEBUG(mNode && "Operation on NULL JSON node");
- DoWrite(mNode, output, indent);
+ DoWrite(mNode, output, 0, indent, false);
}
-void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, int indent) const
+void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, int level, int indentWidth, bool groupChildren) const
{
DALI_ASSERT_DEBUG(value && "Operation on NULL JSON node");
- Indent(output, indent);
+ if(!groupChildren)
+ {
+ Indent(output, level, indentWidth);
+ }
if (value->GetName())
{
output << "null";
if(NULL != value->mNextSibling)
{
- output << ",";
+ output << ", ";
}
- if(indent)
+ if( !groupChildren )
{
output << std::endl;
}
case TreeNode::OBJECT:
case TreeNode::ARRAY:
{
+ bool groupMyChildren = false;
+
+ if( TreeNode::ARRAY == value->GetType() && value->mFirstChild &&
+ ( TreeNode::INTEGER == value->mFirstChild->GetType() ||
+ TreeNode::FLOAT == value->mFirstChild->GetType() ) )
+ {
+ groupMyChildren = true;
+ }
+
if( value->GetType() == TreeNode::OBJECT)
{
+ output << std::endl;
+ Indent(output, level, indentWidth);
output << "{";
- if(indent)
- {
- output << std::endl;
- }
}
else
{
- output << "[";
- if(indent)
+ if( !groupMyChildren )
{
output << std::endl;
+ Indent(output, level, indentWidth);
}
+ output << "[";
+ }
+
+ if( groupMyChildren )
+ {
+ output << " ";
+ }
+ else
+ {
+ output << std::endl;
}
for (TreeNode::ConstIterator it = value->CBegin(); it != value->CEnd(); ++it)
{
- DoWrite( &((*it).second), output, indent + 1);
+ DoWrite( &((*it).second), output, level+1, indentWidth, groupMyChildren );
}
- Indent(output, indent);
+
+ if( !groupMyChildren )
+ {
+ Indent(output, level, indentWidth);
+ }
+
if( value->GetType() == TreeNode::OBJECT )
{
output << "}";
- if(indent)
- {
- output << std::endl;
- }
}
else
{
output << "]";
- if(indent)
- {
- output << std::endl;
- }
}
+
+ if( NULL != value->mNextSibling )
+ {
+ output << ",";
+ }
+
+ if( !groupChildren )
+ {
+ output << std::endl;
+ }
+
+ groupChildren = false;
break;
}
case TreeNode::STRING:
{
- output << "\"" << value->GetString() << "\"";
+ std::string escapedString = EscapeQuotes(value->GetString());
+ output << "\"" << escapedString << "\"";
if(NULL != value->mNextSibling)
{
output << ",";
}
- if(indent)
+
+ if( groupChildren )
+ {
+ output << " ";
+ }
+ else
{
output << std::endl;
}
-
break;
}
case TreeNode::INTEGER:
{
output << ",";
}
- if(indent)
+
+ if( groupChildren )
+ {
+ output << " ";
+ }
+ else
{
output << std::endl;
}
-
break;
}
case TreeNode::FLOAT:
{
output << ",";
}
- if(indent)
+
+ if( groupChildren )
+ {
+ output << " ";
+ }
+ else
{
output << std::endl;
}
{
output << "false";
}
+
if(NULL != value->mNextSibling)
{
output << ",";
}
- if(indent)
+
+ if( groupChildren )
+ {
+ output << " ";
+ }
+ else
{
output << std::endl;
}
} // namespace Toolkit
} // namespace Dali
-