de.topicmapslab.majortom.util
Class LiteralUtils

java.lang.Object
  extended by de.topicmapslab.majortom.util.LiteralUtils

public class LiteralUtils
extends java.lang.Object

Utility class for literals. Checks the type of literals and transform string literals to a literal of a specific type.

Author:
Sven Krosse

Field Summary
private static java.util.regex.Pattern alternativeDateTimePattern
           
private static java.util.regex.Pattern charPattern
          regular expression of char
private static java.util.regex.Pattern datePattern
          regular expression of date
private static java.util.List<java.lang.String> datePatterns
          translation patterns of date
private static java.util.regex.Pattern dateTimePattern
          regular expression of dateTime
private static java.util.regex.Pattern decimalPattern
          regular expression of decimal
private static java.util.regex.Pattern integerPattern
          regular expression of integer
private static java.util.regex.Pattern quotedStringPattern
          regular expression of quoted strings
private static java.util.regex.Pattern timePattern
          regular expression of time
private static java.util.List<java.lang.String> timePatterns
          translation patterns of time
private static java.util.regex.Pattern tripleQuotedStringPattern
          regular expression of triple quoted strings
 
Constructor Summary
LiteralUtils()
           
 
Method Summary
static java.util.Calendar asDate(java.lang.String literal)
          Method formats the given literal as date literal.
static java.util.Calendar asDateTime(java.lang.String literal)
          Method formats the given literal as dateTime literal.
static java.math.BigDecimal asDecimal(java.lang.String literal)
          Method formats the given literal as decimal literal.
static java.math.BigInteger asInteger(java.lang.String literal)
          Method formats the given literal as integer literal.
static java.net.URI asIri(java.lang.String literal)
          Method formats the given literal as IRI literal.
static java.lang.Object asLiteral(java.lang.String literal, java.lang.String datatType)
          Transform the given literal to a literal of the specified type given by dataType IRI.
static java.lang.String asQuotedString(java.lang.String literal)
          Method formats the given literal as string literal.
static java.lang.String asString(java.lang.String literal)
          Method formats the given literal as string literal.
static java.util.Calendar asTime(java.lang.String literal)
          Method formats the given literal as time literal.
static java.lang.String asTripleQuotedString(java.lang.String literal)
          Method formats the given literal as string literal.
static boolean inRange(java.util.Calendar relative, java.util.Calendar value, double range)
          Method checks if the second calendar is before or after the first one and if the distance between the both calendars is smaller than the second calendar represented time range.
static boolean isDate(java.lang.String literal)
          Method checks if the given string literal can be represented as date literal.
static boolean isDateTime(java.lang.String literal)
          Method checks if the given string literal can be represented as dateTime literal.
static boolean isDecimal(java.lang.String literal)
          Method checks if the given string literal can be represented as decimal literal.
static boolean isInteger(java.lang.String literal)
          Method checks if the given string literal can be represented as integer literal.
static boolean isIri(java.lang.String literal)
          Method checks if the given string literal can be represented as IRI literal.
static boolean isQuotedString(java.lang.String literal)
          Method checks if the given string literal can be represented as quoted string literal.
static boolean isString(java.lang.String literal)
          Method checks if the given string literal can be represented as integer literal.
static boolean isTime(java.lang.String literal)
          Method checks if the given string literal can be represented as time literal.
static boolean isTripleQuotedString(java.lang.String literal)
          Method checks if the given string literal can be represented as triple quoted string literal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

datePattern

private static final java.util.regex.Pattern datePattern
regular expression of date


timePattern

private static final java.util.regex.Pattern timePattern
regular expression of time


dateTimePattern

private static final java.util.regex.Pattern dateTimePattern
regular expression of dateTime


alternativeDateTimePattern

private static final java.util.regex.Pattern alternativeDateTimePattern

decimalPattern

private static final java.util.regex.Pattern decimalPattern
regular expression of decimal


integerPattern

private static final java.util.regex.Pattern integerPattern
regular expression of integer


charPattern

private static final java.util.regex.Pattern charPattern
regular expression of char


quotedStringPattern

private static final java.util.regex.Pattern quotedStringPattern
regular expression of quoted strings


tripleQuotedStringPattern

private static final java.util.regex.Pattern tripleQuotedStringPattern
regular expression of triple quoted strings


datePatterns

private static final java.util.List<java.lang.String> datePatterns
translation patterns of date


timePatterns

private static final java.util.List<java.lang.String> timePatterns
translation patterns of time

Constructor Detail

LiteralUtils

public LiteralUtils()
Method Detail

isInteger

public static final boolean isInteger(java.lang.String literal)
Method checks if the given string literal can be represented as integer literal.

Parameters:
literal - the literal
Returns:
true if the literal is an integer literal, false otherwise.

isDecimal

public static final boolean isDecimal(java.lang.String literal)
Method checks if the given string literal can be represented as decimal literal.

Parameters:
literal - the literal
Returns:
true if the literal is a decimal literal, false otherwise.

isDate

public static final boolean isDate(java.lang.String literal)
Method checks if the given string literal can be represented as date literal.

Parameters:
literal - the literal
Returns:
true if the literal is a date literal, false otherwise.

isTime

public static final boolean isTime(java.lang.String literal)
Method checks if the given string literal can be represented as time literal.

Parameters:
literal - the literal
Returns:
true if the literal is a time literal, false otherwise.

isDateTime

public static final boolean isDateTime(java.lang.String literal)
Method checks if the given string literal can be represented as dateTime literal.

Parameters:
literal - the literal
Returns:
true if the literal is a dateTime literal, false otherwise.

isQuotedString

public static final boolean isQuotedString(java.lang.String literal)
Method checks if the given string literal can be represented as quoted string literal.

Parameters:
literal - the literal
Returns:
true if the literal is a quoted string literal, false otherwise.

isTripleQuotedString

public static final boolean isTripleQuotedString(java.lang.String literal)
Method checks if the given string literal can be represented as triple quoted string literal.

Parameters:
literal - the literal
Returns:
true if the literal is a triple quoted string literal, false otherwise.

isString

public static final boolean isString(java.lang.String literal)
Method checks if the given string literal can be represented as integer literal.

Parameters:
literal - the literal
Returns:
true if the literal is an integer literal, false otherwise.

isIri

public static final boolean isIri(java.lang.String literal)
Method checks if the given string literal can be represented as IRI literal.

Parameters:
literal - the literal
Returns:
true if the literal is an IRI literal, false otherwise.

asInteger

public static final java.math.BigInteger asInteger(java.lang.String literal)
                                            throws java.lang.NumberFormatException
Method formats the given literal as integer literal.

Parameters:
literal - the string literal
Returns:
the integer literal
Throws:
java.lang.NumberFormatException - thrown if literal cannot be format as number

asDecimal

public static final java.math.BigDecimal asDecimal(java.lang.String literal)
                                            throws java.lang.NumberFormatException
Method formats the given literal as decimal literal.

Parameters:
literal - the string literal
Returns:
the decimal literal
Throws:
java.lang.NumberFormatException - thrown if literal cannot be format as number

asDate

public static final java.util.Calendar asDate(java.lang.String literal)
                                       throws java.text.ParseException
Method formats the given literal as date literal.

Parameters:
literal - the string literal
Returns:
the date literal
Throws:
java.text.ParseException - thrown if literal cannot be format as date

asTime

public static final java.util.Calendar asTime(java.lang.String literal)
                                       throws java.text.ParseException
Method formats the given literal as time literal.

Parameters:
literal - the string literal
Returns:
the time literal
Throws:
java.text.ParseException - thrown if literal cannot be format as time

asDateTime

public static final java.util.Calendar asDateTime(java.lang.String literal)
                                           throws java.text.ParseException
Method formats the given literal as dateTime literal.

Parameters:
literal - the string literal
Returns:
the dateTime literal
Throws:
java.text.ParseException - thrown if literal cannot be format as dateTime

asQuotedString

public static final java.lang.String asQuotedString(java.lang.String literal)
Method formats the given literal as string literal.

Parameters:
literal - the quoted string literal
Returns:
the string literal without quotes

asTripleQuotedString

public static final java.lang.String asTripleQuotedString(java.lang.String literal)
Method formats the given literal as string literal.

Parameters:
literal - the triple quoted string literal
Returns:
the string literal without quotes

asString

public static final java.lang.String asString(java.lang.String literal)
Method formats the given literal as string literal.

Parameters:
literal - the string literal
Returns:
the string literal without quotes

asIri

public static final java.net.URI asIri(java.lang.String literal)
                                throws java.net.URISyntaxException
Method formats the given literal as IRI literal.

Parameters:
literal - the string literal
Returns:
the string literal without quotes
Throws:
java.net.URISyntaxException - thrown if given literal isn't a valid IRI

asLiteral

public static java.lang.Object asLiteral(java.lang.String literal,
                                         java.lang.String datatType)
                                  throws java.lang.Exception
Transform the given literal to a literal of the specified type given by dataType IRI.

Parameters:
literal - the literal value to convert
datatType - the IRI of the data-type
Returns:
the transformed literal or a string literal if the value does not matches the regular expression of the given type.
Throws:
java.lang.Exception - thrown if transformation fails

inRange

public static boolean inRange(java.util.Calendar relative,
                              java.util.Calendar value,
                              double range)
Method checks if the second calendar is before or after the first one and if the distance between the both calendars is smaller than the second calendar represented time range.

Parameters:
relative - the calendar
value - the calendar to check
double - the range representation
Returns:
true if the second calendar is in the range, false otherwise.