97 lines
3.6 KiB
Java
97 lines
3.6 KiB
Java
package eu.dnetlib.validatorapi.utils;
|
|
|
|
import org.apache.camel.AggregationStrategy;
|
|
import org.apache.camel.Exchange;
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
import org.xml.sax.InputSource;
|
|
import org.xml.sax.SAXException;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import javax.xml.transform.TransformerException;
|
|
import javax.xml.xpath.*;
|
|
import java.io.IOException;
|
|
import java.io.StringReader;
|
|
|
|
public class ListSetAggregationStrategy implements AggregationStrategy {
|
|
|
|
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
|
|
if (oldExchange == null) {
|
|
return newExchange;
|
|
}
|
|
|
|
String oldBody = null;
|
|
try {
|
|
oldBody = exctractSets(oldExchange.getIn().getBody(String.class));
|
|
String newBody = exctractSets(newExchange.getIn().getBody(String.class));
|
|
oldExchange.getIn().setBody(oldBody + newBody);
|
|
|
|
} catch (ParserConfigurationException e) {
|
|
System.out.println(e);
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
System.out.println(e);
|
|
|
|
e.printStackTrace();
|
|
} catch (SAXException e) {
|
|
System.out.println(e);
|
|
|
|
e.printStackTrace();
|
|
} catch (TransformerException e) {
|
|
System.out.println(e);
|
|
|
|
e.printStackTrace();
|
|
} catch (XPathExpressionException e) {
|
|
System.out.println(e);
|
|
|
|
e.printStackTrace();
|
|
}
|
|
return oldExchange;
|
|
}
|
|
|
|
|
|
private String exctractSets(String xml) throws ParserConfigurationException, IOException, SAXException, TransformerException, XPathExpressionException {
|
|
// Create an XPathFactory
|
|
XPathFactory xfactory = XPathFactory.newInstance();
|
|
|
|
// Create an XPath object
|
|
XPath xpath = xfactory.newXPath();
|
|
|
|
// Compile the XPath expression to select the set elements
|
|
XPathExpression expression = xpath.compile("//set");
|
|
|
|
// Create an InputSource from the XML string
|
|
InputSource inputSource = new InputSource(new StringReader(xml));
|
|
|
|
// Parse the inputSource and create a Document object
|
|
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
|
|
|
|
// Evaluate the XPath expression on the document
|
|
NodeList setElements = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
|
|
|
|
// Process the extracted set elements
|
|
StringBuilder extractedElements = new StringBuilder();
|
|
for (int i = 0; i < setElements.getLength(); i++) {
|
|
extractedElements.append(getElementAsString(setElements.item(i)));
|
|
}
|
|
// System.out.println(extractedElements.toString());
|
|
return extractedElements.toString();
|
|
}
|
|
|
|
// Helper method to convert a Node to String
|
|
private static String getElementAsString(Node node) {
|
|
try {
|
|
javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory.newInstance();
|
|
javax.xml.transform.Transformer transformer = tf.newTransformer();
|
|
transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
|
java.io.StringWriter writer = new java.io.StringWriter();
|
|
transformer.transform(new javax.xml.transform.dom.DOMSource(node), new javax.xml.transform.stream.StreamResult(writer));
|
|
return writer.toString();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return "";
|
|
}
|
|
}
|
|
} |