summaryrefslogtreecommitdiffstats
path: root/cps-service
diff options
context:
space:
mode:
authormpriyank <priyank.maheshwari@est.tech>2023-01-05 11:04:34 +0000
committermpriyank <priyank.maheshwari@est.tech>2023-01-05 11:04:39 +0000
commitf7745da81ea488e55f717f765cf85eff8968d9c3 (patch)
tree7304aa91980c129d7f449b1061c378be9e62fa16 /cps-service
parentdd09115e991de04ad00333c4875ffc5c5c8e6f49 (diff)
Document and Transformer Factory refactor
- Extracted out document and transformer factory code Issue-ID: CPS-1435 Change-Id: I7c296a797871b0724f6de51833396a5d822c7d76 Signed-off-by: mpriyank <priyank.maheshwari@est.tech>
Diffstat (limited to 'cps-service')
-rw-r--r--cps-service/src/main/java/org/onap/cps/utils/XmlFileUtils.java17
1 files changed, 13 insertions, 4 deletions
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 b3402d6607..3030d702c2 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
@@ -50,7 +50,9 @@ import org.xml.sax.SAXException;
public class XmlFileUtils {
private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+ private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
private static boolean isNewDocumentBuilderFactoryInstance = true;
+ private static boolean isNewTransformerFactoryInstance = true;
private static final Pattern XPATH_PROPERTY_REGEX =
Pattern.compile("\\[@(\\S{1,100})=['\\\"](\\S{1,100})['\\\"]\\]");
@@ -109,10 +111,7 @@ public class XmlFileUtils {
&& !root.getTagName().equals(YangUtils.DATA_ROOT_NODE_TAG_NAME)) {
final Document documentWithRootNode = addDataRootNode(root, rootNodeTagName, namespace, rootNodeProperty);
documentWithRootNode.setXmlStandalone(true);
- final TransformerFactory transformerFactory = TransformerFactory.newInstance();
- transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
- transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
- final Transformer transformer = transformerFactory.newTransformer();
+ final Transformer transformer = getTransformerFactory().newTransformer();
final StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(documentWithRootNode), new StreamResult(stringWriter));
return stringWriter.toString();
@@ -171,4 +170,14 @@ public class XmlFileUtils {
return documentBuilderFactory;
}
+
+ private static TransformerFactory getTransformerFactory() {
+ if (isNewTransformerFactoryInstance) {
+ transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
+ transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
+ isNewTransformerFactoryInstance = false;
+ }
+
+ return transformerFactory;
+ }
}