Package org.freezedry.persistence.tree

Examples of org.freezedry.persistence.tree.InfoNode


    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

   
    final InputStream inputStream = new BufferedInputStream( new FileInputStream( "test.json" ) );
    final Reader input = new InputStreamReader( inputStream );
    final JsonReader reader = new JsonReader();
    final Class< ? > inputClazz = int[].class;
    final InfoNode infoNode = reader.read( inputClazz, input );
    System.out.println( infoNode.simpleTreeToString() );
   
    final PersistenceEngine engine = new PersistenceEngine();
    final Object reperson = engine.parseSemanticModel( inputClazz, infoNode );
    System.out.println( reperson );
  }
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

          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

    {
      persistName = fieldName;
    }

    // create a compound node that holds the child nodes that forms the element of the Collection.
    final InfoNode node = InfoNode.createCompoundNode( fieldName, 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( Object element : (Collection< ? >)object )
    {
      String name;
      if( elementPersistName == null )
      {
        name = element.getClass().getSimpleName();
      }
      else
      {
        name = elementPersistName;
      }
      node.addChild( createNode( clazz, element, name ) );
    }
   
    return node;
  }
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( 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( Object element : (Collection< ? >)object )
    {
      node.addChild( createNode( null, element, element.getClass().getName() ) );
    }
   
    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.