public class CsvContentImporter extends AbstractContentImporter
ContentImporter importing contents from a CSV file.
Each CSV record (line) contains content properties.
Configuration options:
")AbstractContentImporter.ContentReference| Modifier and Type | Field and Description |
|---|---|
protected Charset |
_charset
The file charset.
|
protected boolean |
_columnHeaderLine
True if the supported CSV files have a header formed of the columns (this is not always the case).
|
protected Map<String,String> |
_columnToMetadata
Contains mapping from CSV column to content metadata path.
|
protected ContentTypesHelper |
_cTypeHelper
The content type helper.
|
protected Character |
_delimiterChar
The CSV delimiter character.
|
protected Set<String> |
_matchColumns
Determine if the file is supported by detecting the following columns in the header.
|
protected Pattern |
_matchPattern
Determine if the file is supported by matching the header line against this pattern.
|
protected Character |
_quoteChar
The CSV quote character.
|
_CONTENT_ID_MAP_KEY, _CONTENT_LINK_MAP_KEY, _CONTENT_REPEATER_SIZE_MAP, _contentTypes, _contentWorkflowHelper, _editActionId, _extensions, _initialActionId, _language, _mixins, _priority, _resolver, _workflowName, DEFAULT_PRIORITY| Constructor and Description |
|---|
CsvContentImporter() |
| Modifier and Type | Method and Description |
|---|---|
private org.supercsv.io.ICsvMapReader |
_getMapReader(BufferedReader reader,
org.supercsv.prefs.CsvPreference preference) |
void |
configure(Configuration configuration) |
protected void |
configureCsvProperties(Configuration configuration)
Configure CSV parsing and mapping properties.
|
protected org.supercsv.prefs.CsvPreference |
getCsvPreference(String header,
Map<String,Object> params)
Get the CSV preference.
|
protected Collection<String> |
getDefaultExtensions()
Get the default allowed extensions.
|
protected char |
getDelimiter(String header,
Map<String,Object> params)
Get the CSV character delimiter.
|
protected MetadataDefinition |
getMetadataDefinition(Content content,
MetadataDefinition parentMetaDef,
String name)
Get a metadata definition, either from the parent metadata definition or from the content itself.
|
protected Map<String,String> |
getMetadataFromProperties(Map<String,String> properties)
Get the content metadata from a CSV record.
|
protected char |
getQuoteChar(Map<String,Object> params)
Get the CSV quote character.
|
protected Reader |
getReader(InputStream in)
Get a reader on the data stream, optionally detecting the charset.
|
protected String |
importContent(Map<String,String> properties,
Map<String,Object> params,
int lineNumber)
Import a content from a CSV record.
|
Set<String> |
importContents(InputStream is,
Map<String,Object> params)
Import the contents from the file.
|
protected boolean |
matchColumns(InputStream is)
Test if the importer supports the given file by testing if it contains the configured columns in its header.
|
protected boolean |
matchHeaderPattern(InputStream is)
Test if the importer supports the given file by matching its first line against the configured pattern.
|
void |
service(ServiceManager serviceManager) |
protected void |
setMetadata(ModifiableCompositeMetadata meta,
MetadataDefinition metaDef,
String name,
String value,
Map<String,Object> params)
Set a single metadata.
|
protected void |
setMetadata(ModifiableContent content,
String path,
String value,
Map<String,Object> params)
Set a metadata from its string value.
|
protected void |
setMetadatas(ModifiableContent content,
Map<String,String> metaValues,
Map<String,Object> params)
Set the content metadatas from the CSV values.
|
protected void |
setRichText(ModifiableCompositeMetadata meta,
String name,
String value)
Set a RichText metadata from a String value.
|
boolean |
supports(InputStream is,
String name)
Tests if the
ContentImporter is able to import the corresponding stream.Implementing classes should first test the file extension, if possible, and if the name is provided. |
addContentReference, addContentReference, addContentReferences, configureContentCreation, configureExtensions, configureWorkflow, createContent, createContent, createContent, createContent, createContent, getContentFromProperties, getContentIdMap, getContentRefMap, getContentRepeaterSizeMap, getContentTypes, getEditActionId, getInitialActionId, getLanguage, getMixins, getPriority, getReferencedContentId, getWorkflowName, isExtensionValid, parseDate, parseDate, restoreContentReferences, setBinaryMetadata, setBooleanMetadata, setDateMetadata, setDoubleMetadata, setGeocodeMetadata, setGeocodeMetadata, setLongMetadata, setReferenceMetadatas, setRepeaterSize, setRichText, setStringMetadataenableLogging, getLogger, setupLogger, setupLogger, setupLoggerprotected ContentTypesHelper _cTypeHelper
protected Character _delimiterChar
protected Character _quoteChar
protected boolean _columnHeaderLine
protected Pattern _matchPattern
protected Set<String> _matchColumns
protected Map<String,String> _columnToMetadata
public CsvContentImporter()
public void service(ServiceManager serviceManager) throws ServiceException
service in interface Serviceableservice in class AbstractContentImporterServiceExceptionpublic void configure(Configuration configuration) throws ConfigurationException
configure in interface Configurableconfigure in class AbstractContentImporterConfigurationExceptionprotected void configureCsvProperties(Configuration configuration) throws ConfigurationException
configuration - the CSV configuration.ConfigurationException - if an error occurs.protected Collection<String> getDefaultExtensions()
AbstractContentImportergetDefaultExtensions in class AbstractContentImporterpublic boolean supports(InputStream is, String name) throws IOException
ContentImporterContentImporter is able to import the corresponding stream.is - an input stream on the file, cannot be null. The stream is closed by the caller.name - the file name, can be null.ContentImporter can import the corresponding stream, false otherwise.IOException - if an error occurs.protected boolean matchHeaderPattern(InputStream is) throws IOException
is - an input stream on the data to test.IOException - if a read error occurs.protected boolean matchColumns(InputStream is) throws IOException
is - an input stream on the data to test.IOException - if a read error occurs.private org.supercsv.io.ICsvMapReader _getMapReader(BufferedReader reader, org.supercsv.prefs.CsvPreference preference) throws IOException
IOExceptionpublic Set<String> importContents(InputStream is, Map<String,Object> params) throws IOException
ContentImporteris - an input stream on the file, cannot be null. The stream is closed by the caller.params - input or output parameters.IOException - if an error occurs importing the contents.protected Reader getReader(InputStream in)
in - the data stream.protected org.supercsv.prefs.CsvPreference getCsvPreference(String header, Map<String,Object> params)
header - the CSV first line.params - the import parameters.CsvPreference object.protected char getDelimiter(String header, Map<String,Object> params)
header - the CSV first line.params - the import parameters.protected char getQuoteChar(Map<String,Object> params)
params - the import parameters.protected String importContent(Map<String,String> properties, Map<String,Object> params, int lineNumber)
properties - the CSV record as a Map of values, indexed by column name or number.params - the import parameters.lineNumber - the line number of the record being imported, for logging purposes.protected Map<String,String> getMetadataFromProperties(Map<String,String> properties)
properties - the CSV record as a Map of values, indexed by column name or number.protected void setMetadatas(ModifiableContent content, Map<String,String> metaValues, Map<String,Object> params)
content - the content to populate.metaValues - the metadata values, extracted from the CSV record.params - the import parameters.protected void setMetadata(ModifiableContent content, String path, String value, Map<String,Object> params)
content - the content to populate.path - the metadata path.value - the metadata string value.params - the import parameters.protected MetadataDefinition getMetadataDefinition(Content content, MetadataDefinition parentMetaDef, String name)
content - the imported content.parentMetaDef - the parent metadata definition.name - the metadata name.protected void setMetadata(ModifiableCompositeMetadata meta, MetadataDefinition metaDef, String name, String value, Map<String,Object> params) throws IOException
meta - the metadata holder.metaDef - the metadata definition.name - the metadata name.value - the metadata value as a String.params - the import parameters.IOException - if an error occurs.protected void setRichText(ModifiableCompositeMetadata meta, String name, String value) throws IOException
meta - the metadata holder.name - the metadata name.value - the String value.IOException - if something goes wrong when manipulating files