/* copyright Spry Software, Inc., 2005-2007 package spry.reader.stdf; import java.io.*; import java.lang.reflect.*; import java.beans.*; import spry.input.DataRecord; import spry.input.RecordUser; import spry.ui.util.ObjectArray; /** * StdfPrinter reads an STDF file and dumps all attributes of all * records in the file to stdout * */ public class StdfPrinter extends AbstractStdfUser implements RecordUser { private boolean recordNamesOnly = false; private boolean printHeader = false; private PrintStream out; /** * @return Returns the printHeader. */ public boolean isPrintHeader() { return printHeader; } /** * If printHeader is true, the STDF header for each record * will be printed * @param printHeader The printHeader to set. */ public void setPrintHeader(boolean printHeader) { this.printHeader = printHeader; } /** * @return true if only the names of records will be printed and not * the attributes */ public boolean isRecordNamesOnly() { return recordNamesOnly; } /** * If recordNamesOnly is true, only record types and not * attributes will be printed * @param recordNamesOnly The recordNamesOnly to set. */ public void setRecordNamesOnly(boolean recordNamesOnly) { this.recordNamesOnly = recordNamesOnly; } /* (non-Javadoc) * @see reader.stdf.StdfUser#processFtr(reader.stdf.FTR) */ public void processFtr(FTR ftr) { // out.println("FTR record"); dumpAttributes(ftr); } /* (non-Javadoc) * @see reader.stdf.StdfUser#processMpr(reader.stdf.MPR) */ public void processMpr(MPR mpr) { //out.println("MPR record"); dumpAttributes(mpr); } /* (non-Javadoc) * @see reader.stdf.StdfUser#processPtr(reader.stdf.PTR) */ public void processPtr(PTR ptr) { // out.println("PTR record"); dumpAttributes(ptr); } public StdfPrinter(String fileName) { try { if (fileName != null) { File file = new File(fileName); if (file.exists()) file.delete(); out = new PrintStream(new BufferedOutputStream(new FileOutputStream(file))); } else out = System.out; } catch (IOException i) { i.printStackTrace(); } } public void finish() { out.close(); } public void processFar(FAR far) { //out.println("FAR record"); dumpAttributes(far); } public void processAtr(ATR atr) { //out.println("ATR record"); dumpAttributes(atr); } public void processMir(MIR mir) { //out.println("MIR record"); dumpAttributes(mir); } public void processMrr(MRR mrr) { //out.println("MRR record"); dumpAttributes(mrr); } /* (non-Javadoc) * @see spry.input.RecordUser#processRecord(spry.input.DataRecord) */ public void processRecord(DataRecord record) { dumpAttributes(record); } /** * processHeader prints the names and values of the STDF * record header fields * @param length The length of the record, excluding the header * @param type The type of the STDF record * @param subtype The subtype of the record */ public boolean processHeader(int length, short type, short subtype) { if (printHeader) { out.print("length: "); out.print(length); out.print(" type: "); out.print(type); out.print(" subtype: "); out.print(subtype); out.println(""); } return true; } // dumps all attribute names and values for record to stdout private void dumpAttributes(DataRecord record) { try { if ( recordNamesOnly) { out.println(record.getClass().toString()); } else { BeanInfo info = Introspector.getBeanInfo(record.getClass()); PropertyDescriptor[] properties = info.getPropertyDescriptors(); for (int index=0; index < properties.length; index++) { Method method = properties[index].getReadMethod(); out.print(properties[index].getName()); out.print(": "); Object obj = null; if (properties[index] instanceof IndexedPropertyDescriptor) { obj = ObjectArray.getValuesString( (IndexedPropertyDescriptor)properties[index], record); } else obj = method.invoke(record,null); if (obj == null) out.print("" ); else out.print(obj.toString()); out.println(""); } } } catch (Exception i) { i.printStackTrace(); } catch (Error e) { e.printStackTrace(); System.out.println("found error "+e.toString()); } } public void processPcr(PCR pcr) { dumpAttributes(pcr); } public void processSbr(SBR sbr) { dumpAttributes(sbr); } public void processHbr(HBR hbr) { dumpAttributes(hbr); } public void processPmr(PMR pmr) { dumpAttributes(pmr); } public void processPgr(PGR pgr) { dumpAttributes(pgr); } public void processPlr(PLR plr) { dumpAttributes(plr); } public void processRdr(RDR rdr) { dumpAttributes(rdr); } public void processSdr(SDR sdr) { dumpAttributes(sdr); } public void processWir(WIR wir) { dumpAttributes(wir); } public void processWrr(WRR wrr) { dumpAttributes(wrr); } public void processWcr(WCR wcr) { dumpAttributes(wcr); } public void processPir(PIR pir) { dumpAttributes(pir); } public void processPrr(PRR prr) { dumpAttributes(prr); } public void processTsr(TSR tsr) { dumpAttributes(tsr); } /* (non-Javadoc) * @see reader.stdf.StdfUser#processBps(reader.stdf.BPS) */ public void processBps(BPS bps) { out.println("BPS record"); dumpAttributes(bps); } /* (non-Javadoc) * @see reader.stdf.StdfUser#processDtr(reader.stdf.DTR) */ public void processDtr(DTR dtr) { out.println("DTR record"); dumpAttributes(dtr); } /* (non-Javadoc) * @see reader.stdf.StdfUser#processEps(reader.stdf.EPS) */ public void processEps(EPS eps) { out.println("EPS record"); dumpAttributes(eps); } /* (non-Javadoc) * @see reader.stdf.StdfUser#processGdr(reader.stdf.GDR) */ public void processGdr(GDR gdr) { out.println("GDR record"); dumpAttributes(gdr); } public static void main (String[] args) { if (args == null || args.length == 0) { System.err.println("Enter stdf file name as a command line argument"); System.exit(1); } int argsIndex = 0; String fileName = null; String outputFileName = null; boolean namesOnly = false; boolean printHeader = false; while (argsIndex < args.length) { if (args[argsIndex].equals("-recordnames") || args[argsIndex].equals("-r")) { namesOnly = true; } else if (args[argsIndex].equals("printheader") || args[argsIndex].equals("-p")) printHeader = true; else if (args[argsIndex].equals("-output")) { argsIndex++; if (argsIndex >= args.length) break; outputFileName = args[argsIndex]; } else fileName = args[argsIndex]; argsIndex++; } File stdfFile = new File(fileName); if (!stdfFile.exists()) { System.err.println(args[0] + " does not exist"); System.exit(2); } StdfPrinter printer = new StdfPrinter(outputFileName); printer.setRecordNamesOnly(namesOnly); printer.setPrintHeader(printHeader); StdfReader reader = new StdfReader(); reader.setUser(printer); try { reader.readFile(stdfFile); } catch (IOException i) { i.printStackTrace(); } } }