summaryrefslogtreecommitdiffstats
path: root/cps-service/src/main/java/org
diff options
context:
space:
mode:
authorPriyank Maheshwari <priyank.maheshwari@est.tech>2024-11-06 10:54:19 +0000
committerGerrit Code Review <gerrit@onap.org>2024-11-06 10:54:19 +0000
commit3399fdea15b9c7e6d05148f82f9ceeeee813b84a (patch)
treefa77f5d5d5ace6bfcd9cdcd1280198deb6d1b0fd /cps-service/src/main/java/org
parentedc650a3a5f5e86c9b82667b90a6bf759a095627 (diff)
parent14d6a9b990005965307bbab66b0ffe15327a8758 (diff)
Merge "XML content support on get a node"
Diffstat (limited to 'cps-service/src/main/java/org')
-rw-r--r--cps-service/src/main/java/org/onap/cps/utils/ContentType.java10
-rw-r--r--cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java87
2 files changed, 96 insertions, 1 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/utils/ContentType.java b/cps-service/src/main/java/org/onap/cps/utils/ContentType.java
index f888504843..eb8e592d9b 100644
--- a/cps-service/src/main/java/org/onap/cps/utils/ContentType.java
+++ b/cps-service/src/main/java/org/onap/cps/utils/ContentType.java
@@ -1,6 +1,7 @@
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2022 Deutsche Telekom AG
+ * Modifications Copyright (C) 2024 TechMahindra Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +21,14 @@
package org.onap.cps.utils;
+import org.springframework.http.MediaType;
+
public enum ContentType {
JSON,
- XML
+ XML;
+
+ public static ContentType fromString(final String contentTypeAsString) {
+ return contentTypeAsString.contains(MediaType.APPLICATION_XML_VALUE) ? XML : JSON;
+ }
+
}
diff --git a/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java b/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java
index 7a6d0bb3d5..94b97bd88f 100644
--- a/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java
+++ b/cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java
@@ -2,6 +2,7 @@
* ============LICENSE_START=======================================================
* Copyright (C) 2022 Deutsche Telekom AG
* Modifications Copyright (C) 2023-2024 Nordix Foundation.
+ * Modifications Copyright (C) 2024 TechMahindra Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,11 +22,13 @@
package org.onap.cps.utils;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -33,6 +36,7 @@ import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
@@ -40,10 +44,14 @@ import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.onap.cps.spi.exceptions.DataValidationException;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
import org.xml.sax.SAXException;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@@ -156,6 +164,85 @@ public class XmlFileUtils {
return document;
}
+ /**
+ * Convert a list of data maps to XML format.
+ *
+ * @param dataMaps List of data maps to convert
+ * @return XML string representation of the data maps
+ */
+ @SuppressFBWarnings(value = "DCN_NULLPOINTER_EXCEPTION")
+ public static String convertDataMapsToXml(final List<Map<String, Object>> dataMaps) {
+ try {
+ final DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
+ final Document document = documentBuilder.newDocument();
+ final DocumentFragment documentFragment = document.createDocumentFragment();
+ for (final Map<String, Object> dataMap : dataMaps) {
+ createXmlElements(document, documentFragment, dataMap);
+ }
+ return transformFragmentToString(documentFragment);
+ } catch (final DOMException | NullPointerException | ParserConfigurationException | TransformerException
+ exception) {
+ throw new DataValidationException(
+ "Data Validation Failed", "Failed to parse xml data: " + exception.getMessage(), exception);
+ }
+ }
+
+ private static void createXmlElements(final Document document, final Node parentNode,
+ final Map<String, Object> dataMap) {
+ for (final Map.Entry<String, Object> mapEntry : dataMap.entrySet()) {
+ if (mapEntry.getValue() instanceof List) {
+ appendList(document, parentNode, mapEntry);
+ } else if (mapEntry.getValue() instanceof Map) {
+ appendMap(document, parentNode, mapEntry);
+ } else {
+ appendObject(document, parentNode, mapEntry);
+ }
+ }
+ }
+
+ private static void appendList(final Document document, final Node parentNode,
+ final Map.Entry<String, Object> mapEntry) {
+ final List<Object> list = (List<Object>) mapEntry.getValue();
+ if (list.isEmpty()) {
+ final Element listElement = document.createElement(mapEntry.getKey());
+ parentNode.appendChild(listElement);
+ } else {
+ for (final Object element : list) {
+ final Element listElement = document.createElement(mapEntry.getKey());
+ if (element instanceof Map) {
+ createXmlElements(document, listElement, (Map<String, Object>) element);
+ } else {
+ listElement.appendChild(document.createTextNode(element.toString()));
+ }
+ parentNode.appendChild(listElement);
+ }
+ }
+ }
+
+ private static void appendMap(final Document document, final Node parentNode,
+ final Map.Entry<String, Object> mapEntry) {
+ final Element childElement = document.createElement(mapEntry.getKey());
+ createXmlElements(document, childElement, (Map<String, Object>) mapEntry.getValue());
+ parentNode.appendChild(childElement);
+ }
+
+ private static void appendObject(final Document document, final Node parentNode,
+ final Map.Entry<String, Object> mapEntry) {
+ final Element element = document.createElement(mapEntry.getKey());
+ element.appendChild(document.createTextNode(mapEntry.getValue().toString()));
+ parentNode.appendChild(element);
+ }
+
+ private static String transformFragmentToString(final DocumentFragment documentFragment)
+ throws TransformerException {
+ final Transformer transformer = getTransformerFactory().newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ final StringWriter writer = new StringWriter();
+ final StreamResult result = new StreamResult(writer);
+ transformer.transform(new DOMSource(documentFragment), result);
+ return writer.toString();
+ }
+
private static DocumentBuilderFactory getDocumentBuilderFactory() {
if (isNewDocumentBuilderFactoryInstance) {