Package org.eclipse.epsilon.emc.csv
Class CsvModel
- java.lang.Object
-
- org.eclipse.epsilon.eol.models.Model
-
- org.eclipse.epsilon.eol.models.CachedModel<java.util.Map<java.lang.String,java.lang.Object>>
-
- org.eclipse.epsilon.emc.csv.CsvModel
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,IModel
public class CsvModel extends CachedModel<java.util.Map<java.lang.String,java.lang.Object>>
The Class CsvModel provides The Epsilon Model Connectivity Layer for CSV files (http://tools.ietf.org/html/rfc4180), with the difference that it supports files in which each line has a different number of fields.Three properties allow configuration of how to read the CSV file:
- PROPERTY_FIELD_SEPARATOR, by default a comma (','), but you can specify
a different separator. In particular, fields are identified using
String.split(String)
, and as such, PROPERTY_FIELD_SEPARATOR is actually treated as regular expression (see PROPERTY_HAS_VARARGS_HEADERS, allows the use of varargs headers. This is a special type of header in which the last filed in the header line is used as a base name for all fields after this one. This allows for lines to have a variable number of fields. During model loading it will be verified that each line has at least the same number of fields as header fields minus one. Fields in a record including and following the last header filed will be assigned the header lastHeader0, lastHeader1, lastHeader2,... and so on an so forth.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.charset.Charset
cs
protected char
fieldSeparator
The field separator.protected java.lang.String
file
The file path.static java.lang.String
HEADERLESS_FIELD_NAME
protected int
idFieldIndex
protected java.lang.String
idFieldName
The field that can be used as idprotected boolean
knownHeaders
The has known headers.static java.lang.String
PROPERTY_FIELD_SEPARATOR
The Constant PROPERTY_FIELD_SEPARATOR.static java.lang.String
PROPERTY_FILE
The Constant PROPERTY_FILE.static java.lang.String
PROPERTY_FILE_ENCODING
static java.lang.String
PROPERTY_HAS_KNOWN_HEADERS
The Constant PROPERTY_HAS_KNOWN_HEADERS.static java.lang.String
PROPERTY_HAS_VARARGS_HEADERS
The Constant PROPERTY_HAS_VARARGS_HEADERS.static java.lang.String
PROPERTY_ID_FIELD
static java.lang.String
PROPERTY_QUOTE_CHARACTER
protected char
quoteChar
The quote char.protected java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
rows
The rows.protected boolean
varargsHeaders
The has varargs headers.-
Fields inherited from class org.eclipse.epsilon.eol.models.CachedModel
allContentsCache, kindCache, PROPERTY_CACHED, PROPERTY_CONCURRENT, typeCache
-
Fields inherited from class org.eclipse.epsilon.eol.models.Model
aliases, ENV_PREFIX, name, PROPERTY_ALIASES, PROPERTY_NAME, PROPERTY_READONLOAD, PROPERTY_READONLY, PROPERTY_STOREONDISPOSAL, propertyGetter, propertySetter, readOnLoad, storeOnDisposal
-
-
Constructor Summary
Constructors Constructor Description CsvModel()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.Collection<java.util.Map<java.lang.String,java.lang.Object>>
allContentsFromModel()
protected java.util.Map<java.lang.String,java.lang.Object>
createInstanceInModel(java.lang.String type)
protected static java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
createRows(java.io.BufferedReader reader, boolean knownHeaders, char fieldSeparator, boolean varargsHeaders)
protected boolean
deleteElementInModel(java.lang.Object instance)
Returns true iff the given instance was deleted from the model.protected void
disposeModel()
protected java.util.Collection<java.util.Map<java.lang.String,java.lang.Object>>
getAllOfKindFromModel(java.lang.String kind)
protected java.util.Collection<java.util.Map<java.lang.String,java.lang.Object>>
getAllOfTypeFromModel(java.lang.String type)
protected java.util.Collection<java.lang.String>
getAllTypeNamesOf(java.lang.Object instance)
Returns the fully qualified names of every type to which the given object conforms.protected java.lang.Object
getCacheKeyForType(java.lang.String type)
Returns an identity for the given type, which will be used byCachedModel
as a key for the memoization of model elements by type.java.lang.Object
getElementById(java.lang.String id)
java.lang.String
getElementId(java.lang.Object instance)
java.lang.Object
getEnumerationValue(java.lang.String enumeration, java.lang.String label)
java.lang.Character
getFieldSeparator()
Gets the field separator.int
getIdFieldIndex()
java.lang.String
getIdFieldName()
java.lang.String
getTypeNameOf(java.lang.Object instance)
Returns a string representing the type of the instance object.java.lang.Object
getTypeOf(java.lang.Object instance)
boolean
hasProperty(java.lang.String type, java.lang.String property)
Checks if the model elements have this property.boolean
hasType(java.lang.String type)
boolean
isInstantiable(java.lang.String type)
boolean
isKnownHeaders()
Checks if is known headers.boolean
isLoaded()
Convenience method for determining when a model has been loaded.boolean
isModelElement(java.lang.Object instance)
Used to test whether a Java object can be contained by this model.boolean
isVarargsHeaders()
Checks if using variable args headers.void
load(StringProperties properties, IRelativePathResolver resolver)
protected void
loadModel()
boolean
owns(java.lang.Object instance)
Used to test whether an object is contained in this model.void
setElementId(java.lang.Object instance, java.lang.String newId)
void
setFieldSeparator(java.lang.Character fieldSeparator)
Sets the field separator.void
setIdFieldIndex(int idFieldIndex)
void
setIdFieldName(java.lang.String idFieldName)
void
setKnownHeaders(boolean knownHeaders)
Sets the known headers.void
setReader(java.io.BufferedReader reader)
Assign the reader used by the model.void
setVarargsHeaders(boolean varargsHeaders)
Sets the variable args headers.boolean
store()
boolean
store(java.lang.String location)
-
Methods inherited from class org.eclipse.epsilon.eol.models.CachedModel
addToCache, allContents, clearCache, createInstance, deleteElement, dispose, getAllOfKind, getAllOfKindOrType, getAllOfType, initCaches, isCachingEnabled, isConcurrent, load, removeFromCache, setCachingEnabled, setConcurrent, wrap
-
Methods inherited from class org.eclipse.epsilon.eol.models.Model
allInstances, createInstance, getAliases, getFullyQualifiedTypeNameOf, getMetamodel, getName, getPropertyGetter, getPropertySetter, getTransactionSupport, isOfKind, isOfType, isPropertySet, isReadOnLoad, isStoredOnDisposal, knowsAboutProperty, load, load, setName, setReadOnLoad, setStoredOnDisposal
-
-
-
-
Field Detail
-
HEADERLESS_FIELD_NAME
public static final java.lang.String HEADERLESS_FIELD_NAME
- See Also:
- Constant Field Values
-
PROPERTY_FILE
public static final java.lang.String PROPERTY_FILE
The Constant PROPERTY_FILE.- See Also:
- Constant Field Values
-
PROPERTY_FIELD_SEPARATOR
public static final java.lang.String PROPERTY_FIELD_SEPARATOR
The Constant PROPERTY_FIELD_SEPARATOR.- See Also:
- Constant Field Values
-
PROPERTY_HAS_KNOWN_HEADERS
public static final java.lang.String PROPERTY_HAS_KNOWN_HEADERS
The Constant PROPERTY_HAS_KNOWN_HEADERS.- See Also:
- Constant Field Values
-
PROPERTY_HAS_VARARGS_HEADERS
public static final java.lang.String PROPERTY_HAS_VARARGS_HEADERS
The Constant PROPERTY_HAS_VARARGS_HEADERS.- See Also:
- Constant Field Values
-
PROPERTY_FILE_ENCODING
public static final java.lang.String PROPERTY_FILE_ENCODING
- See Also:
- Constant Field Values
-
PROPERTY_QUOTE_CHARACTER
public static final java.lang.String PROPERTY_QUOTE_CHARACTER
- See Also:
- Constant Field Values
-
PROPERTY_ID_FIELD
public static final java.lang.String PROPERTY_ID_FIELD
- See Also:
- Constant Field Values
-
fieldSeparator
protected char fieldSeparator
The field separator.
-
quoteChar
protected char quoteChar
The quote char.
-
knownHeaders
protected boolean knownHeaders
The has known headers.
-
varargsHeaders
protected boolean varargsHeaders
The has varargs headers.
-
idFieldName
protected java.lang.String idFieldName
The field that can be used as id
-
idFieldIndex
protected int idFieldIndex
-
file
protected java.lang.String file
The file path.
-
rows
protected java.util.List<java.util.Map<java.lang.String,java.lang.Object>> rows
The rows.
-
cs
protected java.nio.charset.Charset cs
-
-
Method Detail
-
getFieldSeparator
public java.lang.Character getFieldSeparator()
Gets the field separator.- Returns:
- the field separator
-
setFieldSeparator
public void setFieldSeparator(java.lang.Character fieldSeparator)
Sets the field separator.- Parameters:
fieldSeparator
- the new field separator
-
isKnownHeaders
public boolean isKnownHeaders()
Checks if is known headers.- Returns:
- true, if is known headers
-
setKnownHeaders
public void setKnownHeaders(boolean knownHeaders)
Sets the known headers.- Parameters:
knownHeaders
- the new known headers
-
isVarargsHeaders
public boolean isVarargsHeaders()
Checks if using variable args headers.- Returns:
- true, if using variable args headers
-
setVarargsHeaders
public void setVarargsHeaders(boolean varargsHeaders)
Sets the variable args headers.- Parameters:
varargsHeaders
- true if using variable args headers.
-
getIdFieldName
public java.lang.String getIdFieldName()
- Returns:
- the id Field Name
-
setIdFieldName
public void setIdFieldName(java.lang.String idFieldName)
- Parameters:
idFieldName
- the name of the field to use as id
-
getIdFieldIndex
public int getIdFieldIndex()
- Returns:
- the id Field Index
-
setIdFieldIndex
public void setIdFieldIndex(int idFieldIndex)
- Parameters:
idFieldIndex
- the index of the field to use as id
-
getEnumerationValue
public java.lang.Object getEnumerationValue(java.lang.String enumeration, java.lang.String label) throws EolEnumerationValueNotFoundException
-
getTypeOf
public java.lang.Object getTypeOf(java.lang.Object instance)
-
getTypeNameOf
public java.lang.String getTypeNameOf(java.lang.Object instance)
Description copied from interface:IModel
Returns a string representing the type of the instance object. The value returned by this function can be passed to createInstance to instantiate another object of the same type as instance.- Parameters:
instance
- The model object whose type is to be determined.- Returns:
- the name of the type of the model object, instance.
-
getElementById
public java.lang.Object getElementById(java.lang.String id)
-
getElementId
public java.lang.String getElementId(java.lang.Object instance)
-
setElementId
public void setElementId(java.lang.Object instance, java.lang.String newId)
-
owns
public boolean owns(java.lang.Object instance)
Description copied from interface:IModel
Used to test whether an object is contained in this model.- Parameters:
instance
- the Java object to test.- Returns:
- true if and only if instance is contained by this model.
-
isInstantiable
public boolean isInstantiable(java.lang.String type)
-
isModelElement
public boolean isModelElement(java.lang.Object instance)
Description copied from interface:IModel
Used to test whether a Java object can be contained by this model. For example, EMF models can contain instances of EObject, but not instances of just Object.- Specified by:
isModelElement
in interfaceIModel
- Overrides:
isModelElement
in classModel
- Parameters:
instance
- the Java object to test.- Returns:
- true if and only if instance can be contained by this model.
-
hasType
public boolean hasType(java.lang.String type)
-
store
public boolean store(java.lang.String location)
-
store
public boolean store()
-
allContentsFromModel
protected java.util.Collection<java.util.Map<java.lang.String,java.lang.Object>> allContentsFromModel()
- Specified by:
allContentsFromModel
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
-
getAllOfTypeFromModel
protected java.util.Collection<java.util.Map<java.lang.String,java.lang.Object>> getAllOfTypeFromModel(java.lang.String type) throws EolModelElementTypeNotFoundException
- Specified by:
getAllOfTypeFromModel
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Throws:
EolModelElementTypeNotFoundException
-
getAllOfKindFromModel
protected java.util.Collection<java.util.Map<java.lang.String,java.lang.Object>> getAllOfKindFromModel(java.lang.String kind) throws EolModelElementTypeNotFoundException
- Specified by:
getAllOfKindFromModel
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Throws:
EolModelElementTypeNotFoundException
-
createInstanceInModel
protected java.util.Map<java.lang.String,java.lang.Object> createInstanceInModel(java.lang.String type) throws EolModelElementTypeNotFoundException, EolNotInstantiableModelElementTypeException
- Specified by:
createInstanceInModel
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Throws:
EolModelElementTypeNotFoundException
EolNotInstantiableModelElementTypeException
-
hasProperty
public boolean hasProperty(java.lang.String type, java.lang.String property) throws EolModelElementTypeNotFoundException
Checks if the model elements have this property.- Parameters:
type
- the type (should always be "Row" (seehasType(String)
)property
- the property- Returns:
- true, if successful
- Throws:
EolModelElementTypeNotFoundException
- the eol model element type not found exception
-
loadModel
protected void loadModel() throws EolModelLoadingException
- Specified by:
loadModel
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Throws:
EolModelLoadingException
-
createRows
protected static java.util.List<java.util.Map<java.lang.String,java.lang.Object>> createRows(java.io.BufferedReader reader, boolean knownHeaders, char fieldSeparator, boolean varargsHeaders) throws java.lang.Exception
- Parameters:
rows
-reader
-varargsHeaders
-fieldSeparator
-knownHeaders
-- Throws:
EolModelLoadingException
java.lang.Exception
-
load
public void load(StringProperties properties, IRelativePathResolver resolver) throws EolModelLoadingException
- Specified by:
load
in interfaceIModel
- Overrides:
load
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Throws:
EolModelLoadingException
-
disposeModel
protected void disposeModel()
- Specified by:
disposeModel
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
-
isLoaded
public boolean isLoaded()
Description copied from class:CachedModel
Convenience method for determining when a model has been loaded.- Overrides:
isLoaded
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Returns:
- Whether the cache has been populated.
-
deleteElementInModel
protected boolean deleteElementInModel(java.lang.Object instance) throws EolRuntimeException
Description copied from class:CachedModel
Returns true iff the given instance was deleted from the model.- Specified by:
deleteElementInModel
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Throws:
EolRuntimeException
-
getCacheKeyForType
protected java.lang.Object getCacheKeyForType(java.lang.String type) throws EolModelElementTypeNotFoundException
Description copied from class:CachedModel
Returns an identity for the given type, which will be used byCachedModel
as a key for the memoization of model elements by type.- Specified by:
getCacheKeyForType
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
- Throws:
EolModelElementTypeNotFoundException
-
getAllTypeNamesOf
protected java.util.Collection<java.lang.String> getAllTypeNamesOf(java.lang.Object instance)
Description copied from class:CachedModel
Returns the fully qualified names of every type to which the given object conforms. The values are used by byCachedModel
for the memoization of model elements by their kind (CachedModel.getAllOfKind(String)
).- Specified by:
getAllTypeNamesOf
in classCachedModel<java.util.Map<java.lang.String,java.lang.Object>>
-
setReader
public void setReader(java.io.BufferedReader reader)
Assign the reader used by the model.- Parameters:
reader
-
-
-