Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Fixed so that the version is stored in a separate XML file instead of…
Browse files Browse the repository at this point in the history
… the manifest which isn't parsed correctly on all systems.
  • Loading branch information
[email protected] committed Jul 23, 2012
1 parent 9e95a1c commit 67fc4ae
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
<type>2</type>
<locationURI>PROJECT_LOC/conf</locationURI>
</link>
<link>
<name>resources/data</name>
<type>2</type>
<locationURI>PROJECT_LOC/data</locationURI>
</link>
</linkedResources>
</projectDescription>
5 changes: 5 additions & 0 deletions data/version.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<version>
<revision>252M</revision>
<date>20120723 1307</date>
</version>
9 changes: 8 additions & 1 deletion make-jar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ Example: "100M" means revision 100 that was locally modified.

<exec executable="svnversion" outputproperty="svnrev">
</exec>


<echo message="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;" append="false" file="${basedir}/data/version.xml" />
<echo message="&lt;version&gt;&#10;" append="true" file="${basedir}/data/version.xml" />
<echo message=" &lt;revision&gt;${svnrev}&lt;/revision&gt;&#10;" append="true" file="${basedir}/data/version.xml" />
<echo message=" &lt;date&gt;${DSTAMP} ${TSTAMP}&lt;/date&gt;&#10;" append="true" file="${basedir}/data/version.xml" />
<echo message="&lt;/version&gt;&#10;" append="true" file="${basedir}/data/version.xml" />


<jar destfile="${basedir}/JAR/jMc2Obj-dev_r${svnrev}.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="org.jmc.Main"/>
Expand Down
83 changes: 43 additions & 40 deletions src/org/jmc/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.jmc.util.Log;
import org.jmc.util.Xml;
import org.w3c.dom.Document;


/**
Expand All @@ -18,60 +24,57 @@ public class Version

private static final Object syncobj=new Object();

private static String revstr=null;
public static String REVISION()
{
synchronized(syncobj)
{
if(revstr==null)
{
revstr=Version.class.getPackage().getImplementationVersion();
if(revstr==null)
revstr= "(local)";
}
}

return revstr;
}

private static Date dateval=null;
public static Date DATE()
{
private static void initialize()
{
synchronized(syncobj)
{
if(dateval==null)
{
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd hhmm");
if(revstr!=null && dateval!=null) return;

InputStream stream = Version.class.getResourceAsStream("/META-INF/MANIFEST.MF");
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd hhmm");

if(stream != null)
{
InputStream stream = Version.class.getClassLoader().getResourceAsStream("data/version.xml");

Manifest manifest = new Manifest(stream);
if(stream != null)
{

Attributes attributes = manifest.getMainAttributes();
Document doc = Xml.loadDocument(stream);
XPath xpath = XPathFactory.newInstance().newXPath();

String datestr=attributes.getValue("Built-Date");
revstr=(String) xpath.evaluate("/version/revision", doc, XPathConstants.STRING);
String datestr=(String) xpath.evaluate("/version/date", doc, XPathConstants.STRING);

if(datestr==null) datestr="";
if(datestr==null) datestr="";

dateval=sdf.parse(datestr);
}
else
{
Log.error("Cannot load manifest", null, false);
dateval=new Date(0);
}

}catch (Exception e) {
Log.error("Cannot find date of current version",e,false);
dateval=sdf.parse(datestr);
}
else
{
Log.error("Cannot load program version!", null, false);
dateval=new Date(0);
revstr="r0";
}

}catch (Exception e) {
Log.error("Cannot load program version",e,false);
dateval=new Date(0);
revstr="r0";
}
}
}


private static String revstr=null;
public static String REVISION()
{
initialize();
return revstr;
}

private static Date dateval=null;
public static Date DATE()
{
initialize();
return dateval;
}

Expand Down
16 changes: 16 additions & 0 deletions src/org/jmc/util/Xml.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jmc.util;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -53,6 +54,21 @@ public static Document loadDocument(URL url) throws Exception
return builder.parse(url.openStream());
}

/**
* Loads a XML document from a stream and returns the corresponding DOM Document.
*
* @param stream Input stream
* @return Parsed document
* @throws Exception if the loading or parsing fails
*/
public static Document loadDocument(InputStream stream) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(stream);
}


/**
* Creates a new document used for saving XML files.
* @return
Expand Down

0 comments on commit 67fc4ae

Please sign in to comment.