Java Code Examples for com.csvreader.CsvReader#getValues()

The following examples show how to use com.csvreader.CsvReader#getValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnstructuredStorageReaderUtil.java    From DataLink with Apache License 2.0 6 votes vote down vote up
/**
 * @param inputLine
 *            输入待分隔字符串
 * @param delimiter
 *            字符串分割符
 * @return 分隔符分隔后的字符串数组,出现异常时返回为null 支持转义,即数据中可包含分隔符
 * */
public static String[] splitOneLine(String inputLine, char delimiter) {
	String[] splitedResult = null;
	if (null != inputLine) {
		try {
			CsvReader csvReader = new CsvReader(new StringReader(inputLine));
			csvReader.setDelimiter(delimiter);
			if (csvReader.readRecord()) {
				splitedResult = csvReader.getValues();
			}
		} catch (IOException e) {
			// nothing to do
		}
	}
	return splitedResult;
}
 
Example 2
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<OrganisationUnitGroup> orgUnitGroupsFromCsv( CsvReader reader )
    throws IOException
{
    List<OrganisationUnitGroup> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            OrganisationUnitGroup object = new OrganisationUnitGroup();
            setIdentifiableObject( object, values );
            object.setAutoFields();
            object.setShortName( getSafe( values, 3, object.getName(), 50 ) );
            list.add( object );
        }
    }

    return list;
}
 
Example 3
Source File: Utils.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
public static MLDouble readMDoubleFromCSVFile(Path inFilePath, String mName, int nrows, int ncols, char delimiter) throws NumberFormatException, IOException {
    MLDouble mlDouble = new MLDouble(mName, new int[] {nrows, ncols});
    CsvReader cvsReader = new CsvReader(inFilePath.toString());
    cvsReader.setDelimiter(delimiter);
    int i = 0;
    while (cvsReader.readRecord()) {
        String[] rows = cvsReader.getValues();
        int j = 0;
        for (String col : rows) {
            mlDouble.set(new Double(col), i, j);
            j++;
        }
        i++;
    }
    return mlDouble;
}
 
Example 4
Source File: Utils.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
public static double[][] readDoubleMatrixFromCSVFile(Path inFilePath, int nrows, int ncols, char delimiter) throws NumberFormatException, IOException {
    double[][] mlDouble = new double[nrows][ncols];
    CsvReader cvsReader = new CsvReader(inFilePath.toString());
    cvsReader.setDelimiter(delimiter);
    int i = 0;
    while (cvsReader.readRecord()) {
        String[] rows = cvsReader.getValues();
        int j = 0;
        for (String col : rows) {
            mlDouble[i][j] = Double.parseDouble(col);
            j++;
            if (j >= ncols) {
                break;
            }
        }
        i++;
        if (i >= nrows) {
            break;
        }

    }
    return mlDouble;
}
 
Example 5
Source File: Utils.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
public static double[] readDoubleArrayFromCSVFile(Path inFilePath,  int nrows, char delimiter) throws NumberFormatException, IOException {
    double[] mlDouble = new double[nrows];
    CsvReader cvsReader = new CsvReader(inFilePath.toString());
    cvsReader.setDelimiter(delimiter);
    int i = 0;
    while (cvsReader.readRecord()) {
        String[] rows = cvsReader.getValues();
        for (String col : rows) {
            mlDouble[i] = Double.parseDouble(col);
        }
        i++;
        if (i >= nrows) {
            break;
        }
    }
    return mlDouble;
}
 
Example 6
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<DataElementGroup> dataElementGroupsFromCsv( CsvReader reader )
    throws IOException
{
    List<DataElementGroup> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            DataElementGroup object = new DataElementGroup();
            setIdentifiableObject( object, values );
            object.setShortName( getSafe( values, 3, object.getName(), 50 ) );
            object.setAutoFields();
            list.add( object );
        }
    }

    return list;
}
 
Example 7
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<CategoryOptionGroup> categoryOptionGroupsFromCsv( CsvReader reader )
    throws IOException
{
    List<CategoryOptionGroup> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            CategoryOptionGroup object = new CategoryOptionGroup();
            setIdentifiableObject( object, values );
            object.setShortName( getSafe( values, 3, object.getName(), 50 ) );
            list.add( object );
        }
    }

    return list;
}
 
Example 8
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<CategoryCombo> categoryCombosFromCsv( CsvReader reader )
    throws IOException
{
    List<CategoryCombo> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            CategoryCombo object = new CategoryCombo();
            setIdentifiableObject( object, values );
            object.setDataDimensionType( DataDimensionType.valueOf( getSafe( values, 3, DataDimensionType.DISAGGREGATION.toString(), 40 ) ) );
            object.setSkipTotal( Boolean.valueOf( getSafe( values, 4, Boolean.FALSE.toString(), 40 ) ) );
            list.add( object );
        }
    }

    return list;
}
 
Example 9
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<CategoryOption> categoryOptionsFromCsv( CsvReader reader )
    throws IOException
{
    List<CategoryOption> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            CategoryOption object = new CategoryOption();
            setIdentifiableObject( object, values );
            object.setShortName( getSafe( values, 3, object.getName(), 50 ) );
            list.add( object );
        }
    }

    return list;
}
 
Example 10
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<Category> categoriesFromCsv( CsvReader reader )
    throws IOException
{
    List<Category> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            Category object = new Category();
            setIdentifiableObject( object, values );
            object.setDescription( getSafe( values, 3, null, 255 ) );
            object.setDataDimensionType( DataDimensionType.valueOf( getSafe( values, 4, DataDimensionType.DISAGGREGATION.toString(), 40 ) ) );
            object.setDataDimension( Boolean.valueOf( getSafe( values, 5, Boolean.FALSE.toString(), 40 ) ) );
            list.add( object );
        }
    }

    return list;
}
 
Example 11
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<OrganisationUnitGroup> orgUnitGroupMembersFromCsv( CsvReader reader )
    throws IOException
{
    CachingMap<String, OrganisationUnitGroup> uidMap = new CachingMap<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            String groupUid = values[0];
            String memberUid = values[1];

            OrganisationUnitGroup persistedGroup = organisationUnitGroupService.getOrganisationUnitGroup( groupUid );

            if ( persistedGroup != null )
            {

                OrganisationUnitGroup group = uidMap.get( groupUid, () -> {
                    OrganisationUnitGroup nonPersistedGroup = new OrganisationUnitGroup();

                    nonPersistedGroup.setUid( persistedGroup.getUid() );
                    nonPersistedGroup.setName( persistedGroup.getName() );

                    return nonPersistedGroup;
                } );

                OrganisationUnit member = new OrganisationUnit();
                member.setUid( memberUid );
                group.addOrganisationUnit( member );
            }
        }
    }

    return new ArrayList<>( uidMap.values() );
}
 
Example 12
Source File: FileSpec.java    From bdt with Apache License 2.0 5 votes vote down vote up
/**
 * Read csv file and store result in list of maps
 *
 * @param csvFile
 */
@When("^I read info from csv file '(.+?)' with separator '(.+?)'$")
public void readFromCSV(String csvFile, String separator) throws Exception {
    //By default separator is a coma
    char sep = ',';
    if (separator.length() > 1) {
        switch (separator) {
            case "\\t":
                sep = '\t';
                break;
            default:
                sep = ',';
                break;
        }
    } else {
        sep = separator.charAt(0);
    }

    CsvReader rows = new CsvReader(csvFile, sep);

    String[] columns = null;
    if (rows.readRecord()) {
        columns = rows.getValues();
        rows.setHeaders(columns);
    }

    List<Map<String, String>> results = new ArrayList<Map<String, String>>();
    while (rows.readRecord()) {
        Map<String, String> row = new HashMap<String, String>();
        for (String column : columns) {
            row.put(column, rows.get(rows.getIndex(column)));
        }
        results.add(row);
    }

    rows.close();

    commonspec.setResultsType("csv");
    commonspec.setCSVResults(results);
}
 
Example 13
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<IndicatorGroup> indicatorGroupMembersFromCsv( CsvReader reader )
    throws IOException
{
    CachingMap<String, IndicatorGroup> uidMap = new CachingMap<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            String groupUid = values[0];
            String memberUid = values[1];

            IndicatorGroup persistedGroup = indicatorGroupService.getIndicatorGroupByUid( groupUid );

            if ( persistedGroup != null )
            {

                IndicatorGroup group = uidMap.get( groupUid, () -> {
                    IndicatorGroup nonPersistedGroup = new IndicatorGroup();
                    nonPersistedGroup.setUid( persistedGroup.getUid() );
                    nonPersistedGroup.setName( persistedGroup.getName() );
                    return nonPersistedGroup;
                } );

                Indicator member = new Indicator();
                member.setUid( memberUid );
                group.addIndicator( member );
            }
        }
    }
    return new ArrayList<>( uidMap.values() );
}
 
Example 14
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<DataElementGroup> dataElementGroupMembersFromCsv( CsvReader reader )
    throws IOException
{
    CachingMap<String, DataElementGroup> uidMap = new CachingMap<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            String groupUid = values[0];
            String memberUid = values[1];

            DataElementGroup persistedGroup = dataElementGroupService.getDataElementGroupByUid( groupUid );

            if ( persistedGroup != null )
            {
                DataElementGroup group = uidMap.get( groupUid, () -> {
                    DataElementGroup nonPersistedGroup = new DataElementGroup();
                    nonPersistedGroup.setUid( persistedGroup.getUid() );
                    nonPersistedGroup.setName( persistedGroup.getName() );
                    return nonPersistedGroup;
                } );

                DataElement member = new DataElement();
                member.setUid( memberUid );
                group.addDataElement( member );
            }
        }
    }

    return new ArrayList<>( uidMap.values() );
}
 
Example 15
Source File: Utils.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
public static List readHistoDataFromCsv(Path inFilePath, char delimiter) throws NumberFormatException, IOException {
    ArrayList retList = new ArrayList();

    int numberOfCSVLines = countLines(inFilePath);
    numberOfCSVLines = numberOfCSVLines - 1; //take into account header

    CsvReader cvsReader = new CsvReader(inFilePath.toString());
    cvsReader.setDelimiter(delimiter);
    int i = 0;
    cvsReader.readHeaders();


    String[] headers = cvsReader.getHeaders();
    retList.add(headers);

    double[][] mlDouble = new double[numberOfCSVLines][headers.length];

    while (cvsReader.readRecord()) {
        String[] rows = cvsReader.getValues();
        int j = 0;
        for (String col : rows) {
            if (j > 1) {

                mlDouble[i][j - 2] = (col != null) ? Double.parseDouble(col) : 0.0;
            }
            j++;
        }
        i++;
        if (i >= numberOfCSVLines) {
            break;
        }

    }
    retList.add(mlDouble);
    return retList;
}
 
Example 16
Source File: RulesDbClientImpl.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Collection<RuleId> listRules(String workflowId, RuleAttributeSet attributeSet) {
    Objects.requireNonNull(workflowId);

    String path = RULES_PREFIX + workflowId + "/data.csv";
    Map<String, String> query = ImmutableMap.of("start", "0",
                                                "count", "-1",
                                                "headers", "true",
                                                "cols", "algoType,contingencyId,indexType");

    try {
        CsvReader csvReader = new CsvReader(httpClient.getHttpRequest(new HistoDbUrl(config, path, query)), ',', StandardCharsets.UTF_8);
        try {
            csvReader.setSafetySwitch(false);
            csvReader.readHeaders();

            List<RuleId> ruleIds = new ArrayList<>();

            while (csvReader.readRecord()) {
                String[] values = csvReader.getValues();
                ruleIds.add(new RuleId(RuleAttributeSet.valueOf(values[0]),
                            new SecurityIndexId(values[1], SecurityIndexType.fromLabel(values[2]))));
            }

            return ruleIds;
        } finally {
            csvReader.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 17
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Option group set format:
 * <p>
 * <ul>
 * <li>option group set name</li>
 * <li>option group set uid</li>
 * <li>option group set code</li>
 * <li>option group set description</li>
 * <li>data dimension</li>
 * <li>option set uid</li>
 * <li>option set code</li>
 * </ul>
 * @param reader
 * @return
 * @throws IOException
 */
private List<OptionGroupSet> setOptionGroupSetFromCsv( CsvReader reader )
    throws IOException
{
    List<OptionGroupSet> optionGroupSets = new ArrayList<>();
    CachingMap<String, OptionSet> mapOptionSet = new CachingMap<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            OptionGroupSet optionGroupSet = new OptionGroupSet();
            setIdentifiableObject( optionGroupSet, values );
            optionGroupSet.setDescription( getSafe( values, 4, null, null ) );
            optionGroupSet.setDataDimension( Boolean.valueOf( getSafe( values, 3,  Boolean.FALSE.toString(), 40 ) ) ); // boolean

            OptionSet optionSet = new OptionSet();
            optionSet.setUid( getSafe( values, 5, null, 11  ) );
            optionSet.setCode( getSafe( values, 6, null, 50  ) );

            if ( optionSet.getUid() == null && optionSet.getCode() == null )
            {
                continue;
            }

            OptionSet persistedOptionSet =  optionSet.getUid() != null ?
                mapOptionSet.get( optionSet.getUid(), () -> optionService.getOptionSet( optionSet.getUid() ) ) :
                mapOptionSet.get( optionSet.getCode(), () -> optionService.getOptionSetByCode( optionSet.getCode() )
                );

            if ( persistedOptionSet == null )
            {
                continue;
            }

            optionGroupSet.setOptionSet( optionSet );

            optionGroupSets.add( optionGroupSet );
        }
    }
    return optionGroupSets;
}
 
Example 18
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Option Group Set Members format
 * <ul>
 *     <li>option group set uid</li>
 *     <li>option group uid</li>
 * </ul>
 * @param reader
 * @return
 * @throws IOException
 */
public List<OptionGroupSet> optionGroupSetMembersFromCsv( CsvReader reader )
    throws IOException
{
    CachingMap<String, OptionGroupSet> uidMap = new CachingMap<>();
    CachingMap<String, OptionGroupSet> persistedGroupSetMap = new CachingMap<>();


    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            String groupSetUid = getSafe( values, 0, null, 11 );
            String groupUid = getSafe( values, 1, null, 11 );
            if ( groupSetUid == null || groupUid == null )
            {
                continue;
            }

            OptionGroupSet persistedGroupSet = persistedGroupSetMap.get( groupSetUid, () -> optionService.getOptionGroupSet( groupSetUid ) );

            if ( persistedGroupSet != null )
            {
                OptionGroupSet optionSetGroup = uidMap.get( groupSetUid, () -> {
                    OptionGroupSet nonPersistedGroup = new OptionGroupSet();
                    nonPersistedGroup.setUid( persistedGroupSet.getUid() );
                    nonPersistedGroup.setName( persistedGroupSet.getName() );
                    return nonPersistedGroup;
                } );

                OptionGroup member = new OptionGroup();
                member.setUid( groupUid );
                optionSetGroup.addOptionGroup( member );
            }
        }
    }

    return new ArrayList<>( uidMap.values() );
}
 
Example 19
Source File: DefaultCsvImportService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private List<OrganisationUnit> orgUnitsFromCsv( CsvReader reader )
    throws IOException
{
    List<OrganisationUnit> list = new ArrayList<>();

    while ( reader.readRecord() )
    {
        String[] values = reader.getValues();

        if ( values != null && values.length > 0 )
        {
            OrganisationUnit object = new OrganisationUnit();
            setIdentifiableObject( object, values );
            String parentUid = getSafe( values, 3, null, 230 ); // Could be UID, code, name
            object.setShortName( getSafe( values, 4, object.getName(), 50 ) );
            object.setDescription( getSafe( values, 5, null, null ) );
            object.setOpeningDate( getMediumDate( getSafe( values, 6, "1970-01-01", null ) ) );
            object.setClosedDate( getMediumDate( getSafe( values, 7, null, null ) ) );
            object.setComment( getSafe( values, 8, null, null ) );
            setGeometry( object, FeatureType.valueOf( getSafe( values, 9, "NONE", 50 ) ),
                getSafe( values, 10, null, null ) );
            object.setUrl( getSafe( values, 11, null, 255 ) );
            object.setContactPerson( getSafe( values, 12, null, 255 ) );
            object.setAddress( getSafe( values, 13, null, 255 ) );
            object.setEmail( getSafe( values, 14, null, 150 ) );
            object.setPhoneNumber( getSafe( values, 15, null, 150 ) );
            object.setAutoFields();

            if ( parentUid != null )
            {
                OrganisationUnit parent = new OrganisationUnit();
                parent.setUid( parentUid );
                object.setParent( parent );
            }

            list.add( object );
        }
    }

    return list;
}
 
Example 20
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void generateMDLatFile(String folderName, long startTimeInSec, long finishTimeInSec, String generatingFile, SimpleDateFormat format, String analyzedField, long startSec) throws IOException
{
	File folder = new File(folderName);

	if ( folder.exists() )
	{
		File[] files = folder.listFiles();

           Arrays.sort(files, new FileSpecialComparator(true, false));

           if(files.length == 0) {
               return;
           }

           int index = files.length - 1;
		for ( ; index >= 0; --index )
		{
			long startInterval = determineStartInterval(files[index].getName());

               if(startInterval < startTimeInSec * 1000000) {
                   break;
               }
		}

           if(index < 0) {
               index = 0;
           }


           try(BufferedWriter writer = new BufferedWriter(new FileWriter(generatingFile))) {
               int fieldIndex = -1;

               for(int i = index; i < files.length; ++i) {
                   if(isDebugged) {
                       logger.debug(files[i].getName());
                   }

                   CsvReader reader = new CsvReader(folderName + File.separator + files[i].getName());
                   reader.setSafetySwitch(false);

                   reader.readRecord();

                   if(i == index) {

                       String[] headers = reader.getValues();

                       for(int j = 0; j < headers.length; ++j) {
                           if(headers[j].equals(analyzedField)) {
                               fieldIndex = j;
                               break;
                           }
                       }

                       writer.write("Timestamp,Latency");

                       for(int j = 3; j < headers.length; ++j) {
                           writer.write("," + headers[j]);
                       }

                       writer.newLine();

                       if(fieldIndex == -1) {
                           throw new IllegalArgumentException("Could not find fieldName = [" + analyzedField + "] in inputFile = [" + files[i].getName() + "]");
                       }
                   }

                   while(reader.readRecord()) {
                       String[] values = reader.getValues();

                       long timestamp = Long.parseLong(values[0]);

                       if(timestamp >= startTimeInSec * 1000000 && (timestamp < finishTimeInSec * 1000000 || finishTimeInSec == Long.MAX_VALUE)) {
                           try {
                               String sendingTime = values[fieldIndex];

                               long sendingTimestamp = format.parse(sendingTime + " +0000").getTime();

                               sendingTimestamp = (sendingTimestamp - startSec * 1000) * 1000;

                               writer.write(values[0] + ',' + (timestamp - sendingTimestamp));

                               for(int j = 3; j < values.length; ++j) {
                                   writer.write("," + values[j]);
                               }

                               writer.newLine();
                           } catch(ParseException e) {
                           }
                       } else if(timestamp >= startTimeInSec * 1000000) {
                           break;
                       }
                   }

                   reader.close();
               }
		}
       } else {
           throw new EPSCommonException("Could not find [" + folderName + "] folder with files");
       }
}