Package org.eclipse.epsilon.emc.csv
Class CsvModel
- All Implemented Interfaces:
AutoCloseable
,IModel
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 <a href="Regex>http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum) - PROPERTY_HAS_KNOWN_HEADERS, "There maybe an optional header line appearing as the first line of the file with the same format as normal record lines. This header will contain names corresponding to the fields in the file and should contain the same number of fields as the records in the rest of the file". If this property is set to true, model loading will verify that all lines have the same number of fields as the header. When accessing the model elements (rows) each of the header fields can be used to access information on each record.
- 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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.eclipse.epsilon.eol.models.IModel
IModel.AmbiguityCheckResult
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Charset
protected char
The field separator.protected String
The file path.static final String
protected int
protected String
The field that can be used as idprotected boolean
The has known headers.static final String
The Constant PROPERTY_FIELD_SEPARATOR.static final String
The Constant PROPERTY_FILE.static final String
static final String
The Constant PROPERTY_HAS_KNOWN_HEADERS.static final String
The Constant PROPERTY_HAS_VARARGS_HEADERS.static final String
static final String
protected char
The quote char.The rows.protected boolean
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 -
Method Summary
Modifier and TypeMethodDescriptionprotected Collection<Map<String,
Object>> createInstanceInModel
(String type) createRows
(BufferedReader reader, boolean knownHeaders, char fieldSeparator, boolean varargsHeaders) protected boolean
deleteElementInModel
(Object instance) Returns true iff the given instance was deleted from the model.protected void
protected Collection<Map<String,
Object>> getAllOfKindFromModel
(String kind) protected Collection<Map<String,
Object>> getAllOfTypeFromModel
(String type) protected Collection<String>
getAllTypeNamesOf
(Object instance) Returns the fully qualified names of every type to which the given object conforms.protected Object
getCacheKeyForType
(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.getElementById
(String id) getElementId
(Object instance) getEnumerationValue
(String enumeration, String label) Gets the field separator.int
getTypeNameOf
(Object instance) Returns a string representing the type of the instance object.boolean
hasProperty
(String type, String property) Checks if the model elements have this property.boolean
boolean
isInstantiable
(String type) boolean
Checks if is known headers.boolean
isLoaded()
Convenience method for determining when a model has been loaded.boolean
isModelElement
(Object instance) Used to test whether a Java object can be contained by this model.boolean
Checks if using variable args headers.void
load
(StringProperties properties, IRelativePathResolver resolver) protected void
boolean
Used to test whether an object is contained in this model.void
setElementId
(Object instance, String newId) void
setFieldSeparator
(Character fieldSeparator) Sets the field separator.void
setIdFieldIndex
(int idFieldIndex) void
setIdFieldName
(String idFieldName) void
setKnownHeaders
(boolean knownHeaders) Sets the known headers.void
setReader
(BufferedReader reader) Assign the reader used by the model.void
setVarargsHeaders
(boolean varargsHeaders) Sets the variable args headers.boolean
store()
boolean
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
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.eclipse.epsilon.eol.models.IModel
checkAmbiguity, close
-
Field Details
-
HEADERLESS_FIELD_NAME
- See Also:
-
PROPERTY_FILE
The Constant PROPERTY_FILE.- See Also:
-
PROPERTY_FIELD_SEPARATOR
The Constant PROPERTY_FIELD_SEPARATOR.- See Also:
-
PROPERTY_HAS_KNOWN_HEADERS
The Constant PROPERTY_HAS_KNOWN_HEADERS.- See Also:
-
PROPERTY_HAS_VARARGS_HEADERS
The Constant PROPERTY_HAS_VARARGS_HEADERS.- See Also:
-
PROPERTY_FILE_ENCODING
- See Also:
-
PROPERTY_QUOTE_CHARACTER
- See Also:
-
PROPERTY_ID_FIELD
- See Also:
-
fieldSeparator
protected char fieldSeparatorThe field separator. -
quoteChar
protected char quoteCharThe quote char. -
knownHeaders
protected boolean knownHeadersThe has known headers. -
varargsHeaders
protected boolean varargsHeadersThe has varargs headers. -
idFieldName
The field that can be used as id -
idFieldIndex
protected int idFieldIndex -
file
The file path. -
rows
The rows. -
cs
-
-
Constructor Details
-
CsvModel
public CsvModel()
-
-
Method Details
-
getFieldSeparator
Gets the field separator.- Returns:
- the field separator
-
setFieldSeparator
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
- Returns:
- the id Field Name
-
setIdFieldName
- 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 Object getEnumerationValue(String enumeration, String label) throws EolEnumerationValueNotFoundException -
getTypeOf
-
getTypeNameOf
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
-
getElementId
-
setElementId
-
owns
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
-
isModelElement
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
-
store
-
store
public boolean store() -
allContentsFromModel
- Specified by:
allContentsFromModel
in classCachedModel<Map<String,
Object>>
-
getAllOfTypeFromModel
protected Collection<Map<String,Object>> getAllOfTypeFromModel(String type) throws EolModelElementTypeNotFoundException - Specified by:
getAllOfTypeFromModel
in classCachedModel<Map<String,
Object>> - Throws:
EolModelElementTypeNotFoundException
-
getAllOfKindFromModel
protected Collection<Map<String,Object>> getAllOfKindFromModel(String kind) throws EolModelElementTypeNotFoundException - Specified by:
getAllOfKindFromModel
in classCachedModel<Map<String,
Object>> - Throws:
EolModelElementTypeNotFoundException
-
createInstanceInModel
protected Map<String,Object> createInstanceInModel(String type) throws EolModelElementTypeNotFoundException, EolNotInstantiableModelElementTypeException - Specified by:
createInstanceInModel
in classCachedModel<Map<String,
Object>> - Throws:
EolModelElementTypeNotFoundException
EolNotInstantiableModelElementTypeException
-
hasProperty
public boolean hasProperty(String type, 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
- Specified by:
loadModel
in classCachedModel<Map<String,
Object>> - Throws:
EolModelLoadingException
-
createRows
protected static List<Map<String,Object>> createRows(BufferedReader reader, boolean knownHeaders, char fieldSeparator, boolean varargsHeaders) throws Exception - Parameters:
rows
-reader
-varargsHeaders
-fieldSeparator
-knownHeaders
-- Throws:
EolModelLoadingException
Exception
-
load
public void load(StringProperties properties, IRelativePathResolver resolver) throws EolModelLoadingException - Specified by:
load
in interfaceIModel
- Overrides:
load
in classCachedModel<Map<String,
Object>> - Throws:
EolModelLoadingException
-
disposeModel
protected void disposeModel()- Specified by:
disposeModel
in classCachedModel<Map<String,
Object>>
-
isLoaded
public boolean isLoaded()Description copied from class:CachedModel
Convenience method for determining when a model has been loaded.- Overrides:
isLoaded
in classCachedModel<Map<String,
Object>> - Returns:
- Whether the cache has been populated.
-
deleteElementInModel
Description copied from class:CachedModel
Returns true iff the given instance was deleted from the model.- Specified by:
deleteElementInModel
in classCachedModel<Map<String,
Object>> - Throws:
EolRuntimeException
-
getCacheKeyForType
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<Map<String,
Object>> - Throws:
EolModelElementTypeNotFoundException
-
getAllTypeNamesOf
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<Map<String,
Object>>
-
setReader
Assign the reader used by the model.- Parameters:
reader
-
-