public <IV extends PrismValue, ID extends ItemDefinition> XNode serializeItem(
      Item<IV, ID> item, SerializationContext ctx) throws SchemaException {
    ListXNode xlist = new ListXNode();
    List<IV> values = item.getValues();
    ItemDefinition definition = item.getDefinition();

    for (IV val : values) {
      XNode xsubnode = serializeItemValue(val, definition, ctx);
      xlist.add(xsubnode);
    }

    boolean asList;
    if (definition != null) {
      asList = definition.isMultiValue();
    } else {
      asList = values.size() > 1;
    }

    if (asList) {
      return xlist;
    } else {
      if (xlist.isEmpty()) {
        return null;
      } else {
        return xlist.iterator().next();
      }
    }
  }
  public static <T> String serializeValue(
      T value,
      ItemDefinition def,
      QName itemName,
      QName parentName,
      PrismContext prismContext,
      String language)
      throws SchemaException {
    //		System.out.println("value serialization");
    if (value == null) {
      return null;
    }

    XNodeSerializer serializer = prismContext.getXnodeProcessor().createSerializer();

    if (value instanceof List) {
      List<T> values = (List<T>) value;
      if (values.isEmpty()) {
        return null;
      }

      if (def instanceof PrismPropertyDefinition) {
        PrismProperty prop = (PrismProperty) def.instantiate(itemName);
        for (T val : values) {
          PrismPropertyValue<T> pValue = new PrismPropertyValue<T>(val);
          prop.add(pValue);
        }

        XNode node = serializer.serializeItem(prop);
        if (node instanceof ListXNode) {
          ListXNode xList = (ListXNode) node;
          if (xList.size() == 1) {
            XNode sub = xList.iterator().next();
            if (!(sub instanceof MapXNode)) {
              throw new IllegalArgumentException("must be a map");
            }

            String s = prismContext.getParserDom().serializeToString(sub, parentName);
            //						System.out.println("serialized: " + s);
            return s;

          } else {
            MapXNode xmap = new MapXNode();
            xmap.put(itemName, xList);
            String s = prismContext.getParserDom().serializeToString(xmap, parentName);
            //						System.out.println("serialized: " + s);
            return s;
            //						throw new IllegalArgumentException("Check your data.");
          }

          //					MapXNode xmap = xList.new MapXNode();
          //					xmap.put(def.getName(), xList);

        }
        String s = prismContext.getParserDom().serializeToString(node, def.getName());
        //				System.out.println("serialized: " + s);
        return s;
      } else if (def instanceof PrismContainerDefinition) {
        PrismContainer pc = (PrismContainer) def.instantiate();
        for (T val : values) {
          //					PrismContainerValue pcVal = new PrismContainerValue<Containerable>((Containerable)
          // val);
          PrismContainerValue pcVal = ((Containerable) val).asPrismContainerValue();
          pc.add(pcVal.clone());
        }
        XNode node = serializer.serializeItem(pc);
        if (node instanceof ListXNode) {
          ListXNode xList = (ListXNode) node;
          MapXNode xmap = new MapXNode();
          xmap.put(def.getName(), xList);
          String s = prismContext.getParserDom().serializeToString(xmap, parentName);
          //					System.out.println("serialized: " + s);
          return s;
        }
        String s = prismContext.getParserDom().serializeToString(node, def.getName());
        //				System.out.println("serialized: " + s);
        return s;
      }
    }

    PrismValue pVal = null;

    if (value instanceof Containerable) {
      pVal = ((Containerable) value).asPrismContainerValue();
    } else if (value instanceof Referencable) {
      pVal = ((Referencable) value).asReferenceValue();
    } else {
      pVal = new PrismPropertyValue<T>(value);
    }

    //		Class clazz = prismContext.getSchemaRegistry().determineCompileTimeClass(itemName);
    //		PrismContainerDefinition def =
    // prismContext.getSchemaRegistry().determineDefinitionFromClass(clazz);
    //
    //		ItemDefinition def =
    // prismContext.getSchemaRegistry().findItemDefinitionByElementName(itemName);

    //		QName itemName = null;
    if (def != null) {
      itemName = def.getName();
    }

    XNode node = serializer.serializeItemValue(pVal, def);
    String s = prismContext.getParserDom().serializeToString(node, itemName);
    //		System.out.println("serialized: " + s);
    return s;
    //		throw new UnsupportedOperationException("need to be implemented");
  }