Package org.freezedry.persistence.tree

Examples of org.freezedry.persistence.tree.InfoNode


  {
    // create the root node of the tree, which holds the information about the
    // object we are being asked to persist.
    final Class< ? > clazz = object.getClass();
   
    InfoNode rootNode;
   
    // if the object is an array of one or more dimensions, then we need to replace the
    // "[]" which the array suffix (by default = "Array"). Then we use the array node
    // builder to create the semantic model.
    if( clazz.isArray() )
View Full Code Here


   * @param fieldName The name of the field for which to create the node
   * @return the top-level {@link InfoNode} for the object
   */
  public final InfoNode createNode( final Class< ? > containingClass, final Object object, final String fieldName )
  {
    InfoNode node;
    if( object == null )
    {
      return InfoNode.createLeafNode( fieldName, null, fieldName, Object.class );
    }

    // create the InfoNode object (we first have to determine the node type, down the road, we'll check the
    // factories for registered node info node builders for the Class< ? > of the object)
    final Class< ? > clazz = object.getClass();
   
    // construct the node. There are several cases to consider:
    // 0. the field is annotated with a specified node builder in mind
    // 1. the object is intended to be a leaf node: create a leaf InfoNode object
    //    with the appropriately populated values, and we're done.
    // 2. the object is of a special type: use the appropriate node factory
    //    to construct the appropriate node
    // 3. neither of the first two conditions are met: simply call the addNodes(...)
    //    method recursively to construct the nodes.
    // 4. the object is an array, and the array class hasn't been specified in the node
    //    builder map, then we must treat it as a generic array.
    // the first two cases are handled by the info node builders in the info node builders map. even
    // the leaf node info node builders may need to be overridden. the third case is handled
    // by the compound node
    if( containsAnnotatedNodeBuilder( containingClass, fieldName ) )
    {
      final NodeBuilder builder = getAnnotatedNodeBuilder( containingClass, fieldName );
      try
      {
        node = builder.createInfoNode( containingClass, object, fieldName );
      }
      catch( ReflectiveOperationException e )
      {
        final StringBuilder message = new StringBuilder();
        message.append( "Node Builder failed to create InfoNode:" ).append( Constants.NEW_LINE );
        message.append( "  Builder: " ).append( builder.getClass().getName() ).append( Constants.NEW_LINE );
        message.append( "  Containing Class Name: " ).append( containingClass.getName() ).append( Constants.NEW_LINE );
        message.append( "  Object: " ).append( clazz.getName() ).append( Constants.NEW_LINE );
        message.append( "  Field Name: " ).append( fieldName ).append( Constants.NEW_LINE );
        LOGGER.error( message.toString() );
        throw new IllegalStateException( message.toString(), e );
      }
    }
    else if( containsNodeBuilder( clazz ) )
    {
      final NodeBuilder builder = getNodeBuilder( clazz );
      try
      {
        // if the containing class is null, then this is a root object and we
        // must use the root object version of the createInfoNode method
        if( containingClass == null )
        {
          node = builder.createInfoNode( object, fieldName );
        }
        else
        {
          node = builder.createInfoNode( containingClass, object, fieldName );
        }
      }
      catch( ReflectiveOperationException e )
      {
        final StringBuilder message = new StringBuilder();
        message.append( "Node Builder failed to create InfoNode:" ).append( Constants.NEW_LINE );
        message.append( "  Builder: " ).append( builder.getClass().getName() ).append( Constants.NEW_LINE );
        message.append( "  Containing Class Name: " ).append( containingClass == null ? "[null]" : containingClass.getName() ).append( Constants.NEW_LINE );
        message.append( "  Object: " ).append( clazz.getName() ).append( Constants.NEW_LINE );
        message.append( "  Field Name: " ).append( fieldName ).append( Constants.NEW_LINE );
        LOGGER.error( message.toString() );
        throw new IllegalStateException( message.toString(), e );
      }
    }
    else if( clazz.isArray() )
    {
      try
      {
        node = genaralArrayNodeBuilder.createInfoNode( containingClass, object, fieldName );
      }
      catch( ReflectiveOperationException e )
      {
        final StringBuilder message = new StringBuilder();
        message.append( "Default Array Node Builder failed to create InfoNode:" ).append( Constants.NEW_LINE );
        message.append( "  Builder: " ).append( genaralArrayNodeBuilder.getClass().getName() ).append( Constants.NEW_LINE );
        message.append( "  Containing Class Name: " ).append( containingClass.getName() ).append( Constants.NEW_LINE );
        message.append( "  Object: " ).append( clazz.getName() ).append( Constants.NEW_LINE );
        message.append( "  Field Name: " ).append( fieldName ).append( Constants.NEW_LINE );
        LOGGER.error( message.toString() );
        throw new IllegalStateException( message.toString(), e );
      }
    }
    else
    {
      // create a new compound node to holds this, since it isn't a leaf node, and
      // call (recursively) the addNodes(...) method to add the nodes representing the
      // fields of this object to the newly created compound node.
      final InfoNode compoundNode = InfoNode.createCompoundNode( fieldName, fieldName, clazz );
      node = addNodes( compoundNode, object );
    }
   
    // then call addNodes(...) with the newly created node
    return node;
View Full Code Here

    }
   
    final String key = keyValues.get( 0 ).getFirst();
    final String value = keyValues.get( 0 ).getSecond();
    final String rawValue = getDecorator( value ).undecorate( value );
    final InfoNode node = InfoNode.createLeafNode( null, rawValue, key, null );
    parentNode.addChild( node );
  }
View Full Code Here

  {
    // create the map for holding the key-value pairs.
    final List< Pair< String, Object > > keyValuePairs = new ArrayList<>();

    // make a deep copy of the semantic model (since there are parts of the code that change the model)
    final InfoNode rootNode = rootInfoNode.getCopy();

    // recursively build the key-value pairs from the info-node tree
    buildKeyValuePairs( rootNode, rootNode.getPersistName(), keyValuePairs );
   
    // once complete, then return the list of key-value pairs
    return keyValuePairs;
  }
View Full Code Here

  {
    // grab the root key from all the values and use it to create the root info node.
    // recall that the root key should have the same name as the clazz we're using as
    // a template.
    final String rootKey = getRootKey( keyValues, clazz );
    final InfoNode rootNode = InfoNode.createRootNode( rootKey, clazz );
   
    // build the semantic model
    buildInfoNode( rootNode, keyValues );
   
    return rootNode;
View Full Code Here

     
    // the first/top of the json string must be the class name, so there should only be one key,
    // and one value associated with that key { "root_key" : { members } }, where members is
    // defined by { pair, members } and pair is defined by "key", "value" (see class documentation).
    // we still have to add the value as a node.
    final InfoNode rootNode = createRootNode( jsonObject, clazz );
   
    // grab the value and deal with one of the three possibilities:
    // 1. the value is a json object and therefore has name-value pairs
    // 2. the value is a json array and therefore has elements
    // 3. the value is neither, and therefore is a simple value
    // In the first two cases, we build out the remainder of the nodes recursively. In
    // the third case, we set in the value and class in root node and we're done.
    final Object value = JsonUtils.getValue( jsonObject, rootNode.getPersistName() );
    if( value instanceof JSONObject )
    {
      buildInfoNode( (JSONObject)value, rootNode );
    }
    else if( value instanceof JSONArray )
View Full Code Here

    for( String name : names )
    {
      final Object object = JsonUtils.getValue( jsonObject, name );
     
      // build the info node and add it to its parent
      final InfoNode newInfoNode = createInfoNode( name, object, infoNode );
      if( newInfoNode != null )
      {
        infoNode.addChild( newInfoNode );
      }
    }
View Full Code Here

    for( int i = 0; i < jsonArray.length(); ++i )
    {
      final Object object = JsonUtils.getElement( jsonArray, i );
     
      // build the info node and add it to its parent info node
      final InfoNode newInfoNode = createInfoNode( nodeName, object, infoNode );
      infoNode.addChild( newInfoNode );
    }
  }
View Full Code Here

    // there are three possibilities:
    // 1. the value is a json object, in which case the info node is compound
    // 2. the value is a json array, in which case the info node is compound
    // 3. the value is one of the remaining types (boolean, string, int, double, long, etc)
    //    in which case the info node is a leaf
    InfoNode node = null;
    if( value instanceof JSONObject )
    {
      node = InfoNode.createCompoundNode( null, nodeName, null );
      buildInfoNode( (JSONObject)value, node );
    }
View Full Code Here

      {
        renderer.buildInfoNode( parentNode, keyValues );
      }
      else
      {
        final InfoNode node = InfoNode.createCompoundNode( null, groupName, null );
        parentNode.addChild( node );
        buildInfoNode( node, keyValues );
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.freezedry.persistence.tree.InfoNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.