Skip to content

Best Practices #5

@omarrana

Description

@omarrana

Code needed to be split for better readability.

public class Integration {
public ArrayList<Node> integrationNodes;
public ArrayList<Node> seedNodes;
private static XPathFactory xpf;
private static XPath xpath;
/**
* This method integrates the two AML files.
*
* @throws Throwable
*/
public void integrate() throws Throwable {
// arrayList to hold nodes for integration and orignal files.
integrationNodes = new ArrayList<>();
seedNodes = new ArrayList<>();
// reads one of AML file contents
String contents = FileUtils.readFileToString(new File(ConfigManager.getFilePath() + "/seed-Granularity-1.aml"),
"UTF-8");
// One of the AML file will have its contents copied as it is.
PrintWriter prologWriter = new PrintWriter(new File(ConfigManager.getFilePath() + "/integration.aml"));
prologWriter.println(contents);
prologWriter.close();
// Get the output.txt contents which show matching elements.
// using XPath for XML
xpf = XPathFactory.newInstance();
xpath = xpf.newXPath();
// initializing documents.
Document seed = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(ConfigManager.getFilePath() + "/seed-Granularity-0.aml"));
Document integration = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(ConfigManager.getFilePath() + "/integration.aml"));
// Queries both documents to get All attribute values.
NodeList nodes = (NodeList) xpath.evaluate("//*/@*", seed, XPathConstants.NODESET);
NodeList nodes2 = (NodeList) xpath.evaluate("//*/@*", integration, XPathConstants.NODESET);
// stores all the attributes in arrayList
setNodes(nodes, 1);
setNodes(nodes2, 2);
// looping through the seedNode which will be compared to matching
// elements in output.txt
for (int i = 0; i < seedNodes.size(); i++) {
// flag to check if the attribute value is inside matching elements.
int flag = 0;
// loops through all its element.
for (int k = 0; k < DeductiveDB.attrName.size(); k++) {
if (seedNodes.get(i).getTextContent().equals(DeductiveDB.attrName.get(k))) {
// if match is found
flag = 1;
}
if (seedNodes.get(i).getNodeName().equals("FileName")) {
// not in output.txt
flag = 1;
}
}
// we are interested if its not in output.txt
// we need to add non matching elements to the integration file.
// all matching elements were already included when we copied one of
// the seed files.
if (flag == 0) {
// check tells if the non matching elements are already inside
// xml or not
int check = 0;
// for every node in Integration File compare the seed nodes.
for (int j = 0; j < integrationNodes.size(); j++) {
// compares the attribute Node with its values
if (seedNodes.get(i).getTextContent().equals(integrationNodes.get(j).getTextContent())) {
// compares the attribute Node with Node name
if (seedNodes.get(i).getNodeName().equals(integrationNodes.get(j).getNodeName())) {
// compares its Node and Parent node for semantic
// equality.
if (checkAttributeNode(seedNodes.get(i).getTextContent(), seed,
integrationNodes.get(j).getTextContent(), integration)) {
// if all test passes we can say its already in
// the integration document. we can ignore it.
check = 1;
}
}
}
}
// if the node is not in integration file we should add it.
if (check != 1) {
// we get the Node of the attribute Value which is not
// found.
NodeList list = getAttributeNode(seedNodes.get(i).getTextContent(), seed);
// we find its parent node so we can append it under it.
for (int m = 0; m < list.getLength(); m++) {
// matches the parent in the integration document.
NodeList integ = (NodeList) xpath.evaluate("//" + list.item(m).getParentNode().getNodeName(),
integration, XPathConstants.NODESET);
// now we have the parent name and the nodes to be
// added.we export it to integration.aml file.
for (int z = 0; z < integ.getLength(); z++) {
// to transfer node from one document to another it
// must adopt that node.
integ.item(z).getOwnerDocument().adoptNode(list.item(m));
// now we can add under the parent.
integ.item(z).appendChild(list.item(m));
}
}
}
}
}
// finally we update our integration.aml file.
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(integration),
new StreamResult(new File(ConfigManager.getFilePath() + "/integration.aml")));
// checkNodeByValue(seed, integration);
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions