import java.util.HashMap; import java.util.ArrayList; import java.io.StringReader; import java.io.StringWriter; import java.io.InputStreamReader; import java.io.FileInputStream; import java.io.File; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Attr; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Element; import org.xml.sax.InputSource; import org.apache.xerces.parsers.DOMParser; import org.xml.sax.InputSource; /** * pasoa2pt converts pasoa files into a PASS provenance trace file */ public class pasoa2pt { // pnode of current process //pnode curproc; int stamp = 0; DOMParser parser; Document doc; String Source; String Sink; ArrayList args; // A map from strings used in provtraces to pnodes (numbers & types) HashMap pnodes; HashMap> hasargv; HashMap hasexec; public pasoa2pt() throws Exception { pnodes = new HashMap(); hasargv = new HashMap>(); hasexec = new HashMap(); } private String getFirstChildText(Node n) { NodeList nl = n.getChildNodes(); Node child = nl.item(0); if (child.getNodeType() == Node.TEXT_NODE) { return new String(child.getNodeValue()); } return null; } public void processInteractionKey(Node n) { NodeList nl = n.getChildNodes(); Source = null; Sink = null; for (int i = 0; i < nl.getLength(); ++i) { String name = nl.item(i).getNodeName(); if (name.equalsIgnoreCase("messageSource")|| name.equalsIgnoreCase("messageSink")) { NodeList list = nl.item(i).getChildNodes(); for (int j = 0; j < list.getLength(); ++j) { Node addr = list.item(j); if (addr.getNodeName().equalsIgnoreCase("Address")) { String address = getFirstChildText(addr); if (name.equalsIgnoreCase("messageSource")) { Source = address; } else { Sink = address; } pnode proc; proc = pnodes.get(address); if (proc == null) { proc = new pnode('p'); pnodes.put(address, proc); System.out.println("create "+proc); String procname = address.substring(address.lastIndexOf(".") + 1, address.indexOf("@")); System.out.println("add "+ proc + " PATH: \"" + address + "\""); System.out.println("add "+ proc + " NAME: \"" + procname + "\""); } } } } } if (Source.equalsIgnoreCase(Sink)) { Source = Sink = null; } } private String getChildString(Node in) { if (in.getNodeName().equalsIgnoreCase("String")|| in.getNodeName().equalsIgnoreCase("long")) { return getFirstChildText(in); } else { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { String ret = getChildString(nl.item(m)); if (ret != null) { return ret; } } } return null; } public void processInvocationBean(Node in) { if ((Source == null) || (Sink == null)) { return; } if (in.getNodeName().equalsIgnoreCase("void")) { String property = ((Element)in).getAttribute("property"); if (property.equalsIgnoreCase("invocationContext")) { String date = getChildString(in); if (hasexec.get(Sink) == null) { System.out.println("add "+ pnodes.get(Sink) + " EXECTIME : \"" + Long.parseLong(date)/1000 + "\""); hasexec.put(Sink, date); } } else if (property.equalsIgnoreCase("args")) { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { Node n = nl.item(m); if (n.getNodeName().equalsIgnoreCase("array")) { String l = ((Element)n).getAttribute("length"); int length = Integer.parseInt(l); //System.out.println("length " + length); args = new ArrayList(); NodeList arglist = n.getChildNodes(); for (int i = 0; i < arglist.getLength(); ++i) { String arg = getChildString(arglist.item(i)); if (arg != null) { //System.out.println("arg "+arg); args.add(arg); } } if (hasargv.get(Sink)!= null) { return; } hasargv.put(Sink, args); /* set argv parameter */ System.out.print("add " + pnodes.get(Sink) + " ARGV : " ); for (int i = 0; i < args.size(); ++i) { String [] split = args.get(i).split(" "); for (int j = 0; j < split.length; ++j) { System.out.print("\"" + split[j]+ "\" "); } } System.out.println(); for (int i = 0; i < args.size(); ++i) { String path = args.get(i); if (path.indexOf('-') == 0) { /* xxx hack!! */ continue; } if (path.indexOf('y') == 0) { /* xxx hack!! */ continue; } pnode pn = pnodes.get(path); if ( null == pn ) { // If file pnode does not exist yet, output records to make it exist pn = new pnode('f'); pnodes.put( path, pn ); String names[] = path.split("/"); String filename = names[names.length - 1]; System.out.println( "exists " + pn ); System.out.println( "add " + pn + " PATH : \"" + path + "\"" ); System.out.println( "add " + pn + " NAME : \"" + filename + "\"" ); } // Stamp the file int filestamp = stamp; System.out.println( "stamp " + pn + " st" + stamp++ ); // Mark the file as an input to the process System.out.println( "add " + pnodes.get(Sink) + " INPUT -> " + pn + " st" + filestamp ); } } } } } else { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { processResultBean(nl.item(m)); } } } public void addChildToSink(Node in) { String result = getChildString(in); pnode pn; pn = pnodes.get(result); if (pn == null) { pn = new pnode('f'); pnodes.put(result, pn); String path[] = result.split("/"); String filename = path[path.length - 1]; System.out.println("exists " + pn); System.out.println( "add " + pn + " PATH : \"" + result + "\"" ); System.out.println( "add " + pn + " NAME : \"" + filename + "\"" ); } int procstamp = stamp; System.out.println( "stamp " + pnodes.get(Source) + " st" + stamp++ ); // Mark the process as an input to the file System.out.println( "add " + pn + " INPUT -> " + pnodes.get(Source) + " st" + procstamp ); } public void processResultBean(Node in) { if (Sink == null) { return; } if (in.getNodeName().equalsIgnoreCase("void")) { String property = ((Element)in).getAttribute("property"); if (property.equalsIgnoreCase("result")) { pnode pn; Node child = in.getChildNodes().item(0); if (child.getNodeName().equalsIgnoreCase("array")) { NodeList nl = child.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) { addChildToSink(nl.item(i)); } } else { addChildToSink(in); } } else { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { processResultBean(nl.item(m)); } } } } public void processInteractionPAssertion(Node in) { if (in.getNodeName().equalsIgnoreCase("object")) { String objclass = ((Element)in).getAttribute("class"); if (objclass.endsWith("InvocationBean")) { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { processInvocationBean(nl.item(m)); } } else if (objclass.endsWith("ResultBean")) { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { processResultBean(nl.item(m)); } } } else { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { processInteractionPAssertion(nl.item(m)); } } } public void processRelationShipPAssertion(Node in) { if ((in.getNodeName().equalsIgnoreCase("xPath"))&& (in.getParentNode().getNodeName().equalsIgnoreCase("dataAccessor"))) { String expr = getFirstChildText(in); String search = new String("java/object/void[@property='args']/array/void[@index='"); int index = expr.indexOf(search); if (index >= 0) { int argindex = Integer.parseInt(expr.substring(search.length(), search.length() + 1)); if ((args != null) && (args.size() > argindex)) { pnode pn; String path = args.get(argindex); if (path.indexOf('-') == 0) { /* xxx hack!! */ return; } pn = pnodes.get(path); if ( null == pn ) { // If file pnode does not exist yet, output records to make it exist pn = new pnode('f'); pnodes.put( path, pn ); String names[] = path.split("/"); String filename = names[names.length - 1]; System.out.println( "exists " + pn ); System.out.println( "add " + pn + " PATH : \"" + path + "\"" ); System.out.println( "add " + pn + " NAME : \"" + filename + "\"" ); } // Stamp the file int filestamp = stamp; System.out.println( "stamp " + pn + " st" + stamp++ ); // Mark the file as an input to the process System.out.println( "add " + Sink + " INPUT -> " + pn + " st" + filestamp ); } else { System.out.println("accessing args["+argindex+"] : out of bounds"); } } } else { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { processRelationShipPAssertion(nl.item(m)); } } } public void processActorStatePAssertion(Node in) { if (in.getNodeName().equalsIgnoreCase("ActorProfile")) { NodeList ActorProfileChild = in.getChildNodes(); for (int m = 0; m < ActorProfileChild.getLength(); ++m) { Node Name = ActorProfileChild.item(m); if (Name.getNodeName().equalsIgnoreCase("name")) { String nameValue = getFirstChildText(Name); //System.out.println("name "+nameValue); } } } else { NodeList nl = in.getChildNodes(); for (int m = 0; m < nl.getLength(); ++m) { processActorStatePAssertion(nl.item(m)); } } } public void processPAssertion(Node assertion) { NodeList assertions = assertion.getChildNodes(); for (int i = 0; i < assertions.getLength(); i++ ) { Node node = assertions.item(i); String name = node.getNodeName(); if (name.equalsIgnoreCase("interactionPAssertion")) { processInteractionPAssertion(node); } else if (name.equalsIgnoreCase("RelationShipPAssertion")) { //processRelationShipPAssertion(node); } else if (name.equalsIgnoreCase("actorStatePAssertion")) { //processActorStatePAssertion(node); } } } public void processNode(Node n) { if (n.getNodeType() != Node.ELEMENT_NODE) { return; } NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++ ) { Node node = nl.item(i); String name = node.getNodeName(); NodeList list = node.getChildNodes(); for (int j = 0; j < list.getLength(); ++j) { Node tempNode = list.item(j); name = tempNode.getNodeName(); if (name.equalsIgnoreCase("interactionKey")) { processInteractionKey(tempNode); } else if (name.equalsIgnoreCase("sender")|| name.equalsIgnoreCase("receiver")) { processPAssertion(tempNode); } } } } /** * Process the XML file */ public void process(String filename) throws Exception { System.out.println("# processing "+filename); parser = new DOMParser(); parser.parse(new InputSource(new InputStreamReader(new FileInputStream(filename)))); doc = parser.getDocument(); NodeList nl = doc.getChildNodes(); for (int i = 0; i < nl.getLength(); i++ ) { processNode(nl.item(i)); } } /** * Process all the files/directories specified on the command line. * For directories, process all owl files in the specified directory. */ public static void main (String args[]) throws Exception { pasoa2pt obj = new pasoa2pt(); for (int i = 0; i < args.length; i++) { File arg = new File(args[i]); System.out.println( "format provtrace v2 seq" ); if ( arg.isFile() ) { obj.process(args[i]); } else if ( arg.isDirectory() ) { File fileList[] = arg.listFiles(new XMLFilenameFilter()); for ( File file : fileList ) { obj.process(file.getAbsolutePath()); } } } } }