Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 915 | migan | 1 | import items.*; |
| 2 | |||
| 3 | import java.io.BufferedWriter; |
||
| 4 | import java.io.FileWriter; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.text.SimpleDateFormat; |
||
| 7 | import java.util.ArrayList; |
||
| 8 | import java.util.Date; |
||
| 9 | import java.util.Iterator; |
||
| 10 | |||
| 11 | |||
| 12 | public class CHeader { |
||
| 13 | private String filename; |
||
| 14 | |||
| 15 | public CHeader(String filename) |
||
| 16 | { |
||
| 17 | this.filename = filename; |
||
| 18 | } |
||
| 19 | |||
| 20 | public void build(Database db) |
||
| 21 | { |
||
| 22 | System.out.println("Trying to rebuild " + this.filename); |
||
| 23 | |||
| 24 | try |
||
| 25 | { |
||
| 26 | FileWriter fstream = new FileWriter(this.filename); |
||
| 27 | BufferedWriter out = new BufferedWriter(fstream); |
||
| 28 | |||
| 29 | SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss"); |
||
| 30 | String formattedDate = formatter.format(new Date()); |
||
| 31 | |||
| 32 | out.write("// Built on " + formattedDate + " by CanIdManager\n"); |
||
| 33 | out.write("\n"); |
||
| 34 | |||
| 35 | this.outputClasses(out, db); |
||
| 36 | |||
| 37 | this.outputCommands(out, db); |
||
| 38 | |||
| 39 | this.outputModules(out, db); |
||
| 40 | |||
| 41 | out.close(); |
||
| 42 | |||
| 43 | System.out.println("You will now need to recompile all you modules and applications ;)"); |
||
| 44 | } |
||
| 45 | catch (Exception e) |
||
| 46 | { |
||
| 47 | System.err.println("Failed to write " + this.filename); |
||
| 48 | System.err.println("Error: " + e.getMessage()); |
||
| 49 | System.exit(1); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | private void outputClasses(BufferedWriter out, Database db) throws IOException |
||
| 54 | { |
||
| 55 | out.write("//------------------ //\n"); |
||
| 56 | out.write("// Class definitions //\n"); |
||
| 57 | out.write("//------------------ //\n"); |
||
| 58 | |||
| 59 | ArrayList<CanClass> classes = db.getClasses(); |
||
| 60 | |||
| 61 | Iterator<CanClass> iterator = classes.iterator(); |
||
| 62 | |||
| 63 | while (iterator.hasNext()) |
||
| 64 | { |
||
| 65 | CanClass canClass = iterator.next(); |
||
| 66 | out.write("#define " + canClass.getCDefineName() + "\t= " + canClass.getId() + " // " + canClass.getDescription() + "\n"); |
||
| 67 | } |
||
| 68 | |||
| 69 | out.write("\n"); |
||
| 70 | } |
||
| 71 | |||
| 72 | private void outputCommands(BufferedWriter out, Database db) throws IOException |
||
| 73 | { |
||
| 74 | out.write("//-------------------- //\n"); |
||
| 75 | out.write("// Command definitions //\n"); |
||
| 76 | out.write("//-------------------- //\n"); |
||
| 77 | |||
| 78 | ArrayList<Command> commands = db.getCommands(); |
||
| 79 | |||
| 80 | Iterator<Command> iterator = commands.iterator(); |
||
| 81 | |||
| 82 | while (iterator.hasNext()) |
||
| 83 | { |
||
| 84 | Command command = iterator.next(); |
||
| 85 | out.write("#define " + command.getCDefineName() + "\t= " + command.getId() + " // " + command.getDescription() + "\n"); |
||
| 86 | } |
||
| 87 | |||
| 88 | out.write("\n"); |
||
| 89 | } |
||
| 90 | |||
| 91 | private void outputModules(BufferedWriter out, Database db) throws IOException |
||
| 92 | { |
||
| 93 | out.write("//------------------- //\n"); |
||
| 94 | out.write("// Module definitions //\n"); |
||
| 95 | out.write("//------------------- //\n"); |
||
| 96 | |||
| 97 | ArrayList<Module> modules = db.getModules(); |
||
| 98 | |||
| 99 | Iterator<Module> iterator = modules.iterator(); |
||
| 100 | |||
| 101 | while (iterator.hasNext()) |
||
| 102 | { |
||
| 103 | Module module = iterator.next(); |
||
| 104 | out.write("#define " + module.getCDefineName() + "\t= " + module.getId() + " // " + module.getDescription() + "\n"); |
||
| 105 | } |
||
| 106 | |||
| 107 | out.write("\n"); |
||
| 108 | } |
||
| 109 | } |