public final class Data extends Object
This class provides a number of services for working with KnowledgeStore data, i.e., with
Representation
, Value
, Statement
, Record
instances and
instances of scalar types that can be converted to Literal
values. The following
services are offered:
extensionToMimeType(String)
and
mimeTypeToExtensions(String)
allow to map from a file extension to the corresponding
MIME type and the other way round based on an internal database; in addition, method
isMimeTypeCompressible(String)
checks whether data of a MIME type can be effectively
compressed. MIME types and associated extensions have been imported from the Apache Web server
media type database (http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup). A list
of non-compressed media types have been obtained from here: http://pic.dhe.ibm.com/infocenter/storwize/unified_ic/index.jsp?topic=%2Fcom.ibm.storwize.
v7000.unified.140.doc%2Fifs_plancompdata.html. Some manual modification to the data has
been done as well, with to goal to enforce that a file association is mapped to at most a media
type (a few entries from the Apache DB had to be changed).getValueFactory()
returns a singleton,
memory-optimized ValueFactory
for creating Statement
s and Value
s (
URI
s, BNode
s, Literal
s); method getDatatypeFactory()
returns a
singleton factory for creating instances of XML Schema structured types, such as
XMLGregorianCalendar
instances.getTotalComparator()
and
getPartialComparator()
returns two general-purpose comparators that impose,
respectively, a total and a partial order over objects of the data model. The total order allow
comparing any pair of Value
s, Statement
s, Record
s and scalars
convertible to literal values; resorting to type comparison if the denoted value belong to
incomparable domains. The partial order compares only instances of compatible types (e.g.,
numbers with numbers), and can thus be better suited for the evaluation of conditions (while
the total order can help with the presentation of data).getNamespaceMap()
returns a prefix-to-namespace map with common bindings as defined on the prefix.cc
site
(as of end 2013); it can be used to achieve a reasonable presentation of URIs, e.g., for RDF
serialization. Methods newNamespaceMap()
and newNamespaceMap(Map, Map)
create
either an empty, optimized prefix-to-namespace map or a combined prefix-to-namespace map that
merges definitions of two input maps. Method namespaceToPrefix(String, Map)
performs a
reverse lookup in a prefix-to-namespace map, efficiently returning the prefix for a given
namespace.convert(Object, Class)
and
convert(Object, Class, Object)
attempts conversion from a data model object to another
data model class or scalar class compatible convertible to a literal object. More details about
the conversion are specified in the associated Javadoc documentation.normalize(Object)
and
normalize(Object, Collection)
accepts any object of a data model class or of a scalar
class convertible to a scalar literal (e.g., an integer) and normalize it to an instance of the
three data model classes: Value
, Statement
and Record
.toString(Object, Map)
and
toString(Object, Map, boolean)
generate a string representation of any data model
object, using the supplied prefix-to-namespace mappings and possibly including a full listing
of record properties. Method parseValue(String, Map)
allow parsing a Value
out
of a Turtle / TriG string, such as the ones produced by toString(...)
methods.TODO: this class needs urgent refactoring
Modifier and Type | Method and Description |
---|---|
static String |
cleanIRI(String string)
Clean an illegal IRI string, trying to make it legal (as per RFC 3987).
|
static String |
cleanURI(String string)
Clean a possibly illegal URI string (in a way similar to what a browser does), returning
the corresponding cleaned
URI object if successfull. |
static <T> T |
convert(Object object,
Class<T> clazz)
General conversion facility.
|
static <T> T |
convert(Object object,
Class<T> clazz,
T defaultValue)
General conversion facility, with fall back to default value.
|
static String |
extensionToMimeType(String fileNameOrExtension)
Returns the MIME type for the file extension specified, if known.
|
static DatatypeFactory |
getDatatypeFactory()
Returns a
DatatypeFactory for creating XMLGregorianCalendar instances and
instances of other XML schema structured types. |
static ListeningScheduledExecutorService |
getExecutor()
Returns the executor shared by KnowledgeStore components.
|
static Map<String,String> |
getNamespaceMap()
Returns a map of common prefix-to-namespace mappings, taken from a
prefix.cc dump. |
static Comparator<Object> |
getPartialComparator()
Returns a comparator imposing a partial order over objects of the data model (
Value
s, Statement s, Record s). |
static Comparator<Object> |
getTotalComparator()
Returns a comparator imposing a total order over objects of the data model (
Value s,
Statement s, Record s). |
static org.openrdf.model.ValueFactory |
getValueFactory()
Returns an optimized
ValueFactory for creating RDF URI s, BNode s,
Literal s and Statement s. |
static String |
hash(Object... objects)
Utility method to compute an hash string from a vararg list of objects.
|
static boolean |
isMimeTypeCompressible(String mimeType)
Checks whether the specific MIME type can be compressed.
|
static org.openrdf.model.URI |
languageCodeToURI(String code)
Returns the language URI for the ISO 639 code (2-letters, 3-letters) specified.
|
static String |
languageURIToCode(org.openrdf.model.URI uri)
Returns the 2-letter ISO 639 code for the language URI supplied.
|
static List<String> |
mimeTypeToExtensions(String mimeType)
Returns the file extensions commonly associated to the specified MIME type.
|
static String |
namespaceToPrefix(String namespace,
Map<String,String> namespaceMap)
Performs a reverse lookup of the prefix corresponding to a namespace in a
prefix-to-namespace map.
|
static Map<String,String> |
newNamespaceMap()
Creates a new, empty prefix-to-namespace map that supports fast reverse prefix lookup via
namespaceToPrefix(String, Map) . |
static Map<String,String> |
newNamespaceMap(Map<String,String> primaryNamespaceMap,
Map<String,String> secondaryNamespaceMap)
Creates a new prefix-to-namespace map combining the mappings in the supplied maps.
|
static Object |
normalize(Object object)
Normalizes the supplied object to an object of the data model.
|
static boolean |
normalize(Object object,
Collection<Object> collection)
Normalizes the supplied object to zero or more objects of the data model, which are added
to the collection specified.
|
static org.openrdf.model.Value |
parseValue(String string,
Map<String,String> namespaces)
Parses an RDF value out of a string.
|
static void |
setExecutor(ScheduledExecutorService newExecutor)
Setup the executor shared by KnowledgeStore components.
|
static String |
toString(Object object,
Map<String,String> namespaces)
Returns a string representation of the supplied data model object, optionally using the
supplied namespaces.
|
static String |
toString(Object object,
Map<String,String> namespaces,
boolean includeProperties)
Returns a string representation of the supplied data model object, optionally using the
supplied namespaces and including record properties.
|
static void |
validateIRI(String string)
Check that the supplied string is a legal IRI (as per RFC 3987).
|
public static ListeningScheduledExecutorService getExecutor()
setExecutor(ScheduledExecutorService)
, an executor is automatically created using
the thread number and naming given by system properties
eu.fbk.knowledgestore.threadCount
and eu.fbk.knowledgestore.threadNumber
.public static void setExecutor(ScheduledExecutorService newExecutor)
newExecutor
- the new executorpublic static org.openrdf.model.ValueFactory getValueFactory()
ValueFactory
for creating RDF URI
s, BNode
s,
Literal
s and Statement
s. Note that while any ValueFactory
can be
used for this purpose (including the ValueFactoryImpl
shipped with Sesame), the
factory returned by this method has been optimized to create objects that minimize the use
of memory, thus allowing to keep more objects / records in memory.ValueFactory
public static DatatypeFactory getDatatypeFactory()
DatatypeFactory
for creating XMLGregorianCalendar
instances and
instances of other XML schema structured types.DatatypeFactory
public static Comparator<Object> getTotalComparator()
Value
s,
Statement
s, Record
s). Objects are organized in groups: booleans, strings,
numbers (longs, ints, ...), calendar dates, URIs, BNodes and records. Ordering is done
first based on the group. Then, among a group the ordering is the natural one defined for
its elements, where applicable (booleans, strings, numbers, dates), otherwise the following
extensions are adopted: (i) statements are sorted by context first, followed by subject,
predicate and object; (ii) identifiers are sorted based on their string representation.
Note that comparison of dates follows the XML Schema specification with the only exception
that incomparable dates according to this specification (due to unknown timezone) are
considered equal.public static Comparator<Object> getPartialComparator()
Value
s, Statement
s, Record
s). This comparator behaves like the one of
getTotalComparator()
but in case objects belong to different groups (e.g., an
integer and a string) an exception is thrown rather than applying group sorting, thus
resulting in a more strict comparison that may be useful, e.g., for checking conditions.public static Map<String,String> getNamespaceMap()
prefix.cc
dump.
The returned map provides multiple prefixes for some namespace; it also support fast
reverse prefix lookup via namespaceToPrefix(String, Map)
.public static Map<String,String> newNamespaceMap()
namespaceToPrefix(String, Map)
.public static Map<String,String> newNamespaceMap(Map<String,String> primaryNamespaceMap, Map<String,String> secondaryNamespaceMap)
primaryNamespaceMap
take precedence, while the secondaryNamespaceMap
is accessed only if a mapping is not found in the primary map. Modification operations
target exclusively the primaryNamespaceMap
.primaryNamespaceMap
- the primary prefix-to-namespace map, not nullsecondaryNamespaceMap
- the secondary prefix-to-namespace map, not null@Nullable public static String namespaceToPrefix(String namespace, Map<String,String> namespaceMap)
BiMap
from Guava, a namespace map from
newNamespaceMap()
or the map of common prefix-to-namespace declarations); as a
result, calling this method may be significantly faster than manually looping over all the
prefix-to-namespace entries.namespace
- the namespace the corresponding prefix should be looked upnamespaceMap
- the prefix-to-namespace map containing the searched mappingpublic static boolean isMimeTypeCompressible(String mimeType)
mimeType
- the MIME typepublic static String extensionToMimeType(String fileNameOrExtension)
fileNameOrExtension
- the file extension or file name (from which the extension is extracted)public static List<String> mimeTypeToExtensions(String mimeType)
'.'
(e.g., txt
). In case multiple extensions are
returned, it is safe to consider the first one as the most common and preferred.mimeType
- the MIME type@Nullable public static org.openrdf.model.URI languageCodeToURI(@Nullable String code) throws IllegalArgumentException
http://lexvo.org/id/iso639-3/XYZ
.code
- the ISO 639 language code, possibly nullIllegalArgumentException
- in case the supplied string is not a valid ISO 639 2-letters or 3-letters code@Nullable public static String languageURIToCode(@Nullable org.openrdf.model.URI uri) throws IllegalArgumentException
http://lexvo.org/id/iso639-3/XYZ
.uri
- the language URI, possibly nullIllegalArgumentException
- if the supplied URI is not validpublic static String hash(Object... objects)
A-Za-z
and contains only characters
A-Za-z0-9
.objects
- the objects to compute the hash from@Nullable public static <T> T convert(@Nullable Object object, Class<T> clazz) throws IllegalArgumentException
object
to
an instance of the class specified. If the input is null, null is returned. If conversion
is unsupported or fails, an exception is thrown. The following table lists the supported
conversions:
From classes (and sub-classes) To classes (and super-classes) Boolean
,Literal
(xsd:boolean
)Boolean
,Literal
(xsd:boolean
),String
String
,Literal
(plain,xsd:string
)String
,Literal
(plain,xsd:string
),URI
(as uri string),BNode
(as BNode ID),Integer
,Long
,Double
,Float
,Short
,Byte
,BigDecimal
,BigInteger
,AtomicInteger
,AtomicLong
,Boolean
,XMLGregorianCalendar
,GregorianCalendar
,Date
(via parsing),Character
(length >= 1)Number
,Literal
(any numericxsd:
type)Literal
(top-level numericxsd:
type),Integer
,Long
,Double
,Float
,Short
,Byte
,BigDecimal
,BigInteger
,AtomicInteger
,AtomicLong
,String
Date
,GregorianCalendar
,XMLGregorianCalendar
,Literal
(xsd:dateTime
,xsd:date
)Date
,GregorianCalendar
,XMLGregorianCalendar
,Literal
(xsd:dateTime
),String
URI
URI
,Record
(ID assigned),String
BNode
BNode
,URI
(skolemization),String
Statement
Statement
,String
Record
Record
,URI
(ID extracted),String
T
- the type of resultobject
- the object to convert, possibly nullclazz
- the class to convert to, not nullobject
was nullIllegalArgumentException
- in case conversion fails or is unsupported for the object
and class
specified@Nullable public static <T> T convert(@Nullable Object object, Class<T> clazz, @Nullable T defaultValue)
convert(Object, Class)
, but in case the input is null or conversion is not
supported returns the specified default value.T
- the type of resultobject
- the object to convert, possibly nullclazz
- the class to convert to, not nulldefaultValue
- the default value to fall back toobject
was null,
conversion failed or is unsupported@Nullable public static Object normalize(@Nullable Object object) throws IllegalArgumentException
Record
, Value
,
Statement
), it is returned unchanged;IllegalArgumentException
is thrown otherwise;Value
is performed.object
- the object to normalize, possibly an array or iterableIllegalArgumentException
- in case the supplied object is an array or iterable with more than one element,
or if conversion was required but failed or was unsupportedpublic static boolean normalize(Object object, Collection<Object> collection) throws IllegalArgumentException
Record
, Value
,
Statement
), it is stored unchanged in the supplied collection;normalize()
is called for each of them, using the same collection supplied);Value
is performed.object
- the object to normalize, possibly an array or iterablecollection
- a collection where to add the resulting nodesIllegalArgumentException
- in case conversion was necessary but failed or was unsupportedpublic static void validateIRI(@Nullable String string) throws IllegalArgumentException
string
- the IRI string to checkIllegalArgumentException
- in case the IRI is illegal@Nullable public static String cleanIRI(@Nullable String string) throws IllegalArgumentException
string
- the IRI string to cleanIllegalArgumentException
- in case the supplied input cannot be transformed into a legal IRIpublic static String cleanURI(String string) throws IllegalArgumentException
URI
object if successfull. A null result is returned for
a null input. Cleaning consists in (i) encode Unicode characters above U+0080 as UTF-8
octet sequences and (ii) percent-encode all resulting characters that are illegal as per
RFC 3986 (i.e., characters that are not 'reserved' or 'unreserved' according to the RFC).
Note that relative URIs are rejected by this method.string
- the input stringIllegalArgumentException
- if the supplied string (after being cleaned) is still not valid (e.g., it does
not contain a valid URI scheme) or represent a relative URI@Nullable public static org.openrdf.model.Value parseValue(@Nullable String string, @Nullable Map<String,String> namespaces) throws ParseException
"literal", "literal"^^^datatype, "literal"@lang", <uri>, _:bnode
(strings
produced by toString(Object, Map, boolean)
obey this format).string
- the string to parse, possibly nullnamespaces
- the optional prefix-to-namespace mappings to use for parsing the string,
possibly nullParseException
- in case parsing fails@Nullable public static String toString(@Nullable Object object, @Nullable Map<String,String> namespaces, boolean includeProperties)
Value
,
Statement
, Record
instances and instances of scalar types that can be
converted to Value
s (via convert(Object, Class)
).object
- the data model object, possibly nullnamespaces
- the optional prefix-to-namespace mappings to use for generating the string,
possibly nullincludeProperties
- true if record properties should be included in the resulting string, in
addition to the record IDpublic static String toString(Object object, @Nullable Map<String,String> namespaces)
toString(Object, Map, boolean)
when no record properties are desired in output.object
- the data model object, possibly nullnamespaces
- the optional prefix-to-namespace mappings to use for generating the string,
possibly nullCopyright © 2015–2016 FBK-irst. All rights reserved.