Skip to content

Commit

Permalink
Added new classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoonen committed Jun 12, 2019
1 parent 4fa5cdc commit a0598bc
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
148 changes: 148 additions & 0 deletions brmo-loader/src/main/java/nl/b3p/brmo/loader/RsgbBRPTransformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package nl.b3p.brmo.loader;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import nl.b3p.brmo.loader.entity.Bericht;
import nl.b3p.brmo.loader.util.RsgbTransformer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
*
* @author meine
*/
public class RsgbBRPTransformer extends RsgbTransformer {

private static final Log log = LogFactory.getLog(RsgbBRPTransformer.class);
private StagingProxy staging;

public RsgbBRPTransformer(String pathToXsl, StagingProxy staging) throws TransformerConfigurationException, ParserConfigurationException {
super(pathToXsl);
this.staging = staging;
}

@Override
public String transformToDbXml(Bericht bericht) throws SAXException, IOException, TransformerConfigurationException, TransformerException {
String current = super.transformToDbXml(bericht);
StringBuilder loadLog = new StringBuilder();

try {
Bericht old = staging.getOldBericht(bericht, loadLog);

if (old != null) {
Document d = merge( old.getDbXml(), current);
String mergedDBXML = print(d);
bericht.setDbXml(mergedDBXML);
current = mergedDBXML;
}
} catch (SQLException ex) {
log.error("Cannot retrieve old bericht: ", ex);
} catch (Exception ex) {
log.error("Cannot retrieve old bericht: ", ex);
}

// retrieve old bericht
// apply current to old
// return modified dbxml
return current;
}

@Override
public Node transformToDbXmlNode(Bericht bericht) throws SAXException, IOException, TransformerConfigurationException, TransformerException {
Node n = super.transformToDbXmlNode(bericht);

// retrieve old bericht
// apply current to old
// return modified dbxml
return n;
}

protected static Document merge(String oldFile, String newFile) throws Exception {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expression = xpath.compile("/root/data");

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document base = docBuilder.parse(new InputSource(new StringReader(oldFile)));

Element old = (Element) expression.evaluate(base, XPathConstants.NODE);
if (old == null) {
throw new IOException(oldFile + ": expression does not evaluate to node");
}

Document merge = docBuilder.parse(new InputSource(new StringReader(newFile)));
Node newNode = (Node) expression.evaluate(merge, XPathConstants.NODE);

/*
(1)Voor elke node in merge, kijk of hij bestaat in base
zo nee, importeer
zo ja, kijk of dit het een na diepste niveau is
zo ja,
ga voor elk childnode na of deze bestaat
zo nee, importeer
zo ja, overschrijf waarde
zo nee, recurse in (1)
*/

merge(base,newNode, old, true/*, merge*/);


return base;
}

private static void merge(Document base, Node newNode, Element old, boolean first/*, Node merge*/){
while (newNode.hasChildNodes()) {
Node mergeChild = newNode.getFirstChild();
newNode.removeChild(mergeChild);
String name = mergeChild.getNodeName();
NodeList nl = old.getElementsByTagName(name);
if(nl.getLength() == 0){
mergeChild = base.importNode(mergeChild, true);
old.appendChild(mergeChild);
}else{
Element oldItem = (Element) nl.item(0);
if(first){
merge(base, mergeChild, oldItem, false);
}else{
if(!mergeChild.getTextContent().equalsIgnoreCase("")){
oldItem.setTextContent(mergeChild.getTextContent());
}
}
}
}
}

protected static String print(Document doc) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StringWriter sw = new StringWriter();
Result result = new StreamResult(sw);
transformer.transform(source, result);
return sw.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package nl.b3p.brmo.loader;

import java.io.StringWriter;
import org.apache.commons.io.IOUtils;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
*
* @author meine
*/


public class RsgbBRPTransformerTest {

@Test
public void mergeTest() throws Exception{
StringWriter writerOld = new StringWriter();
IOUtils.copy(RsgbBRPTransformerTest.class.getResourceAsStream("old.xml"), writerOld, "UTF-8");
String old = writerOld.toString();

StringWriter writerNew = new StringWriter();
IOUtils.copy(RsgbBRPTransformerTest.class.getResourceAsStream("new.xml"), writerNew, "UTF-8");
String newFile = writerNew.toString();

Document doc = RsgbBRPTransformer.merge( old, newFile);
NodeList data = doc.getElementsByTagName("data");
assertEquals("Too many data elems", 1,data.getLength());
Node dataNode = data.item(0);
//assertEquals("Too many children in data", 6, dataChildren.getLength());
NodeList huw = doc.getElementsByTagName("huw_ger_partn");
assertEquals("Huwelijk not present", 1, huw.getLength());
NodeList subjectList = doc.getElementsByTagName("subject");
assertEquals("too many subjects", 1, subjectList.getLength());
Element subject = (Element)subjectList.item(0);
assertEquals("new value in adres_buitenland not merged", "daaro", subject.getElementsByTagName("adres_buitenland").item(0).getTextContent() );
assertEquals("old value in pa_postbus__of_antwoordnummer not retained", "666", subject.getElementsByTagName("pa_postbus__of_antwoordnummer").item(0).getTextContent() );
}
}

0 comments on commit a0598bc

Please sign in to comment.