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 Charsetprotected charThe field separator.protected StringThe file path.static final Stringprotected intprotected StringThe field that can be used as idprotected booleanThe has known headers.static final StringThe Constant PROPERTY_FIELD_SEPARATOR.static final StringThe Constant PROPERTY_FILE.static final Stringstatic final StringThe Constant PROPERTY_HAS_KNOWN_HEADERS.static final StringThe Constant PROPERTY_HAS_VARARGS_HEADERS.static final Stringstatic final Stringprotected charThe quote char.The rows.protected booleanThe has varargs headers.Fields inherited from class org.eclipse.epsilon.eol.models.CachedModel
allContentsCache, kindCache, PROPERTY_CACHED, PROPERTY_CONCURRENT, typeCacheFields 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 booleandeleteElementInModel(Object instance) Returns true iff the given instance was deleted from the model.protected voidprotected 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 ObjectgetCacheKeyForType(String type) Returns an identity for the given type, which will be used byCachedModelas 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.intgetTypeNameOf(Object instance) Returns a string representing the type of the instance object.booleanhasProperty(String type, String property) Checks if the model elements have this property.booleanbooleanisInstantiable(String type) booleanChecks if is known headers.booleanisLoaded()Convenience method for determining when a model has been loaded.booleanisModelElement(Object instance) Used to test whether a Java object can be contained by this model.booleanChecks if using variable args headers.voidload(StringProperties properties, IRelativePathResolver resolver) protected voidbooleanUsed to test whether an object is contained in this model.voidsetElementId(Object instance, String newId) voidsetFieldSeparator(Character fieldSeparator) Sets the field separator.voidsetIdFieldIndex(int idFieldIndex) voidsetIdFieldName(String idFieldName) voidsetKnownHeaders(boolean knownHeaders) Sets the known headers.voidsetReader(BufferedReader reader) Assign the reader used by the model.voidsetVarargsHeaders(boolean varargsHeaders) Sets the variable args headers.booleanstore()booleanMethods 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, wrapMethods 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, setStoredOnDisposalMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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:IModelReturns 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:IModelUsed 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:IModelUsed 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:
isModelElementin interfaceIModel- Overrides:
isModelElementin 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:
allContentsFromModelin classCachedModel<Map<String,Object>>
-
getAllOfTypeFromModel
protected Collection<Map<String,Object>> getAllOfTypeFromModel(String type) throws EolModelElementTypeNotFoundException - Specified by:
getAllOfTypeFromModelin classCachedModel<Map<String,Object>> - Throws:
EolModelElementTypeNotFoundException
-
getAllOfKindFromModel
protected Collection<Map<String,Object>> getAllOfKindFromModel(String kind) throws EolModelElementTypeNotFoundException - Specified by:
getAllOfKindFromModelin classCachedModel<Map<String,Object>> - Throws:
EolModelElementTypeNotFoundException
-
createInstanceInModel
protected Map<String,Object> createInstanceInModel(String type) throws EolModelElementTypeNotFoundException, EolNotInstantiableModelElementTypeException - Specified by:
createInstanceInModelin classCachedModel<Map<String,Object>> - Throws:
EolModelElementTypeNotFoundExceptionEolNotInstantiableModelElementTypeException
-
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:
loadModelin 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:
EolModelLoadingExceptionException
-
load
public void load(StringProperties properties, IRelativePathResolver resolver) throws EolModelLoadingException - Specified by:
loadin interfaceIModel- Overrides:
loadin classCachedModel<Map<String,Object>> - Throws:
EolModelLoadingException
-
disposeModel
protected void disposeModel()- Specified by:
disposeModelin classCachedModel<Map<String,Object>>
-
isLoaded
public boolean isLoaded()Description copied from class:CachedModelConvenience method for determining when a model has been loaded.- Overrides:
isLoadedin classCachedModel<Map<String,Object>> - Returns:
- Whether the cache has been populated.
-
deleteElementInModel
Description copied from class:CachedModelReturns true iff the given instance was deleted from the model.- Specified by:
deleteElementInModelin classCachedModel<Map<String,Object>> - Throws:
EolRuntimeException
-
getCacheKeyForType
Description copied from class:CachedModelReturns an identity for the given type, which will be used byCachedModelas a key for the memoization of model elements by type.- Specified by:
getCacheKeyForTypein classCachedModel<Map<String,Object>> - Throws:
EolModelElementTypeNotFoundException
-
getAllTypeNamesOf
Description copied from class:CachedModelReturns the fully qualified names of every type to which the given object conforms. The values are used by byCachedModelfor the memoization of model elements by their kind (CachedModel.getAllOfKind(String)).- Specified by:
getAllTypeNamesOfin classCachedModel<Map<String,Object>>
-
setReader
Assign the reader used by the model.- Parameters:
reader-
-