| 1 | /* |
| 2 | * $Id: BaseVersionProperties.java,v 1.4 2006/02/15 08:10:55 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | package com.moesol.util; |
| 13 | |
| 14 | import java.io.FileInputStream; |
| 15 | import java.io.FileNotFoundException; |
| 16 | import java.io.FileWriter; |
| 17 | import java.io.IOException; |
| 18 | import java.io.Writer; |
| 19 | import java.util.Properties; |
| 20 | |
| 21 | import com.moesol.generator.core.ApplyException; |
| 22 | import com.moesol.generator.core.Template; |
| 23 | |
| 24 | /** |
| 25 | * @author Hastings |
| 26 | */ |
| 27 | public class BaseVersionProperties { |
| 28 | |
| 29 | protected Properties m_properties = new Properties(); |
| 30 | |
| 31 | public Properties getProperties() { |
| 32 | return m_properties; |
| 33 | } |
| 34 | |
| 35 | protected void writeVersionProperties(Writer wr) throws IOException { |
| 36 | Template t = new Template( |
| 37 | "buildVersion=${buildVersion}\n" + |
| 38 | "buildVersionCommas=${buildVersionCommas}\n" + |
| 39 | "buildDate=${buildDate}\n"); |
| 40 | try { |
| 41 | t.apply(m_properties, wr); |
| 42 | } catch (ApplyException e) { |
| 43 | throw new RuntimeException(e); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | protected void writeVersionProperties(String outputname) throws IOException { |
| 48 | if (!changedVersionProperties(outputname)) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | FileWriter fr = new FileWriter(outputname); |
| 53 | try { |
| 54 | writeVersionProperties(fr); |
| 55 | } finally { |
| 56 | fr.close(); |
| 57 | } |
| 58 | } |
| 59 | private boolean changedVersionProperties(String outputname) throws IOException { |
| 60 | try { |
| 61 | FileInputStream fis = new FileInputStream(outputname); |
| 62 | |
| 63 | try { |
| 64 | Properties checker = new Properties(); |
| 65 | checker.load(fis); |
| 66 | return checkForChanges(checker); |
| 67 | } finally { |
| 68 | fis.close(); |
| 69 | } |
| 70 | } catch (FileNotFoundException e) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | /** |
| 75 | * @param checker |
| 76 | * @return true if buildVersion, buildVersionCommas, or buildDate |
| 77 | * are different than checker. |
| 78 | */ |
| 79 | boolean checkForChanges(Properties checker) { |
| 80 | if (changedProperty(checker, "buildVersion")) { |
| 81 | return true; |
| 82 | } |
| 83 | if (changedProperty(checker, "buildVersionCommas")) { |
| 84 | return true; |
| 85 | } |
| 86 | if (changedProperty(checker, "buildDate")) { |
| 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | private boolean changedProperty(Properties old, String prop) { |
| 93 | String old_value = old.getProperty(prop); |
| 94 | String new_value = m_properties.getProperty(prop); |
| 95 | if (old_value == null) { |
| 96 | return true; |
| 97 | } |
| 98 | if (!old_value.equals(new_value)) { |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | } |