Coverage details for umich.cac.xml.PbsToJoblog

LineHitsSource
1 package umich.cac.xml;
2  
3 import java.io.File;
4 import java.util.logging.Level;
5 import java.util.logging.Logger;
6  
7 import javax.xml.transform.TransformerException;
8  
9 import org.apache.xpath.XPathAPI;
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
12 import org.w3c.dom.Node;
13 import org.w3c.dom.NodeList;
14 import org.w3c.dom.Text;
15  
16 import umich.cac.util.Passwd;
17 import umich.cac.util.XmlUtil;
18  
19 /**
20  * Converts PBS XML file to NPACI Joblog format. It first reads in the XML file
21  * as Document object, inserts a &ltUID&gt tag to the XML document by reading
22  * the &ltUSER&gt tag and looks up the
23  *
24  * corresponding UID from the password file. Then it uses the npaci_joblog.xsl
25  *
26  * transform on this XML document to produce the final output.
27  *
28  * <pre>
29  * [pbs.xml] ---> [DOM (ADD UID TO XML)] --->[XSL TRANSFORM with npaci_joglog.xsl ] --> joblog.out
30  * </pre>
31  *
32  *
33  *
34  *
35  * The usage is
36  *
37  * <pre>
38  * java umich.cac.xml.PbsToJoblog &ltpbs xml file&gt &ltoutput file&gt
39  * </pre>
40  *
41  * @author rmach@umich.edu
42  * @version $Header: /cvsroot/pbsaccounting/pbsaccounting/src/umich/cac/xml/PbsToJoblog.java,v 1.4 2003/10/21 19:07:45 rodmach Exp $
43  *
44  */
45  
460public class PbsToJoblog {
47  
48     /** XPATH query to Get all user elements in the document */
49  
500    private static String xpath = "//user";
51  
52     /** Logger */
53  
540    private static Logger logger =
550        Logger.getLogger(PbsToJoblog.class.getName());
56  
57     /**
58      *
59      * Prints the usage to STDOUT and exits
60      *
61      */
62  
63     public static void usage() {
64  
650        String usage =
66             "Usage: PbsToJoblog " + "<PBS Xml file> <outputfilename>";
670        System.out.println(usage);
680        System.exit(-1);
69  
700    }
71  
72     /**
73      *
74      * Main. Usage is
75      *
76      * <pre>
77      * java umich.cac.xml.PbsToJoblog <filename.xml> <outputfile>
78      *
79      * </pre>
80      *
81      * @param args
82      * argument one is XML file, argument two is output file
83      *
84      */
85  
86     public static void main(String args[]) {
87  
880        if (args.length != 2) {
890            usage();
90         }
91  
920        String xmlFile = args[0];
930        String outputFile = args[1];
94  
95         //Do some basic error checking
96  
970        File testFile = new File(xmlFile);
98  
990        if (!testFile.exists()) {
1000            logger.log(Level.SEVERE, "File does not exist");
1010            System.exit(-1);
1020        } else if (!testFile.canRead()) {
1030            logger.log(Level.SEVERE, "Unable to read file, check permissions");
1040            System.exit(-1);
1050        } else if (!testFile.isFile()) {
1060            logger.log(Level.SEVERE, "Is not a file, please use a file");
1070            System.exit(-1);
108         }
109  
110         //Object that looks up UID's from the passwd file
1110        Passwd passwd = null;
112         try {
1130            passwd = new Passwd();
1140        } catch (Exception e) {
1150            logger.log(
116                 Level.SEVERE,
117                 "Unable to read passwd file to lookup UID's",
118                 e);
119  
1200        }
121  
1220        Document doc = XmlUtil.parseXmlFile(xmlFile, false);
123  
1240        if (doc == null) {
1250            logger.log(Level.SEVERE, "Unable to parse file" + xmlFile);
1260            System.exit(-1);
127  
128         }
129  
130         try {
131  
132             // Get the matching elements
133  
1340            NodeList nodelist = XPathAPI.selectNodeList(doc, xpath);
135  
136             // Process the elements in the nodelist
137  
1380            for (int i = 0; i < nodelist.getLength(); i++) {
139  
140                 // Get <USER> element
1410                Element userElement = (Element) nodelist.item(i);
142  
143                 //Get the value (username) inside <USER>
144                 // element
1450                Text userName = (Text) userElement.getFirstChild();
146  
147                 //Get the UID for this user
1480                long uid = -1;
149  
1500                if (passwd != null) {
1510                    uid = passwd.lookupUid(userName.getData());
152                 }
153  
154                 //Create the <UID> tag
1550                Element element = doc.createElement("uid");
156  
157                 //Add the uid value
1580                Text text = doc.createTextNode(String.valueOf(uid));
159  
1600                element.appendChild(text);
161                 //Add it to the parent before the <USER>
162                 // element
163  
1640                Node parent = userElement.getParentNode();
1650                parent.insertBefore(element, userElement);
166  
167             }
168  
1690        } catch (TransformerException e) {
1700            logger.log(Level.SEVERE, "Unable to modify XML file " + xmlFile, e);
1710            System.exit(-1);
172  
1730        }
174  
175         //XmlUtil.writeXmlFile(doc,"modified.xml");
176         //Write out the joblog file
1770        XmlUtil.createStreamOutputFile(doc, "npaci_joblog.xsl", outputFile);
178  
1790    }
180  
181 }

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.