Coverage details for umich.cac.pbs.PbsToXml

LineHitsSource
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  
269public class PbsToXml {
27  
28     /**
29      * Logger for application
30      */
31  
326    private static Logger logger = Logger.getLogger(PbsToXml.class.getName());
33  
34     /**
35      * The new line seperator for this system
36      */
37  
389    private String nl = System.getProperty("line.separator");
39  
40     /**
41      * The DTD to use
42      */
43  
449    private String dtd = null;
45  
46     /**
47      * The W3C schema to use
48      */
49  
509    private String w3Cschema = null;
51  
52     /**
53      * Prints the usage to STDOUT and exits
54      */
55  
56     public static void usage() {
57  
580        System.out.println(
59             "Usage: PbsToXml <file.dtd | file.xsd> accountfilename outputfilename.xml");
60  
610        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  
640        System.exit(-1);
65  
660    }
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  
790        this.dtd = dtd;
80  
810    }
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  
940        this.w3Cschema = w3Cschema;
95  
960    }
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  
1080        if (args.length != 3 && args.length != 2) {
109  
1100            usage();
111  
112         }
113  
114         //Convert PBS file to XML and print it out
115  
1160        PbsToXml parse = new PbsToXml();
117  
1180        String filename = null;
1190        String outputFile = null;
120  
1210        if (args.length == 2) {
1220            filename = args[0];
1230            outputFile = args[1];
124         } else {
1250            String validator = args[0];
1260            if (validator.toUpperCase().endsWith(".DTD")) {
1270                parse.setDTD(validator);
1280            } else if (validator.toUpperCase().endsWith(".XSD")) {
1290                parse.setW3CSchema(validator);
130             } else {
1310                logger.log(
132                     Level.SEVERE,
133                     "File must end with a .xml or .xsd extension");
1340                System.exit(-1);
135             }
136  
1370            filename = args[1];
1380            outputFile = args[2];
139         }
140  
141         //Do some basic error checking
142  
1430        File testFile = new File(filename);
144  
1450        if (!testFile.exists()) {
1460            logger.log(Level.SEVERE, "File does not exist");
1470            System.exit(-1);
1480        } else if (!testFile.canRead()) {
1490            logger.log(Level.SEVERE, "Unable to read file, check permissions");
1500            System.exit(-1);
1510        } else if (!testFile.isFile()) {
1520            logger.log(Level.SEVERE, "Is not a file, please use a file");
1530            System.exit(-1);
154         }
155  
1560        String xmlFile = parse.convertToXml(filename);
1570        if (xmlFile == null) {
1580            logger.log(Level.SEVERE, "Unable to parse filename" + filename);
1590            System.exit(-1);
160         } else {
161  
162             try {
163  
1640                BufferedWriter out =
165                     new BufferedWriter(new FileWriter(outputFile));
1660                out.write(xmlFile);
1670                out.close();
1680            } catch (IOException e) {
1690                logger.log(
170                     Level.SEVERE,
171                     "Unable to write to file " + outputFile,
172                     e);
1730                System.exit(-1);
174  
1750            }
176  
177         }
178  
1790    }
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  
1989        String[] fields = inputLine.split(";");
199  
2009        if (fields.length < RecordPosition.FIELD_LENGTH_MIN
201             || fields.length > RecordPosition.FIELD_LENGTH_MAX) {
202  
2033            logger.log(Level.SEVERE, "Incorrect field length!");
204  
2053            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  
2156        StringBuffer xmlBuffer = new StringBuffer();
216  
217         //Determine the type of record we are reading
218  
2196        RecordType recordType =
220             new RecordType(fields[RecordPosition.RECORD_TYPE]);
221  
222         //Get a parser for this type of record
223  
2246        PbsRecordParser parser =
225             RecordFactory.getInstance().getRecordParser(recordType);
226  
227         //Process the record, appending XML output to the XML buffer
228  
2296        if (parser != null) {
2303            xmlBuffer.append(parser.processRecord(fields));
231         } else {
2323            throw new InvalidRecordException("Uknown record type" + recordType);
233         }
2343        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  
2470        BufferedReader in = null;
248  
249         try {
250  
2510            in = new BufferedReader(new FileReader(filename));
252  
2530        } catch (Exception e) {
254  
2550            logger.log(Level.SEVERE, "Unable to open file " + filename, e);
256  
2570            return null;
2580        }
259  
2600        StringBuffer sb = new StringBuffer();
261         String inputLine;
262         String parsedLine;
2630        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
2640        sb.append(nl);
2650        if (dtd != null) {
2660            sb.append("<!DOCTYPE pbs_jobfile SYSTEM \"" + dtd + "\">");
2670            sb.append(nl);
268         }
269  
2700        if (w3Cschema != null) {
2710            sb.append(
272                 "<pbs_jobfile xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\""
273                     + w3Cschema
274                     + "\">");
275  
276         } else {
2770            sb.append("<pbs_jobfile>");
278         }
279  
2800        sb.append(nl);
281  
282         try {
283  
2840            while ((inputLine = in.readLine()) != null) {
285  
286                 try {
287  
2880                    parsedLine = processRecord(inputLine);
289  
2900                    if (parsedLine != null) {
291  
2920                        sb.append(parsedLine);
293  
294                     }
2950                } catch (InvalidRecordException e) {
296  
2970                    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  
3030                }
304  
305             }
306  
3070        } catch (IOException e) {
308  
3090            logger.log(Level.SEVERE, "Problem reading file" + filename, e);
310  
3110            return null;
3120        }
313  
3140        sb.append("</pbs_jobfile>");
315  
316         try {
317  
3180            in.close();
319  
3200        } catch (IOException e) {
321  
3220            logger.log(Level.SEVERE, "Unable to close input stream", e);
323  
3240        }
325  
3260        return sb.toString();
327     }
328  
329 }

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.