Package org.freezedry.persistence.tree

Examples of org.freezedry.persistence.tree.InfoNode


    }
   
    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


          LOGGER.error( message.toString() );
          throw new IllegalStateException( message.toString() );
        }
       
        // find the info node that holds the key, and the info node that holds the value
        InfoNode keyNode = null;
        InfoNode valueNode = null;
        final String name1 = entryNodes.get( 0 ).getPersistName();
        final String name2 = entryNodes.get( 1 ).getPersistName();
        if( name1.equals( mapKeyName ) && name2.equals( mapValueName ) )
        {
          keyNode = entryNodes.get( 0 );
          valueNode = entryNodes.get( 1 );
        }
        else if( name2.equals( mapKeyName ) && name1.equals( mapValueName ) )
        {
          keyNode = entryNodes.get( 1 );
          valueNode = entryNodes.get( 0 );
        }
        else
        {
          final StringBuffer message = new StringBuffer();
          message.append( "The MapRenderer expects MapEntry nodes to have a key subnode and a value subnode." + Constants.NEW_LINE );
          message.append( "  Required Key Subnode Persist Name: " + mapKeyName + Constants.NEW_LINE );
          message.append( "  Required Value Subnode Persist Name: " + mapValueName + Constants.NEW_LINE );
          message.append( "  First Node's Persist Name: " + name1 + Constants.NEW_LINE );
          message.append( "  Second Node's Persist Name: " + name2 );
          LOGGER.error( message.toString() );
          throw new IllegalStateException( message.toString() );
        }
       
        // no we can continue to parse the nodes.
        String newKey = createKey( key, infoNode );
        if( keyNode.isLeafNode() )
        {
          String value;
          final Object object = keyNode.getValue();
          final Class< ? > clazz = object.getClass();
          if( containsDecorator( clazz ) )
          {
            value = getDecorator( clazz ).decorate( object );
          }
          else
          {
            value = object.toString();
          }
          newKey += keyDecorator.decorate( value );
        }
        else
        {
          // TODO currently we have a slight problem here for compound keys.
          final StringBuffer message = new StringBuffer();
          message.append( "The MapRenderer doesn't allow compound (composite) keys at this point." + Constants.NEW_LINE );
          message.append( "  Current Key: " + newKey + Constants.NEW_LINE );
          LOGGER.error( message.toString() );
          throw new IllegalStateException( message.toString() );
        }
       
        // create the key-value pair and return it. we know that we have value node, and that
        // the persistence name of the value is "Value" or something else set by the user. we
        // don't want to write that out, so we simply remove the value from the node.
        valueNode.setPersistName( "" );
        getPersistenceBuilder().createKeyValuePairs( valueNode, newKey, keyValues, true );
       
        // mark the node as processed so that it doesn't get processed again
        node.setIsProcessed( true );
      }
View Full Code Here

   
    // grab the group name for the map, and create the compound node
    // that holds the map entries of the map as child nodes, and add it
    // to the parent node
    final String group = getGroupName( keyValues.get( 0 ).getFirst() );
    final InfoNode mapNode = InfoNode.createCompoundNode( null, group, null );
    parentNode.addChild( mapNode );
   
    // 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 );
   
    // we want to have groups by the map key. the map key is the map key in the key-value pair that.
    // for example, in friends{"Polly"}, the map key is "Polly" (including the quotes). then we
    // can parse each group into its proper node.
    final Map< String, List< Pair< String, String > > > mapKeyGroups = getMapKeyGroups( keyValues );
    for( Map.Entry< String, List< Pair< String, String > > > entry : mapKeyGroups.entrySet() )
    {
      // for each group, i.e. each key, we need a map entry node attached to the map node.
      final InfoNode mapEntryNode = InfoNode.createCompoundNode( null, mapEntryName, null );
      mapNode.addChild( mapEntryNode );

      // grab the map key and the list of key-values associated with that map key
      final String mapKey = entry.getKey();
     
      // run through the list of key-values creating the child nodes for the map-entry node
      final List< Pair< String, String > > keyValueGroup = entry.getValue();
      final List< Pair< String, String > > copiedKeyValues = new ArrayList<>( keyValueGroup );
      for( Pair< String, String > keyValue : keyValueGroup )
      {
        // 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();
       
        // 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, create the key node
          final String rawMapKey = getDecorator( mapKey ).undecorate( mapKey );
          final InfoNode keyNode = InfoNode.createLeafNode( null, rawMapKey, mapKeyName, null );
          mapEntryNode.addChild( keyNode );
         
          // 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 String rawValue = getDecorator( value ).undecorate( value );
         
          // create the leaf info node and add it to the collection node
          final InfoNode valueNode = InfoNode.createLeafNode( null, rawValue, mapValueName, null );
          mapEntryNode.addChild( valueNode );
        }
        else if( compoundMatcher.find() )
        {
          // its a compound node, create the key node
          final String rawMapKey = getDecorator( mapKey ).undecorate( mapKey );
          final InfoNode keyNode = InfoNode.createLeafNode( null, rawMapKey, mapKeyName, null );
          mapEntryNode.addChild( keyNode );
         
          // in this case, we'll have several entries that have the same index, so
          // we'll need to pull those out and put them into a new key-value list
          final String separator = getPersistenceBuilder().getSeparator();
View Full Code Here

   
    // we must convert the object to the appropriate format
    final String date = DateUtils.createStringFromDate( (Calendar)object, dateFormat );
   
    // create a new leaf node with the new date string
    final InfoNode node = InfoNode.createLeafNode( fieldName, date, persistName, clazz );
   
    // return the node
    return node;
  }
View Full Code Here

    // grab the class for the object to persist
    final Class< ? > clazz = object.getClass();
   
    // we must convert the object to the appropriate format
    final String date = DateUtils.createStringFromDate( (Calendar)object, ISO_8601_DATE_FORMAT );
    final InfoNode stringNode = InfoNode.createLeafNode( "value", date, "value", String.class );

    // create the root node and add the string rep of the date
    final InfoNode node = InfoNode.createRootNode( persistName, clazz );
    node.addChild( stringNode );
   
    // return the node
    return node;
  }
View Full Code Here

   * @see org.freezedry.persistence.builders.AbstractLeafNodeBuilder#createObject(java.lang.Class, org.freezedry.persistence.tree.InfoNode)
   */
  @Override
  public Calendar createObject( final Class< ? > clazz, final InfoNode node )
  {
    final InfoNode valueNode = node.getChild( 0 );
    final String value = (String)valueNode.getValue();
    // try the ISO 8601 format (strict)
    Calendar date = null;
    try
    {
      date = DateUtils.createDateFromString( value, ISO_8601_DATE_FORMAT );
View Full Code Here

    }
    if( persistName == null || persistName.isEmpty() )
    {
      persistName = fieldName;
    }
    final InfoNode node = InfoNode.createCompoundNode( fieldName, persistName, clazz );
   
    // grab the annotations for this field and see if the persist name is specified
    // does the class have a @PersistCollection( elementPersistName = "xxxx" )
    String elementPersistName = null;
    try
    {
      // grab the array annotation if the containing class isn't null. If the containing class is null,
      // then later in the code we set the name for which to persist the elements to the classes simple
      // name with the compound array name suffix
      PersistArray arrayAnnotation = null;
      if( containingClass != null && !containingClass.isArray() )
      {
        final Field field = ReflectionUtils.getDeclaredField( containingClass, fieldName );
        arrayAnnotation = field.getAnnotation( PersistArray.class );
      }
      if( arrayAnnotation != null && !arrayAnnotation.elementPersistName().isEmpty() )
      {
        elementPersistName = arrayAnnotation.elementPersistName();
      }
    }
    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.warn( message.toString(), e );
    }
   
    // run through the Collection elements, recursively calling createNode(...) to create
    // the appropriate node which to add to the newly created compound node.
    for( int i = 0; i < Array.getLength( object ); ++i )
    {
      // grab the element of the array
      final Class< ? > elementClazz = object.getClass().getComponentType();
     
      String name;
      if( elementPersistName == null )
      {
        name = elementClazz.getSimpleName();
        if( elementClazz.isArray() )
        {
          name = name.replaceAll( "\\[\\]", compoundArrayNameSuffix );
        }
      }
      else
      {
        name = elementPersistName;
      }
     
      // grab the element and create the node. however, because the element may be a primitive
      // we need to set the node's class type to the actual element node. if we don't do this
      // then, for example, all ints will become Integers and if we ask for the type to be
      // set within the node, the type will be incorrect
      final Object element = Array.get( object, i );
      final InfoNode elementNode = createNode( clazz, element, name );
      elementNode.setClazz( elementClazz );
     
      // add the new node as a child to the parent
      node.addChild( elementNode );
    }
   
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 compound name
    final InfoNode node = InfoNode.createCompoundNode( persistName, persistName, clazz );
   
    // run through the Collection elements, recursively calling createNode(...) to create
    // the appropriate node which to add to the newly created compound node.
    for( int i = 0; i < Array.getLength( object ); ++i )
    {
      // grab the array's element type, and then its fully qualified name
      final Class< ? > elementClazz = object.getClass().getComponentType();
      final String name = elementClazz.getSimpleName();
     
      // grab the element and create the node. however, because the element may be a primitive
      // we need to set the node's class type to the actual element node. if we don't do this
      // then, for example, all ints will become Integers and if we ask for the type to be
      // set within the node, the type will be incorrect
      final Object element = Array.get( object, i );
      final InfoNode elementNode = createNode( null, element, name );
      elementNode.setClazz( elementClazz );
     
      // add the new node as a child to the parent
      node.addChild( elementNode );
    }
   
View Full Code Here

    {
      persistName = fieldName;
    }

    // create a new leaf node
    final InfoNode node = InfoNode.createLeafNode( fieldName, object, persistName, clazz );
   
    // return the node
    return node;
  }
View Full Code Here

    // grab the class for the object to persist
    final Class< ? > clazz = object.getClass();
   
    // create a new leaf node
    final String name = persistName;//clazz.getName();
    final InfoNode node = InfoNode.createLeafNode( name, object, name, clazz );
   
    // return the node
    return node;
  }
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.