public abstract class PropertyBasedDocumentFactory extends AbstractDocumentFactory
Many document factories need a number of default values that are used when
the metadata passed to
DocumentFactory.getDocument(java.io.InputStream,Reference2ObjectMap) is
not sufficient or lacks some key. This abstract class provides a common base for all such factories.
All concrete implementations of this class should have:
Reference2ObjectMap
having Enum keys;
Properties object;
In the third case, the properties will be parsed by the parseProperties(Properties)
method. In the fourth case, by the parseProperties(String[]) method.
Since all implementations are expected to provide such constructors, corresponding static factory methods have been provided to simplify factory instantiation.
If the implementation needs to read and parse some key, it must override the
parseProperty(String, String[], Reference2ObjectMap) method.
Keys are specified with a dotted notation. The last dot-separated token is the actual key. The prefix is used to select properties: only properties with a prefix that is a prefix of the current class name are considered. Moreover, if a property with a completely specified prefix (i.e., a prefix that is a class name) is not parsed an exception will be thrown.
This class provide helpers methods resolve(Enum, Reference2ObjectMap) and resolveNotNull(Enum, Reference2ObjectMap)
to help in writing implementations of DocumentFactory.getDocument(java.io.InputStream,Reference2ObjectMap) that
handle default metadata correctly.
| Modifier and Type | Class and Description |
|---|---|
static class |
PropertyBasedDocumentFactory.MetadataKeys
Case-insensitive keys for metadata passed to
DocumentFactory.getDocument(java.io.InputStream,it.unimi.dsi.fastutil.objects.Reference2ObjectMap). |
DocumentFactory.FieldType| Modifier and Type | Field and Description |
|---|---|
protected Reference2ObjectMap<Enum<?>,Object> |
defaultMetadata
The set of default metadata for this factory.
|
| Modifier | Constructor and Description |
|---|---|
protected |
PropertyBasedDocumentFactory() |
protected |
PropertyBasedDocumentFactory(Properties properties) |
protected |
PropertyBasedDocumentFactory(Reference2ObjectMap<Enum<?>,Object> defaultMetadata) |
protected |
PropertyBasedDocumentFactory(String[] property) |
| Modifier and Type | Method and Description |
|---|---|
protected static String |
ensureJustOne(String key,
String[] values)
This method checks that the array of values contains just one element, and returns the element.
|
static PropertyBasedDocumentFactory |
getInstance(Class<?> klass) |
static PropertyBasedDocumentFactory |
getInstance(Class<?> klass,
Properties properties) |
static PropertyBasedDocumentFactory |
getInstance(Class<?> klass,
Reference2ObjectMap<Enum<?>,Object> metadata) |
static PropertyBasedDocumentFactory |
getInstance(Class<?> klass,
String[] property) |
Reference2ObjectMap<Enum<?>,Object> |
parseProperties(Properties properties)
Scans the property set, parsing the properties that concern this class.
|
Reference2ObjectMap<Enum<?>,Object> |
parseProperties(String[] property)
Parses the given list of properties either as key=value specs (value may
be a list of comma-separated values), or as filenames.
|
protected boolean |
parseProperty(String key,
String[] valuesUnused,
Reference2ObjectMap<Enum<?>,Object> metadataUnused)
Parses a property with given key and value, adding it to the given map.
|
protected Object |
resolve(Enum<?> key,
Reference2ObjectMap<Enum<?>,Object> metadata)
Resolves the given key against the given metadata, falling back to the default metadata.
|
protected Object |
resolve(Enum<?> key,
Reference2ObjectMap<Enum<?>,Object> metadata,
Object o)
Resolves the given key against the given metadata, falling back to the provided object.
|
protected Object |
resolveNotNull(Enum<?> key,
Reference2ObjectMap<Enum<?>,Object> metadata)
Resolves the given key against the given metadata, falling back to the default metadata
and guaranteeing a non-
null result. |
static boolean |
sameKey(Enum<?> enumKey,
String key)
A utility method checking whether the downcased name of an
Enum is equal to a given string. |
ensureFieldIndex, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitcopy, fieldIndex, fieldName, fieldType, getDocument, numberOfFieldsprotected Reference2ObjectMap<Enum<?>,Object> defaultMetadata
parseProperties(Properties).protected PropertyBasedDocumentFactory(Reference2ObjectMap<Enum<?>,Object> defaultMetadata)
protected PropertyBasedDocumentFactory(Properties properties) throws ConfigurationException
ConfigurationExceptionprotected PropertyBasedDocumentFactory(String[] property) throws ConfigurationException
ConfigurationExceptionprotected PropertyBasedDocumentFactory()
public static PropertyBasedDocumentFactory getInstance(Class<?> klass, String[] property) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
public static PropertyBasedDocumentFactory getInstance(Class<?> klass, Properties properties) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
public static PropertyBasedDocumentFactory getInstance(Class<?> klass) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
public static PropertyBasedDocumentFactory getInstance(Class<?> klass, Reference2ObjectMap<Enum<?>,Object> metadata) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
public static boolean sameKey(Enum<?> enumKey, String key)
Enum is equal to a given string.
This class uses an Enum (PropertyBasedDocumentFactory.MetadataKeys) to store valid property keys. We follow
both the uppercase naming convention for enums and the lowercase naming convention for properties,
and this method encapsulates the method calls that necessary to correctly handle key parsing.
protected boolean parseProperty(String key, String[] valuesUnused, Reference2ObjectMap<Enum<?>,Object> metadataUnused) throws ConfigurationException
Currently this implementation just parses the PropertyBasedDocumentFactory.MetadataKeys.LOCALE property.
Subclasses should do their own parsing, returing true in case of success and
returning super.parseProperty() otherwise.
key - the property key.valuesUnused - the property value; this is an array, because properties may have a list of comma-separated values.metadataUnused - the metadata map.ConfigurationExceptionprotected static String ensureJustOne(String key, String[] values) throws ConfigurationException
key - the property name (used to build the exception message).values - the array of values.ConfigurationException - iff values does not contain a single element.public Reference2ObjectMap<Enum<?>,Object> parseProperties(Properties properties) throws ConfigurationException
properties - a set of properties.ConfigurationExceptionpublic Reference2ObjectMap<Enum<?>,Object> parseProperties(String[] property) throws ConfigurationException
property - an array of strings specifying properties.ConfigurationExceptionprotected Object resolve(Enum<?> key, Reference2ObjectMap<Enum<?>,Object> metadata)
key - a key.metadata - a metadata map.metadata for key, or the value
returned by defaultMetadata for key if the former is null (the latter,
of course, might be null).protected Object resolve(Enum<?> key, Reference2ObjectMap<Enum<?>,Object> metadata, Object o)
key - a key.metadata - a metadata map.o - a default object.metadata for key, or o if the
former is null.protected Object resolveNotNull(Enum<?> key, Reference2ObjectMap<Enum<?>,Object> metadata)
null result.key - a key.metadata - a metadata map.metadata for key, or the value
returned by defaultMetadata for key if the former is null; if the
latter is null, too, a NoSuchElementException will be thrown.