Coverage details for umich.cac.xml.PbsToGrid

LineHitsSource
1 package umich.cac.xml;
2  
3 import java.io.File;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8  
9 import javax.xml.transform.TransformerException;
10  
11 import org.apache.xpath.XPathAPI;
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.NodeList;
15 import org.w3c.dom.Text;
16  
17 import umich.cac.util.XmlUtil;
18  
19  
20  
21 /**
22  * Converts PBS XML file to UsageRecord GGF WG format. Before the
23  * conversion is done this program will insert two elements:
24  * /pbs_jobfile/ppn -- the number of processors on each node.
25  * /pbs_jobfile/machine_name -- the name of the machine. This should
26  * be the same as the URWG MachineName element.
27  *
28  * <p>
29  * Additionally, the timestamps will be converted from epoch time to
30  * xsd:dateTime format. This is done for the elements: ctime, etime,
31  * qtime, start, end. Each of these elements will be restructured as:
32  * &lt;time_element&gt;/epoch -- The value in epoch time
33  * &lt;time_element&gt;/datetime -- the value in xsd:dateTime format.
34  *
35  * <p>
36  * Further, the &lt;nodegroup&gt; elements will have an extra attribute
37  * added called 'processors' which is the product of the numnodes
38  * attribute and ppn (the attribute or the default value if the
39  * attribute is not present).
40  *
41  * <p>
42  * Note this borrowed heavily from PbsToJoblog.java.
43  *
44  * The usage is
45  * <pre>
46  * java umich.cac.xml.PbsToGrid &lt;pbs xml file&gt; &lt;output file&gt;
47  * &lt;machine name&gt; &lt;ppn&gt;
48  * </pre>
49  *
50  * @author devink@sdsc.edu
51  * @version $Header: /cvsroot/pbsaccounting/pbsaccounting/src/umich/cac/xml/PbsToGrid.java,v 1.7 2003/10/21 19:07:46 rodmach Exp $
52  * @created July 15, 2003
53  */
54  
550public class PbsToGrid {
56  
57         /** List of XPATH queries to Get all time elements in the document */
580        private static String xpathList[] = { "//ctime", "//etime", "//qtime", "//start", "//end" };
59  
60         /** XPath query to get the nodegroup elements */
610        private static String xpathNodegroup = "//nodegroup";
62  
63         /** Logger */
640        private static Logger logger =
650          Logger.getLogger(PbsToGrid.class.getName());
66  
67         private int defaultPPN;
68         private String machineName;
69         private Document inputDoc;
70         private Document outputDoc;
71  
72         /**
73          * Prints the usage to STDOUT and exits
74          */
75         public static void usage() {
760                String usage = "Usage: PbsToJoblog "
77                              + "<PBS Xml file> <outputfilename> "
78                              + " <machine name> <ppn> ";
790                        System.out.println(usage);
800                        System.exit(-1);
810        }
82  
83         public void setPPN(int ppn) {
840            defaultPPN = ppn;
850        }
86         public void setMachineName(String name) {
870            machineName = name;
880        }
89         public boolean setInputFile(String xmlFile) {
90             //Do some basic error checking
910            File testFile = new File(xmlFile);
92  
930            if (!testFile.exists()) {
940                logger.log(Level.SEVERE, "File does not exist");
950                return false;
960            } else if (!testFile.canRead()) {
970                logger.log(Level.SEVERE, "Unable to read file, check permissions");
980                return false;
990            } else if (!testFile.isFile()) {
1000                logger.log(Level.SEVERE, "Is not a file, please use a file");
1010                return false;
102             }
103  
1040            inputDoc = XmlUtil.parseXmlFile(xmlFile, false);
1050            if (inputDoc == null) {
1060                logger.log(Level.SEVERE, "Unable to parse file" + xmlFile);
1070                return false;
108             }
1090            return true;
110         }
111         public void setInputDOM(Document doc) {
1120            inputDoc = doc;
1130        }
114         public Document getOutput() {
1150            return outputDoc;
116         }
117  
118         /**
119          * Main. Usage is
120          *
121          * <pre>
122          * java umich.cac.xml.PbsToGrid <filename.xml> <outputfile>
123          * </pre>
124          *
125          * @param args
126          * argument one is XML file, argument two is output file
127          */
128         public static void main(String args[]) {
129  
1300                if (args.length != 4) {
1310                        usage();
132                 }
133  
1340                PbsToGrid converter = new PbsToGrid();
135  
1360                if (! converter.setInputFile(args[0]) ) {
1370                    System.exit(-1);
138                 }
139  
1400                String outputFile = args[1];
141  
1420                converter.setMachineName(args[2]);
1430                converter.setPPN(Integer.parseInt(args[3]));
144  
145                 try {
1460                    converter.convertToGrid();
147                 }
1480                catch (TransformerException e) {
1490                    logger.log(Level.SEVERE, "Unable to Modify XML File "
150                             + args[0], e);
1510                    System.exit(1);
1520                }
153  
1540                XmlUtil.writeXmlFile(converter.getOutput(), outputFile);
1550        }
156  
157         public void convertToGrid() throws TransformerException
158         {
159             long time;
160             Element newElement;
161             Text newText;
162             NodeList nodelist;
163  
164             // Do all the time conversions.
1650            for (int j = 0; j < xpathList.length; j++) {
166                 // Get the matching elements
1670                nodelist = XPathAPI.selectNodeList(inputDoc, xpathList[j]);
168  
169                 // Process the elements in the nodelist
1700                for (int i = 0; i < nodelist.getLength(); i++) {
1710                        Element timeElement = (Element) nodelist.item(i);
172  
173                         //Get the value (epoch time) inside the element
1740                        Text ttime = (Text) timeElement.getFirstChild();
175  
176                         // Transform the time into xs:dateTime format
177                         
178                         // Convert time from seconds past epoch,
179                         // to milisecords past epoch.
1800                        time = Long.valueOf(ttime.getNodeValue()).longValue();
1810                        time *= 1000;
182  
183                         // create a new date formatter with the
184                         // necessary format pattern. Note we are
185                         // always in UTC.
1860                        SimpleDateFormat df =
187                             new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
1880                        String ts = df.format(new Date(time));
189  
190                         // add the newly formatted value as <element>_datetime
191                         
192                         /* Method 2 */
193                         // Move old data to new sub element --
194                         // is this really needed?
1950                        newElement = inputDoc.createElement("epoch");
1960                        timeElement.removeChild(ttime);
1970                        newElement.appendChild(ttime);
1980                        timeElement.appendChild(newElement);
199  
200                         // Add the newly formatted time
2010                        newElement = inputDoc.createElement("datetime");
2020                        newText = inputDoc.createTextNode(ts);
2030                        newElement.appendChild(newText);
2040                        timeElement.appendChild(newElement);
205                 }
206             }
207  
208             // calculate the processors value for node groups
2090            nodelist = XPathAPI.selectNodeList(inputDoc, xpathNodegroup);
2100            for (int i = 0; i < nodelist.getLength(); i++) {
2110                Element ngElement = (Element) nodelist.item(i);
2120                int procs = 0;
213  
2140                if (ngElement.hasAttribute("numnodes") ) {
215                     int ng_count; // number of nodes in this nodegroup
216                     int ng_ppn; // ppn for this nodegroup
217  
2180                    ng_count = Integer.parseInt(
219                             ngElement.getAttribute("numnodes"));
220  
2210                    if (ngElement.hasAttribute("ppn") ) {
2220                        ng_ppn = Integer.parseInt(
223                                 ngElement.getAttribute("ppn"));
224                     }
225                     else {
2260                        ng_ppn = defaultPPN;
227                     }
228  
2290                    procs = ng_count * ng_ppn;
230                 }
2310                ngElement.setAttribute("processors", Integer.toString(procs));
232             }
233  
234             // Add the config information in.
2350            Element topElement = inputDoc.getDocumentElement();
236  
237             // machine_name
2380            newElement = inputDoc.createElement("machine_name");
2390            newText = inputDoc.createTextNode(machineName);
2400            newElement.appendChild(newText);
2410            topElement.appendChild(newElement);
242  
243             // ppn
2440            newElement = inputDoc.createElement("ppn");
2450            newText = inputDoc.createTextNode(Integer.toString(defaultPPN));
2460            newElement.appendChild(newText);
2470            topElement.appendChild(newElement);
248  
249  
250  
251             //Write out the joblog file
2520            outputDoc = XmlUtil.createOutputDOM(inputDoc, "ur.xsl");
2530        }
254 }
255  
256  
257  

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.