Package org.freezedry.persistence.tree

Examples of org.freezedry.persistence.tree.InfoNode


    }
    if( persistName == null || persistName.isEmpty() )
    {
      persistName = fieldName;
    }
    final InfoNode node = InfoNode.createCompoundNode( fieldName, persistName, clazz );
   
    // set the entry, key, and value persistence names to the default value of the annotation. if the
    // map field is annotated, then we over write the default values with the annotated values.
    // does the class have a @PersistMap( keyPersistName = "xxxx", valuePersistName = "yyyy", entryPeristName = "zzzz" )
    String entryPersistName = PersistMap.ENTRY_PERSIST_NAME;
    String keyPersistName = PersistMap.KEY_PERSIST_NAME;
    String valuePersistName = PersistMap.VALUE_PERSIST_NAME;
    try
    {
      final Field field = ReflectionUtils.getDeclaredField( containingClass, fieldName );
      final PersistMap mapAnnotation = field.getAnnotation( PersistMap.class );
      if( mapAnnotation != null )
      {
        if( !mapAnnotation.entryPersistName().isEmpty() )
        {
          entryPersistName = mapAnnotation.entryPersistName();
        }
        if( !mapAnnotation.keyPersistName().isEmpty() )
        {
          keyPersistName = mapAnnotation.keyPersistName();
        }
        if( !mapAnnotation.valuePersistName().isEmpty() )
        {
          valuePersistName = mapAnnotation.valuePersistName();
        }
      }
    }
    catch( ReflectiveOperationException e )
    {
      final StringBuffer message = new StringBuffer();
      message.append( "Field not found in containing class:" + Constants.NEW_LINE );
      message.append( "  Containing class: " + containingClass.getName() + Constants.NEW_LINE );
      message.append( "  Field name: " + fieldName + Constants.NEW_LINE );
      LOGGER.info( message.toString() );
    }
   
    // run through the Map entries, recursively calling createNode(...) to create
    // the appropriate node which to add to the newly created compound node.
    for( Map.Entry< ?, ? > entry : ((Map< ?, ? >)object).entrySet() )
    {
      // create the map entry node
      final InfoNode entryNode = InfoNode.createCompoundNode( "", entryPersistName, entry.getClass() );
     
      // create the key node and add it to the entry node
      entryNode.addChild( createNode( clazz, entry.getKey(), keyPersistName ) );
     
      // create the value node and add it to the entry node
      entryNode.addChild( createNode( clazz, entry.getValue(), valuePersistName ) );
     
      // add the entry node to the info node representing the map
      node.addChild( entryNode );
    }
   
View Full Code Here


    // create the InfoNode object (we first have to determine the node type, down the road, we'll check the
    // factories for registered node generators for the Class< ? > of the object)
    final Class< ? > clazz = object.getClass();

    // create the root node
    final InfoNode node = InfoNode.createRootNode( clazz.getName(), clazz );

    // run through the Collection elements, recursively calling createNode(...) to create
    // the appropriate node which to add to the newly created compound node.
    // run through the Map entries, recursively calling createNode(...) to create
    // the appropriate node which to add to the newly created compound node.
    for( Map.Entry< ?, ? > entry : ((Map< ?, ? >)object).entrySet() )
    {
      // create the map entry node
      final InfoNode entryNode = InfoNode.createCompoundNode( "", entry.getClass().getSimpleName(), entry.getClass() );
     
      // create the key node and add it to the entry node
      entryNode.addChild( createNode( null, entry.getKey(), KEY_PREFIX + KEY_VALUE_SEPARATOR + entry.getKey().getClass().getName() ) );
     
      // create the value node and add it to the entry node
      entryNode.addChild( createNode( null, entry.getValue(), VALUE_PREFIX + KEY_VALUE_SEPARATOR + entry.getValue().getClass().getName() ) );
     
      // add the entry node to the info node representing the map
      node.addChild( entryNode );
    }
   
View Full Code Here

   
    // grab the group name for the collection, and create the compound node
    // that holds the elements of the collection as child nodes, and add it
    // to the parent node
    final String group = getGroupName( keyValues.get( 0 ).getFirst() );
    final InfoNode collectionNode = InfoNode.createCompoundNode( null, group, null );
    parentNode.addChild( collectionNode );
   
    // construct the patterns to determine if the node should be a compound node,
    // in which case we recurse back to the builder, or a leaf node, in which case
    // we simply create it here and add it to the collection node
    final String compoundRegex = "^" + group + decorationRegex;
    final Pattern compoundPattern = Pattern.compile( compoundRegex );
   
    final String leafRegex = compoundRegex + "$";
    final Pattern leafPattern = Pattern.compile( leafRegex );
   
    // run through the list of key-values creating the child nodes for the collection node
    final List< Pair< String, String > > copiedKeyValues = new ArrayList<>( keyValues );
    for( Pair< String, String > keyValue : keyValues )
    {
      // check to see if any items have been removed from the list. this could happen
      // when there is a compound node that we have combined, and removed all the entries
      // from this list
      if( !copiedKeyValues.contains( keyValue ) )
      {
        continue;
      }

      // grab the key
      final String key = keyValue.getFirst();
     
      // then we must figure out whether this is a compound node or a leaf node
      final Matcher compoundMatcher = compoundPattern.matcher( key );
      final Matcher leafMatcher = leafPattern.matcher( key );
      if( leafMatcher.find() )
      {
        // its a leaf, so now we need to figure out what the value is. we know that
        // it must be a number (integer, double) or a string.
        final String value = keyValue.getSecond();
        final Decorator decorator = getDecorator( value );
        final String rawValue = decorator.undecorate( value );
        final String persistName = decorator.representedClass().getSimpleName();
       
        // create the leaf info node and add it to the collection node
        final InfoNode elementNode = InfoNode.createLeafNode( null, rawValue, persistName, null );
        collectionNode.addChild( elementNode );
      }
      else if( compoundMatcher.find() )
      {
        // in this case, we'll have several entries that have the same index, so
View Full Code Here

     
      // the order of the elements can be reversed. For example, the key could be the second
      // element (instead of the first) and the value could be the first. We check both possibilities
      Object key = null;
      Object value = null;
      final InfoNode firstNode = keyValue.get( 0 );
      final InfoNode secondNode = keyValue.get( 1 );
      if( keyPersistenceName.equals( firstNode.getPersistName() ) &&
        valuePersistenceName.equals( secondNode.getPersistName() )  )
      {
        key = buildObject( containingClass, keyClass, keyTypes, firstNode, node );
        value = buildObject( containingClass, valueClass, valueTypes, secondNode, node );
      }
      else if( keyPersistenceName.equals( secondNode.getPersistName() ) &&
           valuePersistenceName.equals( firstNode.getPersistName() ) )
      {
        key = buildObject( containingClass, keyClass, keyTypes, secondNode, node );
        value = buildObject( containingClass, valueClass, valueTypes, firstNode, node );
      }
View Full Code Here

        throw new IllegalArgumentException( message.toString() );
      }
     
      // the order of the elements can be reversed. For example, the key could be the second
      // element (instead of the first) and the value could be the first. We check both possibilities
      final InfoNode firstNode = keyValue.get( 0 );
      final InfoNode secondNode = keyValue.get( 1 );
      Pair< Object, Object > keyValuePair = null;
      if( firstNode.getPersistName().startsWith( KEY_PREFIX + KEY_VALUE_SEPARATOR ) &&
        secondNode.getPersistName().startsWith( VALUE_PREFIX + KEY_VALUE_SEPARATOR ) )
      {
        keyValuePair = getKeyValuePair( firstNode, secondNode, node );
      }
      else if( firstNode.getPersistName().startsWith( VALUE_PREFIX + KEY_VALUE_SEPARATOR ) &&
           secondNode.getPersistName().startsWith( KEY_PREFIX + KEY_VALUE_SEPARATOR ) )
      {
        keyValuePair = getKeyValuePair( secondNode, firstNode, node );
      }

      // add the new objects to the map
View Full Code Here

    {
      // grab the group name for the collection, and create the compound node
      // that holds the elements of the collection as child nodes, and add it
      // to the parent node
      final String group = getGroupName( keyValues.get( 0 ).getFirst() );
      final InfoNode collectionNode = InfoNode.createCompoundNode( null, group, null );
      parentNode.addChild( collectionNode );
     
      // run through the list of key-values creating the child nodes for the collection node
      for( Pair< String, String > keyValue : keyValues )
      {
        // grab the key
        final String key = keyValue.getFirst();
       
        // this must match the validation pattern, i.e. that it is a simple collection. if
        // it doesn't match the simple collection pattern, the forward it to the compound
        // collection renderer (CollectionRenderer, this class' parent class)
        final Matcher leafMatcher = validationPattern.matcher( key );
        if( leafMatcher.find() )
        {
          // we should have list represented by "[ element1, element2, ..., elementN ]". We need to
          // pull apart the elements.
          final String valueList = keyValue.getSecond();
          final Matcher listMatcher = listPattern.matcher( valueList );
          if( listMatcher.find() )
          {
            // create the list of values
            final List< String > values = parseValueList( valueList );
           
            for( String value : values )
            {
              // its a leaf, so now we need to figure out what the value is. we know that
              // it must be a number (integer, double) or a string.
              final Decorator decorator = getDecorator( value);
              final String rawValue = decorator.undecorate( value );
              final String persistName = decorator.representedClass().getSimpleName();
             
              // create the leaf info node and add it to the collection node
              final InfoNode elementNode = InfoNode.createLeafNode( null, rawValue, persistName, null );
              collectionNode.addChild( elementNode );
            }
          }
        }
      }
View Full Code Here

   * @return The flattened version of the object
   * @see KeyValueBuilder
   */
  public final Map< String, Object > flattenObject( final Object object )
  {
    final InfoNode rootNode = persistenceEngine.createSemanticModel( object );
    return mapWriter.createMap( rootNode );
  }
View Full Code Here

  {
    // 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.
    // 5. the object is an enum, and must be treated as an enum, which may have a special get name method
    // 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 if( clazz.isEnum() )
    {
      node = generalEnumNodeBuilder.createInfoNode( containingClass, object, fieldName );
    }
    else
    {
      // check to see if the field has a generic type. for example, suppose that the containing
      // class is defined as: class A< T extends B > { .... }
      // if class A has a member defined as: T member
      // then we want to store the member's acutal class.
      String persistName = fieldName;
      try
      {
        if( containingClass != null && containingClass.getDeclaredField( fieldName ).getGenericType() != null )
        {
          final String className = object.getClass().getName().replace( ".", "_" );
          persistName += genericTypeSeparator + className;
        }
      }
      catch( NoSuchFieldException e )
      {
        /* this is empty purposely */
      }

      // 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, persistName, clazz );
      node = addNodes( compoundNode, object );
    }
   
    // then call addNodes(...) with the newly created node
    return 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

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.