Line | Hits | Source |
---|---|---|
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 | * <time_element>/epoch -- The value in epoch time | |
33 | * <time_element>/datetime -- the value in xsd:dateTime format. | |
34 | * | |
35 | * <p> | |
36 | * Further, the <nodegroup> 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 <pbs xml file> <output file> | |
47 | * <machine name> <ppn> | |
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 | ||
55 | 0 | public class PbsToGrid { |
56 | ||
57 | /** List of XPATH queries to Get all time elements in the document */ | |
58 | 0 | private static String xpathList[] = { "//ctime", "//etime", "//qtime", "//start", "//end" }; |
59 | ||
60 | /** XPath query to get the nodegroup elements */ | |
61 | 0 | private static String xpathNodegroup = "//nodegroup"; |
62 | ||
63 | /** Logger */ | |
64 | 0 | private static Logger logger = |
65 | 0 | 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() { | |
76 | 0 | String usage = "Usage: PbsToJoblog " |
77 | + "<PBS Xml file> <outputfilename> " | |
78 | + " <machine name> <ppn> "; | |
79 | 0 | System.out.println(usage); |
80 | 0 | System.exit(-1); |
81 | 0 | } |
82 | ||
83 | public void setPPN(int ppn) { | |
84 | 0 | defaultPPN = ppn; |
85 | 0 | } |
86 | public void setMachineName(String name) { | |
87 | 0 | machineName = name; |
88 | 0 | } |
89 | public boolean setInputFile(String xmlFile) { | |
90 | //Do some basic error checking | |
91 | 0 | File testFile = new File(xmlFile); |
92 | ||
93 | 0 | if (!testFile.exists()) { |
94 | 0 | logger.log(Level.SEVERE, "File does not exist"); |
95 | 0 | return false; |
96 | 0 | } else if (!testFile.canRead()) { |
97 | 0 | logger.log(Level.SEVERE, "Unable to read file, check permissions"); |
98 | 0 | return false; |
99 | 0 | } else if (!testFile.isFile()) { |
100 | 0 | logger.log(Level.SEVERE, "Is not a file, please use a file"); |
101 | 0 | return false; |
102 | } | |
103 | ||
104 | 0 | inputDoc = XmlUtil.parseXmlFile(xmlFile, false); |
105 | 0 | if (inputDoc == null) { |
106 | 0 | logger.log(Level.SEVERE, "Unable to parse file" + xmlFile); |
107 | 0 | return false; |
108 | } | |
109 | 0 | return true; |
110 | } | |
111 | public void setInputDOM(Document doc) { | |
112 | 0 | inputDoc = doc; |
113 | 0 | } |
114 | public Document getOutput() { | |
115 | 0 | 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 | ||
130 | 0 | if (args.length != 4) { |
131 | 0 | usage(); |
132 | } | |
133 | ||
134 | 0 | PbsToGrid converter = new PbsToGrid(); |
135 | ||
136 | 0 | if (! converter.setInputFile(args[0]) ) { |
137 | 0 | System.exit(-1); |
138 | } | |
139 | ||
140 | 0 | String outputFile = args[1]; |
141 | ||
142 | 0 | converter.setMachineName(args[2]); |
143 | 0 | converter.setPPN(Integer.parseInt(args[3])); |
144 | ||
145 | try { | |
146 | 0 | converter.convertToGrid(); |
147 | } | |
148 | 0 | catch (TransformerException e) { |
149 | 0 | logger.log(Level.SEVERE, "Unable to Modify XML File " |
150 | + args[0], e); | |
151 | 0 | System.exit(1); |
152 | 0 | } |
153 | ||
154 | 0 | XmlUtil.writeXmlFile(converter.getOutput(), outputFile); |
155 | 0 | } |
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. | |
165 | 0 | for (int j = 0; j < xpathList.length; j++) { |
166 | // Get the matching elements | |
167 | 0 | nodelist = XPathAPI.selectNodeList(inputDoc, xpathList[j]); |
168 | ||
169 | // Process the elements in the nodelist | |
170 | 0 | for (int i = 0; i < nodelist.getLength(); i++) { |
171 | 0 | Element timeElement = (Element) nodelist.item(i); |
172 | ||
173 | //Get the value (epoch time) inside the element | |
174 | 0 | 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. | |
180 | 0 | time = Long.valueOf(ttime.getNodeValue()).longValue(); |
181 | 0 | time *= 1000; |
182 | ||
183 | // create a new date formatter with the | |
184 | // necessary format pattern. Note we are | |
185 | // always in UTC. | |
186 | 0 | SimpleDateFormat df = |
187 | new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | |
188 | 0 | 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? | |
195 | 0 | newElement = inputDoc.createElement("epoch"); |
196 | 0 | timeElement.removeChild(ttime); |
197 | 0 | newElement.appendChild(ttime); |
198 | 0 | timeElement.appendChild(newElement); |
199 | ||
200 | // Add the newly formatted time | |
201 | 0 | newElement = inputDoc.createElement("datetime"); |
202 | 0 | newText = inputDoc.createTextNode(ts); |
203 | 0 | newElement.appendChild(newText); |
204 | 0 | timeElement.appendChild(newElement); |
205 | } | |
206 | } | |
207 | ||
208 | // calculate the processors value for node groups | |
209 | 0 | nodelist = XPathAPI.selectNodeList(inputDoc, xpathNodegroup); |
210 | 0 | for (int i = 0; i < nodelist.getLength(); i++) { |
211 | 0 | Element ngElement = (Element) nodelist.item(i); |
212 | 0 | int procs = 0; |
213 | ||
214 | 0 | if (ngElement.hasAttribute("numnodes") ) { |
215 | int ng_count; // number of nodes in this nodegroup | |
216 | int ng_ppn; // ppn for this nodegroup | |
217 | ||
218 | 0 | ng_count = Integer.parseInt( |
219 | ngElement.getAttribute("numnodes")); | |
220 | ||
221 | 0 | if (ngElement.hasAttribute("ppn") ) { |
222 | 0 | ng_ppn = Integer.parseInt( |
223 | ngElement.getAttribute("ppn")); | |
224 | } | |
225 | else { | |
226 | 0 | ng_ppn = defaultPPN; |
227 | } | |
228 | ||
229 | 0 | procs = ng_count * ng_ppn; |
230 | } | |
231 | 0 | ngElement.setAttribute("processors", Integer.toString(procs)); |
232 | } | |
233 | ||
234 | // Add the config information in. | |
235 | 0 | Element topElement = inputDoc.getDocumentElement(); |
236 | ||
237 | // machine_name | |
238 | 0 | newElement = inputDoc.createElement("machine_name"); |
239 | 0 | newText = inputDoc.createTextNode(machineName); |
240 | 0 | newElement.appendChild(newText); |
241 | 0 | topElement.appendChild(newElement); |
242 | ||
243 | // ppn | |
244 | 0 | newElement = inputDoc.createElement("ppn"); |
245 | 0 | newText = inputDoc.createTextNode(Integer.toString(defaultPPN)); |
246 | 0 | newElement.appendChild(newText); |
247 | 0 | topElement.appendChild(newElement); |
248 | ||
249 | ||
250 | ||
251 | //Write out the joblog file | |
252 | 0 | outputDoc = XmlUtil.createOutputDOM(inputDoc, "ur.xsl"); |
253 | 0 | } |
254 | } | |
255 | ||
256 | ||
257 |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |