import java.io.File; import java.io.FileReader; import java.util.HashMap; import org.xml.sax.XMLReader; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.DefaultHandler; /** * mindswap2pt converts mindswap owl files into a PASS provenance trace file */ public class mindswap2pt extends DefaultHandler { // What section/element are we in? enum SECTION { NONE, SERVICE_EXECUTION, PARAMETERS, HEADER, IMAGE, GRAPHIC }; enum ELEM { NONE, TIME_RUN, ARGV, LABEL }; SECTION section = SECTION.NONE; ELEM elem = ELEM.NONE; // pnode of current process static pnode curproc; static String curelem; int stamp = 0; static XMLReader xr; // A map from strings used in provtraces to pnodes (numbers & types) HashMap pnodes = new HashMap(); /** * 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 { // Initialize XML reader mindswap2pt handler = new mindswap2pt(); xr = XMLReaderFactory.createXMLReader(); xr.setContentHandler(handler); xr.setErrorHandler(handler); System.out.println( "format provtrace v2 seq" ); // Parse each file provided on the command line. for (int i = 0; i < args.length; i++) { File arg = new File(args[i]); if ( arg.isFile() ) { processFile( arg ); } else if ( arg.isDirectory() ) { File fileList[] = arg.listFiles(new OwlFilenameFilter()); for ( File file : fileList ) { processFile( file ); } } } } /** * Process the specified OWL file */ public static void processFile( File file ) throws Exception { FileReader freader; System.out.println( "#" ); System.out.println( "# PROCESSING: " + file.getName() ); System.out.println( "#" ); curproc = new pnode('p'); System.out.println( "create " + curproc ); freader = new FileReader(file); xr.parse(new InputSource(freader)); } public mindswap2pt () { super(); } //////////////////////////////////////////////////////////////////// // Event handlers. //////////////////////////////////////////////////////////////////// public void startDocument () { } public void endDocument () { } public void startElement (String uri, String name, String qName, Attributes atts) { String resource; pnode pn; // Handle section headers if ( "ServiceExecution".equals(name) ) { section = SECTION.SERVICE_EXECUTION; return; } else if ( "Parameters".equals(name) ) { section = SECTION.PARAMETERS; curelem = atts.getValue("rdf:about"); return; } else if ( "Header".equals(name) ) { section = SECTION.HEADER; curelem = atts.getValue("rdf:about"); return; } else if ( "Image".equals(name) ) { section = SECTION.IMAGE; curelem = atts.getValue("rdf:about"); return; } else if ( "Graphic".equals(name) ) { section = SECTION.GRAPHIC; curelem = atts.getValue("rdf:about"); return; } if ( SECTION.SERVICE_EXECUTION != section ) { if ("label".equals(name)) { elem = ELEM.LABEL; } return; } if ( "timeRun".equals(name) ) { // // EXECTIME // elem = ELEM.TIME_RUN; } else if ( "hasInputImage".equals(name) || "hasInputHeader".equals(name) || "hasInputParameters".equals(name) || "hasInputSlice".equals(name)) { // // INPUT // resource = atts.getValue("rdf:resource"); // Lookup the file in the pnodes map pn = pnodes.get(resource); if ( null == pn ) { // If file pnode does not exist yet, output records to make it exist pn = new pnode('f'); pnodes.put( resource, pn ); String path[] = resource.split("/"); String filename = path[path.length - 1]; System.out.println( "exists " + pn ); System.out.println( "add " + pn + " PATH : \"" + resource + "\"" ); 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 " + curproc + " INPUT -> " + pn + " st" + filestamp ); } else if ( "serviceUsed".equals(name) ) { // // NAME & PATH (of process) // resource = atts.getValue("rdf:resource"); String tmp[] = resource.split("#"); String procname = tmp[tmp.length - 1]; System.out.println( "add " + curproc + " PATH : \"" + resource + "\"" ); System.out.println( "add " + curproc + " NAME : \"" + procname + "\"" ); } else if ( "hasTextInputParameters".equals(name) ) { // // ARGV // elem = ELEM.ARGV; } else if (name.startsWith("hasOutput")) { resource = atts.getValue("rdf:resource"); // Lookup the file in the pnodes map pn = pnodes.get(resource); if ( null == pn ) { // If file pnode does not exist yet, output records to make it exist pn = new pnode('f'); pnodes.put( resource, pn ); String path[] = resource.split("/"); String filename = path[path.length - 1]; System.out.println( "exists " + pn ); System.out.println( "add " + pn + " PATH : \"" + resource + "\"" ); System.out.println( "add " + pn + " NAME : \"" + filename + "\"" ); } // Stamp the file int filestamp = stamp; System.out.println( "stamp " + curproc + " st" + stamp++ ); // Mark the process as an input to the file System.out.println( "add " + pn + " INPUT -> " + curproc + " st" + filestamp ); } } public void endElement (String uri, String name, String qName) { // Handle section headers if ( "ServiceExecution".equals(name) || "Parameters".equals(name) || "Header".equals(name) || "Image".equals(name) || "Graphic".equals(name)) { section = SECTION.NONE; curelem = null; return; } // We only parse SERVICE_EXECUTION if ( "timeRun".equals(name) || "hasTextInputParameters".equals(name) || "label".equals(name) ) { elem = ELEM.NONE; } } public void characters (char ch[], int start, int length) { String charstr = ""; // Escape control characters for (int i = start; i < start + length; i++) { switch (ch[i]) { case '\\': charstr += "\\\\"; break; case '"': charstr += "\\\""; break; case '\n': charstr += "\\n"; break; case '\r': charstr += "\\r"; break; case '\t': charstr += "\\t"; break; default: charstr += ch[i]; break; } } // Is this an element whose contents we look at? switch ( elem ) { case TIME_RUN: System.out.println("add " + curproc + " EXECTIME : \"" + charstr + "\""); break; case ARGV: System.out.print("add " + curproc + " ARGV : " ); for ( String str : charstr.trim().split("\\s") ) { System.out.print( "\"" + str + "\" " ); } System.out.println(); break; case LABEL: pnode pn = pnodes.get(curelem); if (pn != null) { System.out.println("add " + pn + " label : \"" + charstr + "\""); } default: // Not interested in this element break; } } }