static Collection<Element> createParentElements(Path path,Element rootElement,ElementMetadata<?,?> rootMetadata,int elementCount) throws ParseException {
  Preconditions.checkNotNull(path,"path");
  Preconditions.checkNotNull(rootElement,"rootElement");
  List<MetadataKey<?>> steps=path.getSteps();
  if (steps.isEmpty()) {
    return ImmutableList.of();
  }
  Element parent=rootElement;
  List<Element> parents=null;
  ElementMetadata<?,?> parentMetadata=rootMetadata;
  for (int i=0; i < steps.size() - 1; i++) {
    ElementKey<?,?> childKey=(ElementKey<?,?>)steps.get(i);
    ElementMetadata<?,?> childMetadata=(parentMetadata == null) ? null : parentMetadata.bindElement(childKey);
    if (parents != null) {
      checkNotMultiple(childMetadata);
      List<Element> children=Lists.newArrayListWithCapacity(parents.size());
      for (      Element p : parents) {
        children.add(getOrCreateChild(p,childKey));
      }
      parents=children;
      parentMetadata=childMetadata;
      continue;
    }
    if (childMetadata == null || childMetadata.getCardinality() == Cardinality.SINGLE) {
      Element child=getOrCreateChild(parent,childKey);
      parent=child;
      parentMetadata=childMetadata;
      continue;
    }
    List<Element> children=Lists.newArrayListWithCapacity(elementCount);
    for (int j=0; j < elementCount; j++) {
      try {
        Element child=Element.createElement(childKey);
        parent.addElement(child);
        children.add(child);
      }
 catch (      ContentCreationException e) {
        throw new ParseException(e);
      }
    }
    parents=children;
    parentMetadata=childMetadata;
  }
  if (parents != null) {
    return parents;
  }
  return ImmutableList.of(parent);
}

static Collection < Element > createParentElements ( Path path , Element rootElement , ElementMetadata < ? , ? > rootMetadata , int elementCount ) throws ParseException { Preconditions . checkNotNull ( path , "path" ) ; Preconditions . checkNotNull ( rootElement , "rootElement" ) ; List < MetadataKey < ? > > steps = path . getSteps ( ) ; if ( steps . isEmpty ( ) ) { return ImmutableList . of ( ) ; } Element parent = rootElement ; List < Element > parents = null ; ElementMetadata < ? , ? > parentMetadata = rootMetadata ; for ( int i = 0 ; i < steps . size ( ) - 1 ; i ++ ) { ElementKey < ? , ? > childKey = ( ElementKey < ? , ? > ) steps . get ( i ) ; ElementMetadata < ? , ? > childMetadata = ( parentMetadata == null ) ? null : parentMetadata . bindElement ( childKey ) ; if ( parents != null ) { checkNotMultiple ( childMetadata ) ; List < Element > children = Lists . newArrayListWithCapacity ( parents . size ( ) ) ; for ( Element p : parents ) { children . add ( getOrCreateChild ( p , childKey ) ) ; } parents = children ; parentMetadata = childMetadata ; continue ; } if ( childMetadata == null || childMetadata . getCardinality ( ) == Cardinality . SINGLE ) { Element child = getOrCreateChild ( parent , childKey ) ; parent = child ; parentMetadata = childMetadata ; continue ; } List < Element > children = Lists . newArrayListWithCapacity ( elementCount ) ; for ( int j = 0 ; j < elementCount ; j ++ ) { try { Element child = Element . createElement ( childKey ) ; parent . addElement ( child ) ; children . add ( child ) ; } catch ( ContentCreationException e ) { throw new ParseException ( e ) ; } } parents = children ; parentMetadata = childMetadata ; } if ( parents != null ) { return parents ; } return ImmutableList . of ( parent ) ; }
creates an array of parent elements for the parent element . this method requires the metadata in hand because we need to know where any multiple cardinality elements show up in the path .
