- writeValueByType(element, type, codecProvider, nodeValue);
- }
-
- public static void writeValueByType(Element element, TypeDefinition<?> type, XmlCodecProvider codecProvider, Object nodeValue) {
- TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
- if (baseType instanceof IdentityrefTypeDefinition) {
- if (nodeValue instanceof QName) {
- QName value = (QName) nodeValue;
- String prefix = "x";
- if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
- prefix = value.getPrefix();
- }
- element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
- element.setTextContent(prefix + ":" + value.getLocalName());
- } else {
- Object value = nodeValue;
- logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(),
- baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
- if (value != null) {
- element.setTextContent(String.valueOf(value));
- }
- }
- } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
- if (nodeValue instanceof InstanceIdentifier) {
- // Map< key = namespace, value = prefix>
- Map<String, String> prefixes = new HashMap<>();
- InstanceIdentifier instanceIdentifier = (InstanceIdentifier) nodeValue;
- StringBuilder textContent = new StringBuilder();
- for (PathArgument pathArgument : instanceIdentifier.getPath()) {
- textContent.append("/");
- writeIdentifierWithNamespacePrefix(element, textContent, pathArgument.getNodeType(), prefixes);
- if (pathArgument instanceof NodeIdentifierWithPredicates) {
- Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
-
- for (QName keyValue : predicates.keySet()) {
- String predicateValue = String.valueOf(predicates.get(keyValue));
- textContent.append("[");
- writeIdentifierWithNamespacePrefix(element, textContent, keyValue, prefixes);
- textContent.append("='");
- textContent.append(predicateValue);
- textContent.append("'");
- textContent.append("]");
- }
- } else if (pathArgument instanceof NodeWithValue) {
- textContent.append("[.='");
- textContent.append(((NodeWithValue)pathArgument).getValue());
- textContent.append("'");
- textContent.append("]");
- }
- }
- element.setTextContent(textContent.toString());
-
- } else {
- Object value = nodeValue;
- logger.debug("Value of {}:{} is not instance of InstanceIdentifier but is {}", baseType.getQName()
- .getNamespace(), //
- baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
- if (value != null) {
- element.setTextContent(String.valueOf(value));
- }
- }
- } else {
- if (nodeValue != null) {
- final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
- if (codec != null) {
- try {
- final String text = codec.serialize(nodeValue);
- element.setTextContent(text);
- } catch (ClassCastException e) {
- logger.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", nodeValue, baseType, e);
- element.setTextContent(String.valueOf(nodeValue));
- }
- } else {
- logger.error("Failed to find codec for {}, falling back to using stream", baseType);
- element.setTextContent(String.valueOf(nodeValue));