From: <cr...@us...> - 2008-08-18 15:23:28
|
Revision: 4462 http://jnode.svn.sourceforge.net/jnode/?rev=4462&view=rev Author: crawley Date: 2008-08-18 15:23:22 +0000 (Mon, 18 Aug 2008) Log Message: ----------- Added some code to preserve copies of any original (non-generated) property files the first time that the tool is run. Modified Paths: -------------- trunk/builder/src/configure/org/jnode/configure/PropertySet.java trunk/builder/src/configure/org/jnode/configure/adapter/BasePropertyFileAdapter.java trunk/builder/src/configure/org/jnode/configure/adapter/FileAdapter.java trunk/builder/src/configure/org/jnode/configure/adapter/PropertyFileAdapter.java trunk/builder/src/configure/org/jnode/configure/adapter/XMLPropertyFileAdapter.java Modified: trunk/builder/src/configure/org/jnode/configure/PropertySet.java =================================================================== --- trunk/builder/src/configure/org/jnode/configure/PropertySet.java 2008-08-18 15:19:23 UTC (rev 4461) +++ trunk/builder/src/configure/org/jnode/configure/PropertySet.java 2008-08-18 15:23:22 UTC (rev 4462) @@ -178,15 +178,33 @@ adapter.load(this, configure); } + /** + * Save the properties in this property set to the property file. This + * method takes care of creating a backup file + * + * @param configure + * @throws ConfigureException + */ public void save(Configure configure) throws ConfigureException { if (file.exists()) { File file = this.file.getAbsoluteFile(); - File backup = new File(file.getParentFile(), file.getName() + ".bak"); - if (backup.exists()) { - if (!backup.delete()) { - throw new ConfigureException( - "Cannot delete existing '" + backup + "'"); + File backup; + if (adapter.wasSourceGenerated(this)) { + backup = new File(file.getParentFile(), file.getName() + ".bak"); + if (backup.exists()) { + if (!backup.delete()) { + throw new ConfigureException( + "Cannot delete existing '" + backup + "'"); + } } + } else { + backup = new File(file.getParentFile(), file.getName() + ".orig"); + int no = 1; + while (backup.exists()) { + backup = new File(file.getParentFile(), file.getName() + ".orig" + ++no); + } + configure.output("Saving the current (non-generated!) " + file.getName() + " file"); + configure.output("as " + backup); } if (!file.renameTo(backup)) { throw new ConfigureException( @@ -247,4 +265,5 @@ public Property getProperty(String name) { return properties.get(name); } + } Modified: trunk/builder/src/configure/org/jnode/configure/adapter/BasePropertyFileAdapter.java =================================================================== --- trunk/builder/src/configure/org/jnode/configure/adapter/BasePropertyFileAdapter.java 2008-08-18 15:19:23 UTC (rev 4461) +++ trunk/builder/src/configure/org/jnode/configure/adapter/BasePropertyFileAdapter.java 2008-08-18 15:23:22 UTC (rev 4462) @@ -28,6 +28,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -93,6 +94,66 @@ public boolean isSaveSupported() { return saveSupported; } + + public final boolean wasSourceGenerated(PropertySet propSet) throws ConfigureException { + File propFile = propSet.getFile(); + if (!propFile.exists()) { + return false; + } + String expectedFirstLine; + File templateFile = propSet.getTemplateFile(); + if (templateFile != null && templateFile.exists()) { + expectedFirstLine = readFirstLine(templateFile); + } else { + expectedFirstLine = getSignatureLine(); + } + String firstLine = readFirstLine(propFile); + return (firstLine == null || expectedFirstLine == null || + firstLine.equals(expectedFirstLine.trim())); + } + + /** + * Return the first non-trivial line in the file; i.e. the first line of the + * file whose length is > 3 after trimming. (A trimmed line with 1 to 3 + * characters is most likely to be some kind of comment marker.) + * + * @param file the file to be read. + * @return the line or <code>null</code>. + */ + private String readFirstLine(File file) { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String line; + while ((line = br.readLine()) != null) { + line = line.trim(); + if (line.length() > 3) { + return line; + } + } + return null; + } catch (IOException ex) { + return null; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + // ignore + } + } + } + } + + /** + * Override this method to return a fixed 'signature' line that a generated + * file will started. + * + * @return the signature line or <code>null</code>. + */ + protected String getSignatureLine() { + return null; + } public void load(PropertySet propSet, Configure configure) throws ConfigureException { File file = propSet.getFile(); Modified: trunk/builder/src/configure/org/jnode/configure/adapter/FileAdapter.java =================================================================== --- trunk/builder/src/configure/org/jnode/configure/adapter/FileAdapter.java 2008-08-18 15:19:23 UTC (rev 4461) +++ trunk/builder/src/configure/org/jnode/configure/adapter/FileAdapter.java 2008-08-18 15:23:22 UTC (rev 4462) @@ -63,11 +63,40 @@ */ public static final String TEXT_FORMAT = "text"; + /** + * Test if this adapter supported loading of property values. + * @return <code>true</code> if loading is supported. + */ public boolean isLoadSupported(); + /** + * Test if this adapter supported loading of property values without a template + * @return <code>true</code> if saving is supported. + */ public boolean isSaveSupported(); + /** + * Load the properties for a property set. + * @param propSet the property set to be loaded + * @param configure provides diagnostic methods + * @throws ConfigureException + */ public void load(PropertySet propSet, Configure configure) throws ConfigureException; + /** + * Save the properties for a property set. + * @param propSet the property set to be saved + * @param configure provides diagnostic methods + * @throws ConfigureException + */ public void save(PropertySet propSet, Configure configure) throws ConfigureException; + + /** + * Test if the property set's source file was, or could have been generated by this + * program. + * @param propSet tells us about the source file + * @return <code>true</code> if the file couols have been generated; <code>false</code> otherwise. + * @throws ConfigureException + */ + public boolean wasSourceGenerated(PropertySet propSet) throws ConfigureException; } Modified: trunk/builder/src/configure/org/jnode/configure/adapter/PropertyFileAdapter.java =================================================================== --- trunk/builder/src/configure/org/jnode/configure/adapter/PropertyFileAdapter.java 2008-08-18 15:19:23 UTC (rev 4461) +++ trunk/builder/src/configure/org/jnode/configure/adapter/PropertyFileAdapter.java 2008-08-18 15:23:22 UTC (rev 4462) @@ -32,6 +32,8 @@ * @author cr...@jn... */ public class PropertyFileAdapter extends BasePropertyFileAdapter { + + private static final String SIGNATURE = "# Generated by the JNode configure utility"; private static BasePropertyFileAdapter.ValueCodec PROPERTY_VALUE_CODEC = new PropertyValueCodec(); @@ -48,7 +50,13 @@ @Override protected void saveToFile(Properties props, OutputStream output, String comment) throws IOException { + output.write(SIGNATURE.getBytes()); + output.write(System.getProperty("line.separator").getBytes()); props.store(output, comment); } + @Override + protected String getSignatureLine() { + return SIGNATURE; + } } Modified: trunk/builder/src/configure/org/jnode/configure/adapter/XMLPropertyFileAdapter.java =================================================================== --- trunk/builder/src/configure/org/jnode/configure/adapter/XMLPropertyFileAdapter.java 2008-08-18 15:19:23 UTC (rev 4461) +++ trunk/builder/src/configure/org/jnode/configure/adapter/XMLPropertyFileAdapter.java 2008-08-18 15:23:22 UTC (rev 4462) @@ -31,6 +31,8 @@ * @author cr...@jn... */ public class XMLPropertyFileAdapter extends BasePropertyFileAdapter { + private static final String SIGNATURE = "<!-- Generated by the JNode configure utility -->"; + private static BasePropertyFileAdapter.ValueCodec XML_VALUE_CODEC = new XMLValueCodec(); public XMLPropertyFileAdapter() { @@ -45,6 +47,13 @@ @Override protected void saveToFile(Properties props, OutputStream output, String comment) throws IOException { + output.write(SIGNATURE.getBytes()); + output.write(System.getProperty("line.separator").getBytes()); props.storeToXML(output, comment); } + + @Override + protected String getSignatureLine() { + return SIGNATURE; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |