private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type, final Object defaultValue) { TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type); if (typeDefAwareCodec != null) { Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue); return codec.deserialize(castedDefaultValue); } // FIXME: BUG-4647 Refactor / redesign this to throw hard error, // once BUG-4638 is fixed and will provide proper getDefaultValue implementation. return null; }
@SuppressWarnings("unchecked") public static TypeDefinitionAwareCodec<Object, ?> from(final TypeDefinition<?> typeDefinition) { return (TypeDefinitionAwareCodec<Object, ?>) fromType(typeDefinition); }
private static String serialize(final @NonNull TypeDefinition<?> type, final @NonNull Object value) { final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type); if (codec == null) { LOG.warn("Failed to find codec for {}, falling back to using stream", type); return value.toString(); } try { return codec.serialize(value); } catch (ClassCastException e) { LOG.warn("Provided node value {} did not have type {} required by mapping. Using stream instead.", value, type, e); return value.toString(); } }
@Override @SuppressWarnings("checkstyle:illegalCatch") public Object deserialize(final String stringRepresentation) { if (!getTypeDefinition().isPresent()) { return stringRepresentation; } for (final TypeDefinition<?> type : getTypeDefinition().get().getTypes()) { final TypeDefinitionAwareCodec<Object, ?> typeAwareCodec = from(type); if (typeAwareCodec == null) { /* * This is a type for which we have no codec (eg identity ref) so we'll say it's * valid */ return stringRepresentation; } try { return typeAwareCodec.deserialize(stringRepresentation); } catch (final Exception e) { LOG.debug("Value {} did not matched representation for {}",stringRepresentation,type,e); // invalid - try the next union type. } } throw new IllegalArgumentException("Invalid value \"" + stringRepresentation + "\" for union type."); } }
@SuppressWarnings("unchecked") @Override public Object serialize(final Object input) { try { if (type instanceof IdentityrefTypeDefinition) { return identityrefCodec.serialize(input); } else if (type instanceof LeafrefTypeDefinition) { return LEAFREF_DEFAULT_CODEC.serialize(input); } else if (type instanceof InstanceIdentifierTypeDefinition) { return instanceIdentifier.serialize(input); } else { final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec .from(type); if (typeAwarecodec != null) { return typeAwarecodec.serialize(input); } else { logger.debug("Codec for type \"" + type.getQName().getLocalName() + "\" is not implemented yet."); return null; } } } catch (final ClassCastException e) { // TODO remove this catch when everyone use codecs logger.error( "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), e); return input; } }
@Override @SuppressWarnings("checkstyle:illegalCatch") public Object deserialize(final String stringRepresentation) { if (!getTypeDefinition().isPresent()) { return stringRepresentation; } for (final TypeDefinition<?> type : getTypeDefinition().get().getTypes()) { final TypeDefinitionAwareCodec<Object, ?> typeAwareCodec = from(type); if (typeAwareCodec == null) { /* * This is a type for which we have no codec (eg identity ref) so we'll say it's * valid */ return stringRepresentation; } try { return typeAwareCodec.deserialize(stringRepresentation); } catch (final Exception e) { LOG.debug("Value {} did not matched representation for {}",stringRepresentation,type,e); // invalid - try the next union type. } } throw new IllegalArgumentException("Invalid value \"" + stringRepresentation + "\" for union type."); } }
private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type, final Object defaultValue) { final TypeDefinitionAwareCodec<?, ?> typeDefAwareCodec = TypeDefinitionAwareCodec.from(type); if (typeDefAwareCodec != null) { final Object castedDefaultValue = typeDefAwareCodec.deserialize((String) defaultValue); return codec.deserialize(castedDefaultValue); } // FIXME: BUG-4647 Refactor / redesign this to throw hard error, // once BUG-4638 is fixed and will provide proper getDefaultValue implementation. return null; }
@SuppressWarnings("unchecked") public static TypeDefinitionAwareCodec<Object, ?> from(final TypeDefinition<?> typeDefinition) { return (TypeDefinitionAwareCodec<Object, ?>) fromType(typeDefinition); }
private Object getValue(String value){ if(schemaNode != null){ if(schemaNode instanceof LeafListSchemaNode){ return TypeDefinitionAwareCodec .from(LeafListSchemaNode.class.cast(schemaNode).getType()).deserialize(value); } } return value; }
private Object getValue(QName key, String value){ if(listSchemaNode != null){ for(DataSchemaNode node : listSchemaNode.getChildNodes()){ if(node instanceof LeafSchemaNode && node.getQName().equals(key)){ return TypeDefinitionAwareCodec.from(LeafSchemaNode.class.cast(node).getType()).deserialize(value); } } } return value; } }
} else { final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec = TypeDefinitionAwareCodec .from(type); if (typeAwarecodec != null) { if (input instanceof IdentityValuesDTO) { return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue()); return typeAwarecodec.deserialize(String.valueOf(input)); } else { logger.debug("Codec for type \"" + type.getQName().getLocalName()