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"); }
public static <T> String serializeValue( T value, PrismPropertyDefinition def, QName itemName, PrismContext prismContext, String langXml) 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(); 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; MapXNode xmap = new MapXNode(); xmap.put(def.getName(), xList); String s = prismContext.getParserDom().serializeToString(xmap, def.getName()); // 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 { PrismProperty pp = def.instantiate(); pVal = new PrismPropertyValue<T>(value); pp.add(pVal); XNode xnode = serializer.serializeItemValue(pVal, def); if (xnode == null) { throw new IllegalArgumentException("null node after serialization"); } MapXNode xmap = null; if (xnode instanceof RootXNode) { XNode sub = ((RootXNode) xnode).getSubnode(); if (!(sub instanceof MapXNode)) { throw new IllegalArgumentException("not uspported yet"); } xmap = (MapXNode) sub; } else if (xnode instanceof MapXNode) { xmap = (MapXNode) xnode; } else if (xnode instanceof PrimitiveXNode) { String s = ((PrimitiveXNode) xnode).getStringValue(); return s; // } else { throw new IllegalStateException("hmmm"); } XNode node = xmap.get(itemName); String s = prismContext.getParserDom().serializeToString(node, itemName); } 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"); }