| Line | Hits | Source |
|---|---|---|
| 1 | package umich.cac.pbs; | |
| 2 | ||
| 3 | import java.io.BufferedReader; | |
| 4 | import java.io.BufferedWriter; | |
| 5 | import java.io.File; | |
| 6 | import java.io.FileReader; | |
| 7 | import java.io.FileWriter; | |
| 8 | import java.io.IOException; | |
| 9 | import java.util.logging.Level; | |
| 10 | import java.util.logging.Logger; | |
| 11 | ||
| 12 | /** | |
| 13 | * This class converts PBS accounting log files into an XML representation | |
| 14 | * <p> | |
| 15 | * | |
| 16 | * <code> | |
| 17 | * Usage: java umich.cac.pbs.PbsToXml <i>filename</i> <i>outputfilename</i> | |
| 18 | * </code> | |
| 19 | * | |
| 20 | * @author rmach@umich.edu | |
| 21 | * @version $Header: | |
| 22 | * /cvsroot/pbsaccounting/pbsaccounting/src/umich/cac/pbs/PbsToXml.java,v | |
| 23 | * 1.3 2003/04/30 15:49:29 rodmach Exp $ | |
| 24 | */ | |
| 25 | ||
| 26 | 9 | public class PbsToXml { |
| 27 | ||
| 28 | /** | |
| 29 | * Logger for application | |
| 30 | */ | |
| 31 | ||
| 32 | 6 | private static Logger logger = Logger.getLogger(PbsToXml.class.getName()); |
| 33 | ||
| 34 | /** | |
| 35 | * The new line seperator for this system | |
| 36 | */ | |
| 37 | ||
| 38 | 9 | private String nl = System.getProperty("line.separator"); |
| 39 | ||
| 40 | /** | |
| 41 | * The DTD to use | |
| 42 | */ | |
| 43 | ||
| 44 | 9 | private String dtd = null; |
| 45 | ||
| 46 | /** | |
| 47 | * The W3C schema to use | |
| 48 | */ | |
| 49 | ||
| 50 | 9 | private String w3Cschema = null; |
| 51 | ||
| 52 | /** | |
| 53 | * Prints the usage to STDOUT and exits | |
| 54 | */ | |
| 55 | ||
| 56 | public static void usage() { | |
| 57 | ||
| 58 | 0 | System.out.println( |
| 59 | "Usage: PbsToXml <file.dtd | file.xsd> accountfilename outputfilename.xml"); | |
| 60 | ||
| 61 | 0 | System.out.println( |
| 62 | "where <file.dtd | file.xsd> is DTD or Schema File you will use to validate, like linux_joblog.dtd or linux_joblog.xsd"); | |
| 63 | ||
| 64 | 0 | System.exit(-1); |
| 65 | ||
| 66 | 0 | } |
| 67 | ||
| 68 | /** | |
| 69 | * Set the DTD to use | |
| 70 | * | |
| 71 | * @param dtd | |
| 72 | * name of the dtd to use, like linux_joblog.dtd. This dtd is | |
| 73 | * added to XML file, if null then it won't be added to XML | |
| 74 | * file. The file doesn't have to exist at creation time | |
| 75 | */ | |
| 76 | ||
| 77 | public void setDTD(String dtd) { | |
| 78 | ||
| 79 | 0 | this.dtd = dtd; |
| 80 | ||
| 81 | 0 | } |
| 82 | ||
| 83 | /** | |
| 84 | * Set the W3C Schema to use | |
| 85 | * | |
| 86 | * @param w3Cschema | |
| 87 | * name of the W3C schema to use, like linux_joblog.xsd. This | |
| 88 | * schema is added to XML file, if null then it won't be added | |
| 89 | * to XML file. The file doesn't have to exist at creation time | |
| 90 | */ | |
| 91 | ||
| 92 | public void setW3CSchema(String w3Cschema) { | |
| 93 | ||
| 94 | 0 | this.w3Cschema = w3Cschema; |
| 95 | ||
| 96 | 0 | } |
| 97 | ||
| 98 | /** | |
| 99 | * Main class. Takes joblog as argument and prints out XML representation | |
| 100 | * on STDOUT | |
| 101 | * | |
| 102 | * @param args | |
| 103 | * The PBS accounting file to parse | |
| 104 | */ | |
| 105 | ||
| 106 | public static void main(String args[]) { | |
| 107 | ||
| 108 | 0 | if (args.length != 3 && args.length != 2) { |
| 109 | ||
| 110 | 0 | usage(); |
| 111 | ||
| 112 | } | |
| 113 | ||
| 114 | //Convert PBS file to XML and print it out | |
| 115 | ||
| 116 | 0 | PbsToXml parse = new PbsToXml(); |
| 117 | ||
| 118 | 0 | String filename = null; |
| 119 | 0 | String outputFile = null; |
| 120 | ||
| 121 | 0 | if (args.length == 2) { |
| 122 | 0 | filename = args[0]; |
| 123 | 0 | outputFile = args[1]; |
| 124 | } else { | |
| 125 | 0 | String validator = args[0]; |
| 126 | 0 | if (validator.toUpperCase().endsWith(".DTD")) { |
| 127 | 0 | parse.setDTD(validator); |
| 128 | 0 | } else if (validator.toUpperCase().endsWith(".XSD")) { |
| 129 | 0 | parse.setW3CSchema(validator); |
| 130 | } else { | |
| 131 | 0 | logger.log( |
| 132 | Level.SEVERE, | |
| 133 | "File must end with a .xml or .xsd extension"); | |
| 134 | 0 | System.exit(-1); |
| 135 | } | |
| 136 | ||
| 137 | 0 | filename = args[1]; |
| 138 | 0 | outputFile = args[2]; |
| 139 | } | |
| 140 | ||
| 141 | //Do some basic error checking | |
| 142 | ||
| 143 | 0 | File testFile = new File(filename); |
| 144 | ||
| 145 | 0 | if (!testFile.exists()) { |
| 146 | 0 | logger.log(Level.SEVERE, "File does not exist"); |
| 147 | 0 | System.exit(-1); |
| 148 | 0 | } else if (!testFile.canRead()) { |
| 149 | 0 | logger.log(Level.SEVERE, "Unable to read file, check permissions"); |
| 150 | 0 | System.exit(-1); |
| 151 | 0 | } else if (!testFile.isFile()) { |
| 152 | 0 | logger.log(Level.SEVERE, "Is not a file, please use a file"); |
| 153 | 0 | System.exit(-1); |
| 154 | } | |
| 155 | ||
| 156 | 0 | String xmlFile = parse.convertToXml(filename); |
| 157 | 0 | if (xmlFile == null) { |
| 158 | 0 | logger.log(Level.SEVERE, "Unable to parse filename" + filename); |
| 159 | 0 | System.exit(-1); |
| 160 | } else { | |
| 161 | ||
| 162 | try { | |
| 163 | ||
| 164 | 0 | BufferedWriter out = |
| 165 | new BufferedWriter(new FileWriter(outputFile)); | |
| 166 | 0 | out.write(xmlFile); |
| 167 | 0 | out.close(); |
| 168 | 0 | } catch (IOException e) { |
| 169 | 0 | logger.log( |
| 170 | Level.SEVERE, | |
| 171 | "Unable to write to file " + outputFile, | |
| 172 | e); | |
| 173 | 0 | System.exit(-1); |
| 174 | ||
| 175 | 0 | } |
| 176 | ||
| 177 | } | |
| 178 | ||
| 179 | 0 | } |
| 180 | ||
| 181 | /** | |
| 182 | * Process a record from the PBS accounting file, and pass to the | |
| 183 | * appropriate formatter to convert it to XML | |
| 184 | * | |
| 185 | * @param inputLine | |
| 186 | * inputLine from PBS accounting file to parse | |
| 187 | * @return XML representation of the record | |
| 188 | * @throws InvalidRecordException | |
| 189 | * If the record can not be parsed, or it doesn't have enough | |
| 190 | * fields | |
| 191 | */ | |
| 192 | ||
| 193 | public String processRecord(String inputLine) | |
| 194 | throws InvalidRecordException { | |
| 195 | ||
| 196 | // Parse a semicolon string. | |
| 197 | ||
| 198 | 9 | String[] fields = inputLine.split(";"); |
| 199 | ||
| 200 | 9 | if (fields.length < RecordPosition.FIELD_LENGTH_MIN |
| 201 | || fields.length > RecordPosition.FIELD_LENGTH_MAX) { | |
| 202 | ||
| 203 | 3 | logger.log(Level.SEVERE, "Incorrect field length!"); |
| 204 | ||
| 205 | 3 | throw new InvalidRecordException( |
| 206 | "Not enough fields in record, expected between" | |
| 207 | + RecordPosition.FIELD_LENGTH_MIN | |
| 208 | + " and " | |
| 209 | + RecordPosition.FIELD_LENGTH_MAX | |
| 210 | + "but have " | |
| 211 | + fields.length | |
| 212 | + ". File is invalid"); | |
| 213 | } | |
| 214 | ||
| 215 | 6 | StringBuffer xmlBuffer = new StringBuffer(); |
| 216 | ||
| 217 | //Determine the type of record we are reading | |
| 218 | ||
| 219 | 6 | RecordType recordType = |
| 220 | new RecordType(fields[RecordPosition.RECORD_TYPE]); | |
| 221 | ||
| 222 | //Get a parser for this type of record | |
| 223 | ||
| 224 | 6 | PbsRecordParser parser = |
| 225 | RecordFactory.getInstance().getRecordParser(recordType); | |
| 226 | ||
| 227 | //Process the record, appending XML output to the XML buffer | |
| 228 | ||
| 229 | 6 | if (parser != null) { |
| 230 | 3 | xmlBuffer.append(parser.processRecord(fields)); |
| 231 | } else { | |
| 232 | 3 | throw new InvalidRecordException("Uknown record type" + recordType); |
| 233 | } | |
| 234 | 3 | return xmlBuffer.toString(); |
| 235 | } | |
| 236 | ||
| 237 | /** | |
| 238 | * Convert PBS accounting file to its XML representation | |
| 239 | * | |
| 240 | * @param filename | |
| 241 | * Name of the accounting file to parse | |
| 242 | * @return XML representation of the file, or null if unable to open file | |
| 243 | */ | |
| 244 | ||
| 245 | public String convertToXml(String filename) { | |
| 246 | ||
| 247 | 0 | BufferedReader in = null; |
| 248 | ||
| 249 | try { | |
| 250 | ||
| 251 | 0 | in = new BufferedReader(new FileReader(filename)); |
| 252 | ||
| 253 | 0 | } catch (Exception e) { |
| 254 | ||
| 255 | 0 | logger.log(Level.SEVERE, "Unable to open file " + filename, e); |
| 256 | ||
| 257 | 0 | return null; |
| 258 | 0 | } |
| 259 | ||
| 260 | 0 | StringBuffer sb = new StringBuffer(); |
| 261 | String inputLine; | |
| 262 | String parsedLine; | |
| 263 | 0 | sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 264 | 0 | sb.append(nl); |
| 265 | 0 | if (dtd != null) { |
| 266 | 0 | sb.append("<!DOCTYPE pbs_jobfile SYSTEM \"" + dtd + "\">"); |
| 267 | 0 | sb.append(nl); |
| 268 | } | |
| 269 | ||
| 270 | 0 | if (w3Cschema != null) { |
| 271 | 0 | sb.append( |
| 272 | "<pbs_jobfile xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"" | |
| 273 | + w3Cschema | |
| 274 | + "\">"); | |
| 275 | ||
| 276 | } else { | |
| 277 | 0 | sb.append("<pbs_jobfile>"); |
| 278 | } | |
| 279 | ||
| 280 | 0 | sb.append(nl); |
| 281 | ||
| 282 | try { | |
| 283 | ||
| 284 | 0 | while ((inputLine = in.readLine()) != null) { |
| 285 | ||
| 286 | try { | |
| 287 | ||
| 288 | 0 | parsedLine = processRecord(inputLine); |
| 289 | ||
| 290 | 0 | if (parsedLine != null) { |
| 291 | ||
| 292 | 0 | sb.append(parsedLine); |
| 293 | ||
| 294 | } | |
| 295 | 0 | } catch (InvalidRecordException e) { |
| 296 | ||
| 297 | 0 | logger.log( |
| 298 | Level.SEVERE, | |
| 299 | "Skipping, unable to parse line, don't know how to handle this record type yet" | |
| 300 | + inputLine, | |
| 301 | e); | |
| 302 | ||
| 303 | 0 | } |
| 304 | ||
| 305 | } | |
| 306 | ||
| 307 | 0 | } catch (IOException e) { |
| 308 | ||
| 309 | 0 | logger.log(Level.SEVERE, "Problem reading file" + filename, e); |
| 310 | ||
| 311 | 0 | return null; |
| 312 | 0 | } |
| 313 | ||
| 314 | 0 | sb.append("</pbs_jobfile>"); |
| 315 | ||
| 316 | try { | |
| 317 | ||
| 318 | 0 | in.close(); |
| 319 | ||
| 320 | 0 | } catch (IOException e) { |
| 321 | ||
| 322 | 0 | logger.log(Level.SEVERE, "Unable to close input stream", e); |
| 323 | ||
| 324 | 0 | } |
| 325 | ||
| 326 | 0 | return sb.toString(); |
| 327 | } | |
| 328 | ||
| 329 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |