VersionParser.java
/* Generated By:JavaCC: Do not edit this line. VersionParser.java */
/*
* Copyright (C) 2005 Christian Schulte <cs@schulte.it>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $JOMC: VersionParser.jj 5091 2016-04-04 15:40:17Z schulte $
*
*/
package org.jomc.util;
import java.io.StringReader;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.LinkedList;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Parses and compares version identifiers.
* <p><blockquote><pre>
* Version ::= Token ( ( <SEPARATOR> )* Token )* <EOF>
* Token ::= <INTEGER>
* | <IDENTIFIER>
* </pre></blockquote></p>
* <p>
* A separator character is defined as<blockquote><pre>
* [".","_","-","@","/","\\"," ","\t","\n","\r","\f","\b","\"","\'"]</pre></blockquote>
* An integer is a sequence of digits. An identifier is everything else, not
* a separator character or an integer.
* </p>
*
* @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
* @version $JOMC: VersionParser.jj 5091 2016-04-04 15:40:17Z schulte $
* @see #compare(String, String)
*/
public final class VersionParser implements VersionParserConstants {
/**
* Parses the input to produce an array of tokens.
*
* @return The parsed tokens.
*
* @throws ParseException if the parse fails.
* @throws TokenMgrError for any invalid tokens.
*/
public Token[] parse() throws ParseException, TokenMgrError
{
return this.Version();
}
/**
* Compares two versions for order.
* <p>This method parses the given strings to produce a sequence of tokens and then compares these tokens for
* order.</p>
*
* @param v1 The version to compare with.
* @param v2 The version to compare to.
*
* @return A negative integer, zero, or a positive integer as the first version is less than, equal to, or greater
* than the second.
*
* @throws NullPointerException if {@code v1} or {@code v2} is {@code null}.
* @throws ParseException if parsing fails.
* @throws TokenMgrError for any invalid tokens.
*/
public static int compare( final String v1, final String v2 ) throws ParseException, TokenMgrError
{
if ( v1 == null )
{
throw new NullPointerException( "v1" );
}
if ( v2 == null )
{
throw new NullPointerException( "v2" );
}
try
{
final NumberFormat format = NumberFormat.getNumberInstance( Locale.ENGLISH );
final StringReader v1Reader = new StringReader( v1 );
final VersionParser versionParser = new VersionParser( v1Reader );
final Token[] c = versionParser.parse();
final StringReader v2Reader = new StringReader( v2 );
versionParser.ReInit( v2Reader );
final Token[] r = versionParser.parse();
final int len = Math.max( c.length, r.length );
int result = 0;
v1Reader.close();
v2Reader.close();
for ( int i = 0; i < len; i++ )
{
final Token current;
final Token spec;
if ( i < c.length )
{
current = c[i];
}
else
{
current = new Token();
current.kind = r[i].kind;
if ( r[i].kind == VersionParserConstants.IDENTIFIER )
{
// If a version has less tokens than another, comparison is stopped
// at the first identifier. Remaining tokens are considered suffices
// less than the shorter version.
result = 1;
break;
}
else if ( r[i].kind == VersionParserConstants.INTEGER )
{
current.image = "0";
}
}
if ( i < r.length )
{
spec = r[i];
}
else
{
spec = new Token();
spec.kind = c[i].kind;
if ( c[i].kind == VersionParserConstants.IDENTIFIER )
{
// If a version has less tokens than another, comparison is stopped
// at the first identifier. Remaining tokens are considered suffices
// less than the shorter version.
result = -1;
break;
}
else if ( c[i].kind == VersionParserConstants.INTEGER )
{
spec.image = "0";
}
}
if ( current.kind != spec.kind )
{
throw new ParseException( getMessage( "cannotCompare", current.image, spec.image, v1, v2 ) );
}
if ( current.kind == VersionParserConstants.IDENTIFIER )
{
result = current.image.compareTo( spec.image );
if ( result != 0 )
{
break;
}
}
else if ( current.kind == VersionParserConstants.INTEGER )
{
final Long m = (Long) format.parse( current.image );
final Long n = (Long) format.parse( spec.image );
result = m.compareTo( n );
if ( result != 0 )
{
break;
}
}
else
{
// Unsupported tokens are compared lexicographically by default.
result = current.image.compareTo( spec.image );
if ( result != 0 )
{
break;
}
}
}
return result;
}
catch ( java.text.ParseException e )
{
throw new ParseException( e.getMessage() );
}
}
private static String getMessage( final String key, final Object... arguments )
{
return MessageFormat.format( ResourceBundle.getBundle( VersionParser.class.getName().replace( '.', '/' ),
Locale.getDefault() ).getString( key ),
arguments );
}
final private Token[] Version() throws ParseException {
final List tokens = new LinkedList();
Token(tokens);
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INTEGER:
case SEPARATOR:
case IDENTIFIER:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
label_2:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SEPARATOR:
;
break;
default:
jj_la1[1] = jj_gen;
break label_2;
}
jj_consume_token(SEPARATOR);
}
Token(tokens);
}
jj_consume_token(0);
{if (true) return (Token[]) tokens.toArray(new Token[tokens.size()]);}
throw new Error("Missing return statement in function");
}
final private void Token(final List tokens) throws ParseException {
Token part;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INTEGER:
part = jj_consume_token(INTEGER);
tokens.add ( part );
break;
case IDENTIFIER:
part = jj_consume_token(IDENTIFIER);
tokens.add( part );
break;
default:
jj_la1[2] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
/** Generated Token Manager. */
public VersionParserTokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
public Token token;
/** Next token. */
public Token jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[3];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0xe,0x4,0xa,};
}
/** Constructor with InputStream. */
public VersionParser(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public VersionParser(java.io.InputStream stream, String encoding) {
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new VersionParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 3; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 3; i++) jj_la1[i] = -1;
}
/** Constructor. */
public VersionParser(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new VersionParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 3; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 3; i++) jj_la1[i] = -1;
}
/** Constructor with generated Token Manager. */
public VersionParser(VersionParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 3; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(VersionParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 3; i++) jj_la1[i] = -1;
}
private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
if (token.kind == kind) {
jj_gen++;
return token;
}
token = oldToken;
jj_kind = kind;
throw generateParseException();
}
/** Get the next Token. */
final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
jj_gen++;
return token;
}
/** Get the specific Token. */
final public Token getToken(int index) {
Token t = token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
else t = t.next = token_source.getNextToken();
}
return t;
}
private int jj_ntk() {
if ((jj_nt=token.next) == null)
return (jj_ntk = (token.next=token_source.getNextToken()).kind);
else
return (jj_ntk = jj_nt.kind);
}
private java.util.List jj_expentries = new java.util.ArrayList();
private int[] jj_expentry;
private int jj_kind = -1;
/** Generate ParseException. */
public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[4];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 3; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<<j)) != 0) {
la1tokens[j] = true;
}
}
}
}
for (int i = 0; i < 4; i++) {
if (la1tokens[i]) {
jj_expentry = new int[1];
jj_expentry[0] = i;
jj_expentries.add(jj_expentry);
}
}
int[][] exptokseq = new int[jj_expentries.size()][];
for (int i = 0; i < jj_expentries.size(); i++) {
exptokseq[i] = (int[])jj_expentries.get(i);
}
return new ParseException(token, exptokseq, tokenImage);
}
/** Enable tracing. */
final public void enable_tracing() {
}
/** Disable tracing. */
final public void disable_tracing() {
}
}