Line | Hits | Source |
---|---|---|
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 <UID> tag to the XML document by reading | |
22 | * the <USER> 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 <pbs xml file> <output file> | |
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 | ||
46 | 0 | public class PbsToJoblog { |
47 | ||
48 | /** XPATH query to Get all user elements in the document */ | |
49 | ||
50 | 0 | private static String xpath = "//user"; |
51 | ||
52 | /** Logger */ | |
53 | ||
54 | 0 | private static Logger logger = |
55 | 0 | 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 | ||
65 | 0 | String usage = |
66 | "Usage: PbsToJoblog " + "<PBS Xml file> <outputfilename>"; | |
67 | 0 | System.out.println(usage); |
68 | 0 | System.exit(-1); |
69 | ||
70 | 0 | } |
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 | ||
88 | 0 | if (args.length != 2) { |
89 | 0 | usage(); |
90 | } | |
91 | ||
92 | 0 | String xmlFile = args[0]; |
93 | 0 | String outputFile = args[1]; |
94 | ||
95 | //Do some basic error checking | |
96 | ||
97 | 0 | File testFile = new File(xmlFile); |
98 | ||
99 | 0 | if (!testFile.exists()) { |
100 | 0 | logger.log(Level.SEVERE, "File does not exist"); |
101 | 0 | System.exit(-1); |
102 | 0 | } else if (!testFile.canRead()) { |
103 | 0 | logger.log(Level.SEVERE, "Unable to read file, check permissions"); |
104 | 0 | System.exit(-1); |
105 | 0 | } else if (!testFile.isFile()) { |
106 | 0 | logger.log(Level.SEVERE, "Is not a file, please use a file"); |
107 | 0 | System.exit(-1); |
108 | } | |
109 | ||
110 | //Object that looks up UID's from the passwd file | |
111 | 0 | Passwd passwd = null; |
112 | try { | |
113 | 0 | passwd = new Passwd(); |
114 | 0 | } catch (Exception e) { |
115 | 0 | logger.log( |
116 | Level.SEVERE, | |
117 | "Unable to read passwd file to lookup UID's", | |
118 | e); | |
119 | ||
120 | 0 | } |
121 | ||
122 | 0 | Document doc = XmlUtil.parseXmlFile(xmlFile, false); |
123 | ||
124 | 0 | if (doc == null) { |
125 | 0 | logger.log(Level.SEVERE, "Unable to parse file" + xmlFile); |
126 | 0 | System.exit(-1); |
127 | ||
128 | } | |
129 | ||
130 | try { | |
131 | ||
132 | // Get the matching elements | |
133 | ||
134 | 0 | NodeList nodelist = XPathAPI.selectNodeList(doc, xpath); |
135 | ||
136 | // Process the elements in the nodelist | |
137 | ||
138 | 0 | for (int i = 0; i < nodelist.getLength(); i++) { |
139 | ||
140 | // Get <USER> element | |
141 | 0 | Element userElement = (Element) nodelist.item(i); |
142 | ||
143 | //Get the value (username) inside <USER> | |
144 | // element | |
145 | 0 | Text userName = (Text) userElement.getFirstChild(); |
146 | ||
147 | //Get the UID for this user | |
148 | 0 | long uid = -1; |
149 | ||
150 | 0 | if (passwd != null) { |
151 | 0 | uid = passwd.lookupUid(userName.getData()); |
152 | } | |
153 | ||
154 | //Create the <UID> tag | |
155 | 0 | Element element = doc.createElement("uid"); |
156 | ||
157 | //Add the uid value | |
158 | 0 | Text text = doc.createTextNode(String.valueOf(uid)); |
159 | ||
160 | 0 | element.appendChild(text); |
161 | //Add it to the parent before the <USER> | |
162 | // element | |
163 | ||
164 | 0 | Node parent = userElement.getParentNode(); |
165 | 0 | parent.insertBefore(element, userElement); |
166 | ||
167 | } | |
168 | ||
169 | 0 | } catch (TransformerException e) { |
170 | 0 | logger.log(Level.SEVERE, "Unable to modify XML file " + xmlFile, e); |
171 | 0 | System.exit(-1); |
172 | ||
173 | 0 | } |
174 | ||
175 | //XmlUtil.writeXmlFile(doc,"modified.xml"); | |
176 | //Write out the joblog file | |
177 | 0 | XmlUtil.createStreamOutputFile(doc, "npaci_joblog.xsl", outputFile); |
178 | ||
179 | 0 | } |
180 | ||
181 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |