Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* @(#) $Id: VersionNumber.java 2548 2008-04-21 13:06:32Z gub $
*/
package at.spardat.xma.boot.cache;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Support for version numbers in resource file names.
* It supports parsing of version numbers, insertion of version number for delta files,
* insertion of hash values into the filename and matching of file names that differ only in
* version numbers or hash values.
* @author s2877
* @since 1.3.0
*/
public class VersionNumber implements Comparable {
final static Pattern pattern = Pattern.compile("[_-](\\d+)\\.(\\d+)\\.(\\d+)\\.");
final static Pattern hash = Pattern.compile("[0-9a-f]{32}");
int major,minor,bugfix;
/**
* Constructs a VersionNumber with the given major, minor and bugfix number.
* @param major part of the version
* @param minor part of the version
* @param bugfix part of the version
*/
public VersionNumber(int major,int minor, int bugfix) {
this.major=major;
this.minor=minor;
this.bugfix=bugfix;
}
/**
* Parses the version number out of the given filename.
* @param filename the filename to parse
* @return the parsed version number or null if no version number is contained in the filename
*/
static public VersionNumber parse(String filename) {
Matcher matcher = pattern.matcher(filename);
if(matcher.find()){
return new VersionNumber(
Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3))
);
} else return null;
}
/**
* Creates a search pattern that matches all other versions of the file defined by the given filename.
* Files with and without contained hash value in its name are matched.
* The filename must not contain any path (/ or \ charchters)
* @param filename to generate the search pattern for
* @return a pattern matching all other versions of the given file
*/
static public Pattern searchPattern(String filename) {
Matcher matcher = pattern.matcher(filename);
if(matcher.find()) {
Matcher ext = hash.matcher(filename);
if(ext.find()) { // cut out hash
filename = filename.substring(0,ext.start()-1)+filename.substring(ext.end(),filename.length());
}
String search = filename.substring(0,matcher.start()) + pattern.pattern()
+ filename.substring(matcher.end(),filename.length());
// escape all unescaped "."
search = search.replaceAll("([^\\\\])\\.","$1\\\\.");
// optional hash inclusion befor last dot
int lastdot = search.lastIndexOf('.');
search = search.substring(0,lastdot+1) + "(" + hash.pattern() + "\\.)?"
+ search.substring(lastdot+1,search.length());
return Pattern.compile("^"+search+"$");
} else {
return null;
}
}
/**
* Insert this version number into the given url to make it specifie the delta file
* to the file given in the url.
* e.g. http://localhost/myapp/test_1.2.0.jar -> http://localhost/myapp/test_1.1.0-1.2.0.xdelta.jar
* @param url to convert into a delta download url
* @return the modified url
*/
public URL insertAsDelta(URL url) {
String filename = url.toString();
Matcher matcher = pattern.matcher(filename);
if(!matcher.find()) return url;
try {
return new URL(
filename.substring(0,matcher.start())+ "_"
+ major + "." + minor + "." + bugfix + "-"
+ filename.substring(matcher.start()+1,matcher.end()-1)
+ ".xdelta" + filename.substring(matcher.end()-1,filename.length())
);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
/**
* Inserts the given hash value into the given filename. The hash value is inserted
* just bevor the last file extention. If the file has no extention the hash value is appended.
* @param filename to insert the hash value into
* @param hash to insert into the filename
* @return the modified filename
*/
static public String insertHash(String filename,String hash) {
int lastdot = filename.lastIndexOf('.');
if(lastdot>=0) {
return filename.substring(0,lastdot) + "." + hash + filename.substring(lastdot,filename.length());
} else {
return filename + "." + hash;
}
}
/**
* Creates a search pattern that matches all file defined by the given filename
* regardless of any included hash value.
* The filename must not contain any path (/ or \ charchters)
* @param filename to generate the search pattern for
* @return a pattern matching all other versions of the given file
*/
static public Pattern matchWithoutHashPattern(String filename) {
Matcher matcher = hash.matcher(filename);
if(matcher.find()) {
filename = filename.substring(0,matcher.start())
+ "(" + hash.pattern() + "\\.)?"
+ (matcher.end()
* 0 if this VersionNumber is equal to the other
* 1 if this VersionNumber is greater than the other
* @throws ClassCastException if the parameter is not a VersionNumber.
*/
public int compareTo(Object other) {
if(other==null) throw new NullPointerException();
if(!(other instanceof VersionNumber)) throw new ClassCastException();
VersionNumber number = (VersionNumber) other;
if(majornumber.major) return 1;
if(minornumber.minor) return 1;
if(bugfixnumber.bugfix) return 1;
return 0;
}
/**
* Tests if this VersionNumber is equal to an other
* @param other the VersionNumber to compare to.
* @return true if major, minor and bugfix are equal to the
* respective fields of the other VersionNumber.
*/
public boolean equals(Object other) {
if(!(other instanceof VersionNumber)) return false;
VersionNumber number = (VersionNumber) other;
if(major!=number.major) return false;
if(minor!=number.minor) return false;
if(bugfix!=number.bugfix) return false;
return true;
}
/**
* Gives a hash code value of this object.
* The hash value is determined by the values of major, minor and bugfix.
* If two VersionNumbers are equal, they have the same hash code value.
*/
public int hashCode() {
return toString().hashCode();
}
}