Check NULL return value in builder-impl.cpp
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-impl.cpp
index 9b6daa0..7f1b441 100644 (file)
@@ -179,9 +179,10 @@ void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::U
   }
   else
   {
+    // load configuration map
+    LoadConfiguration( *parser.GetRoot(), mConfigurationMap );
     // load constant map (allows the user to override the constants in the json after loading)
     LoadConstants( *parser.GetRoot(), mReplacementMap );
-
     // merge includes
     if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
     {
@@ -201,7 +202,12 @@ void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::U
       }
     }
 
-    if( !mParser.Parse( data ) )
+    if( mParser.Parse( data ) )
+    {
+      // Drop the styles and get them to be rebuilt against the new parse tree as required.
+      mStyles.Clear();
+    }
+    else
     {
       DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
                         mParser.GetErrorLineNumber(),
@@ -212,8 +218,8 @@ void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::U
     }
   }
 
-  DUMP_PARSE_TREE(parser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
-  DUMP_TEST_MAPPINGS(parser);
+  DUMP_PARSE_TREE(mParser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
+  DUMP_TEST_MAPPINGS(mParser);
 
   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
 }
@@ -228,6 +234,11 @@ void Builder::AddConstant( const std::string& key, const Property::Value& value
   mReplacementMap[key] = value;
 }
 
+const Property::Map& Builder::GetConfigurations() const
+{
+  return mConfigurationMap;
+}
+
 const Property::Map& Builder::GetConstants() const
 {
   return mReplacementMap;
@@ -345,7 +356,7 @@ bool Builder::LookupStyleName( const std::string& styleName )
 
 const StylePtr Builder::GetStyle( const std::string& styleName )
 {
-  const StylePtr* style = mStyles.FindCaseInsensitiveC( styleName );
+  const StylePtr* style = mStyles.FindConst( styleName );
 
   if( style==NULL )
   {
@@ -773,6 +784,25 @@ Builder::~Builder()
 {
 }
 
+void Builder::LoadConfiguration( const TreeNode& root, Property::Map& intoMap )
+{
+  Replacement replacer(intoMap);
+
+  if( OptionalChild constants = IsChild(root, "config") )
+  {
+    for(TreeNode::ConstIterator iter = (*constants).CBegin();
+        iter != (*constants).CEnd(); ++iter)
+    {
+      Dali::Property::Value property;
+      if( (*iter).second.GetName() )
+      {
+        DeterminePropertyFromNode( (*iter).second, property, replacer );
+        intoMap[ (*iter).second.GetName() ] = property;
+      }
+    }
+  }
+}
+
 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
 {
   Replacement replacer(intoMap);
@@ -1092,7 +1122,7 @@ void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& nod
   StylePtr* matchedStyle = NULL;
   if( styleName )
   {
-    matchedStyle = mStyles.FindCaseInsensitive( styleName );
+    matchedStyle = mStyles.Find( styleName );
     if( ! matchedStyle )
     {
       OptionalChild styleNodes = IsChild(root, KEYNAME_STYLES);
@@ -1139,7 +1169,8 @@ void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& nod
   if( matchedStyle )
   {
     StylePtr style( *matchedStyle );
-    style->ApplyVisualsAndPropertiesRecursively( handle ); // (recurses through states)
+    Dictionary<Property::Map> instancedProperties;
+    style->ApplyVisualsAndPropertiesRecursively( handle, instancedProperties );
   }
   else // If there were no styles, instead set properties
   {
@@ -1180,7 +1211,7 @@ void Builder::RecordStyle( StylePtr           style,
           continue;
         }
 
-        StylePtr* stylePtr = style->subStates.FindCaseInsensitive( stateName );
+        StylePtr* stylePtr = style->subStates.Find( stateName );
         if( stylePtr )
         {
           StylePtr style(*stylePtr);
@@ -1203,63 +1234,35 @@ void Builder::RecordStyle( StylePtr           style,
         Dali::Property::Value property(Property::MAP);
         if( DeterminePropertyFromNode( visual.second, Property::MAP, property, replacements ) )
         {
-          Property::Map* mapPtr = style->visuals.FindCaseInsensitive( visual.first );
+          Property::Map* mapPtr = style->visuals.Find( visual.first );
           if( mapPtr )
           {
             // Override existing visuals
             mapPtr->Clear();
-            mapPtr->Merge(*property.GetMap());
+            mapPtr->Merge( *property.GetMap() );
           }
           else
           {
-            style->visuals.Add(visual.first, *property.GetMap());
+            Property::Map* map = property.GetMap();
+            if( map )
+            {
+              style->visuals.Add( visual.first, *map );
+            }
           }
         }
       }
     }
     else if( key == KEYNAME_ENTRY_TRANSITION )
     {
-      Dali::Property::Value property(Property::MAP);
-      if( DeterminePropertyFromNode( keyValue.second, Property::MAP, property, replacements ) )
-      {
-        style->entryTransition = Toolkit::TransitionData::New( *property.GetMap() );
-      }
+      RecordTransitionData( keyValue, style->entryTransition, replacements );
     }
     else if( key == KEYNAME_EXIT_TRANSITION )
     {
-      Dali::Property::Value property(Property::MAP);
-      if( DeterminePropertyFromNode( keyValue.second, Property::MAP, property, replacements ) )
-      {
-        style->exitTransition = Toolkit::TransitionData::New( *property.GetMap() );
-      }
+      RecordTransitionData( keyValue, style->exitTransition, replacements );
     }
     else if( key == KEYNAME_TRANSITIONS )
     {
-      //@todo add new transitions to style.transitions
-      //      override existing transitions. A transition matches on target & property name
-      const TreeNode& node = keyValue.second;
-      if( node.GetType() == TreeNode::ARRAY )
-      {
-        Dali::Property::Value property(Property::ARRAY);
-        if( DeterminePropertyFromNode( node, Property::ARRAY, property, replacements ) )
-        {
-          style->transitions = *property.GetArray();
-        }
-      }
-      else if( node.GetType() == TreeNode::OBJECT )
-      {
-        Dali::Property::Value property(Property::MAP);
-        if( DeterminePropertyFromNode( node, Property::MAP, property, replacements ) )
-        {
-          Property::Array propertyArray;
-          propertyArray.Add( property );
-          style->transitions = propertyArray;
-        }
-      }
-      else
-      {
-        DALI_LOG_WARNING( "RecordState() Node \"%s\" is not a JSON array or object\n", key.c_str() );
-      }
+      RecordTransitions( keyValue, style->transitions, replacements );
     }
     else if( key == KEYNAME_TYPE ||
              key == KEYNAME_ACTORS ||
@@ -1290,6 +1293,63 @@ void Builder::RecordStyle( StylePtr           style,
   }
 }
 
+void Builder::RecordTransitions(
+  const TreeNode::KeyNodePair& keyValue,
+  Property::Array& value,
+  const Replacement& replacements )
+{
+  //@todo add new transitions to style.transitions
+  //      override existing transitions. A transition matches on target & property name
+  const TreeNode& node = keyValue.second;
+  if( node.GetType() == TreeNode::ARRAY )
+  {
+    Dali::Property::Value property(Property::ARRAY);
+    if( DeterminePropertyFromNode( node, Property::ARRAY, property, replacements ) )
+    {
+      value = *property.GetArray();
+    }
+  }
+  else if( node.GetType() == TreeNode::OBJECT )
+  {
+    Dali::Property::Value property(Property::MAP);
+    if( DeterminePropertyFromNode( node, Property::MAP, property, replacements ) )
+    {
+      Property::Array propertyArray;
+      propertyArray.Add( property );
+      value = propertyArray;
+    }
+  }
+  else
+  {
+    DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON array or object\n", keyValue.first );
+  }
+}
+
+void Builder::RecordTransitionData(
+  const TreeNode::KeyNodePair& keyValue,
+  Toolkit::TransitionData& transitionData,
+  const Replacement& replacements )
+{
+  const TreeNode& node = keyValue.second;
+  if( node.GetType() == TreeNode::ARRAY )
+  {
+    Dali::Property::Value property(Property::ARRAY);
+    if( DeterminePropertyFromNode( keyValue.second, Property::ARRAY, property, replacements ) )
+    {
+      transitionData = Toolkit::TransitionData::New( *property.GetArray() );
+    }
+  }
+  else if( node.GetType() == TreeNode::OBJECT )
+  {
+    Dali::Property::Value property(Property::MAP);
+    if( DeterminePropertyFromNode( keyValue.second, Property::MAP, property, replacements ) )
+    {
+      transitionData = Toolkit::TransitionData::New( *property.GetMap() );
+    }
+  }
+}
+
+
 // Set properties from node on handle.
 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
                                Dali::Handle& handle, const Replacement& constant )