Search This Blog

Wednesday, August 16, 2017

Java Code to Parse SOA Adapter's Plan.xml file

Java Code to Parse SOA Adapter's Plan.xml file
import java.io.File;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParsePlan_XML {

public static void main(String[] args) {
ArrayList<String> jndi_names = new ArrayList<String>();
HashMap<String, String> hm1 = new HashMap<String, String>();

HashMap<String, String> hm = new HashMap<String, String>();



File inputFile = new File(args[0]);

try {

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
// System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("variable");

for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);

if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;

hm.put(eElement.getElementsByTagName("name")
.item(0)
.getTextContent(), eElement.getElementsByTagName("value")
.item(0)
.getTextContent());

}
}
for (Map.Entry m : hm.entrySet()) {
// System.out.println(m.getKey() + " " + m.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}

try {

final String regex_name = "(.*jndi-name=\"eis\\/yum\\/sftpAdapter\"](.*)name=)(.*)]\\/name";

final String regex = ".*jndi-name=(.*)]\\/jndi-name";
//final String string = "/weblogic-connector/outbound-resource-adapter/connection-definition-group/[connection-factory-interface=\"javax.resource.cci.ConnectionFactory\"]/connection-instance/[jndi-name=\"eis/yum/sftpAdapter\"]/jndi-name";

// File inputFile = new File("E:\\tars-download\\3-14734264081\\Plan.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
// System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("variable-assignment");

for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
// System.out.println("\nCurrent Element :"
// + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;

hm1.put(eElement.getElementsByTagName("name")
.item(0)
.getTextContent(), eElement.getElementsByTagName("xpath")
.item(0)
.getTextContent());

final String string = eElement.getElementsByTagName("xpath")
.item(0)
.getTextContent();

for (Map.Entry m : hm1.entrySet()) {
// System.out.println(m.getKey()+" "+m.getValue());

}
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {

for (int i = 1; i <= matcher.groupCount(); i++) {
jndi_names.add(matcher.group(i));
// System.out.println("Group " + i + ": " + matcher.group(i));

}
}

}

}

for (int i = 0; i < jndi_names.size(); i++) {

System.out.println();

for (Map.Entry m : hm1.entrySet()) {

String s1 = jndi_names.get(i).replaceAll("/", "\\\\/");
// System.out.println(s1);
String regex_value1 = "(.*jndi-name=" + s1 + "](.*)name=)(.*)]\\/value";
// regex_value= regex_value.replaceAll("/", "\\/");
// System.out.println(regex_value1);
String string = m.getValue().toString();
// System.out.println(string);
final Pattern pattern = Pattern.compile(regex_value1);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
System.out.println(jndi_names.get(i) + " " + matcher.group(3) + " " + hm.get(m.getKey()));
// System.out.println(hm.get(m.getKey()));

}

}
// System.out.println(jndi_names.size());
// System.out.println(jndi_names.get(i));
// System.out.println(string1);

}
} catch (Exception e) {
e.printStackTrace();
}
}

}


Tags: Publish
August 16, 2017 at 04:57PM
Open in Evernote

No comments: