Package au.com.bytecode.opencsv

Examples of au.com.bytecode.opencsv.CSVReader


public class CsvLineData extends AbstractLineData {
    private final CSVReader csvReader;

    public CsvLineData(Reader reader, char delim, int offset) {
        super(offset);
        this.csvReader = new CSVReader(reader, delim,'"','\\',0,false,false);
        initHeaders(createHeaders(readRawRow()));
        createMapData(lineSize, offset);
    }
View Full Code Here


public class OpenCSVTest {

    @Test
    public void testReadLineWithCommaSeparator() throws Exception {
        final StringReader headerWithLine = new StringReader("a,b\n1,42");
        assertReadFile(new CSVReader(headerWithLine), "42");
    }
View Full Code Here

        assertReadFile(new CSVReader(headerWithLine), "42");
    }
    @Test
    public void testReadLineWithTabSeparator() throws Exception {
        final StringReader headerWithLine = new StringReader("a\tb\n1\t42");
        assertReadFile(new CSVReader(headerWithLine,'\t'), "42");
    }
View Full Code Here

        assertReadFile(new CSVReader(headerWithLine,'\t'), "42");
    }
    @Test
    public void testReadLineWithTabSeparatorAndDoubleQuotes() throws Exception {
        final StringReader headerWithLine = new StringReader("a\t\"b\"\n1\t\"42\"");
        assertReadFile(new CSVReader(headerWithLine,'\t','"'), "42");
    }
View Full Code Here

    }

    @Test
    public void testReadLineWithTabSeparatorAndDoubleQuotesWithNewlineInValue() throws Exception {
        final StringReader headerWithLine = new StringReader("a\t\"b\"\n1\t\"4\n2\"");
        assertReadFile(new CSVReader(headerWithLine,'\t','"'), "4\n2");
    }
View Full Code Here

    }

    @Test
    public void testReadLineWithCommaSeparator() throws Exception {
        final BufferedReader reader = new BufferedReader(new FileReader(TEST_CSV));
        final CSVReader csvReader = new CSVReader(reader,'\t','"');

        int res = 0;
        long time = System.currentTimeMillis();
        String[] line = null;
        while ((line = csvReader.readNext()) != null) {
            res += line.length;
        }
        time = System.currentTimeMillis() - time;
        System.out.println("time = " + time + " ms.");
        Assert.assertEquals(ROWS * COLS, res);
View Full Code Here

    }
  }

  private static void parseResults(InputStream in, SearchResultListener srl)
    throws IOException {
    CSVReader csvr = new CSVReader(new InputStreamReader(in));
    try {
      String [] header = csvr.readNext();

      if (header != null
          && header.length > 0
          && !(header.length == 1 && header[0].isEmpty())) {
        srl.setFieldNames(header);

        String[] line;
        while ((line = csvr.readNext()) != null) {
          if (line.length == header.length) {
            srl.processSearchResult(line);
          }
        }
      }
View Full Code Here

     * </ul>
     */
    private int source;

    public SplunkResultEnumerator(InputStream in, List<String> wantedFields) {
      csvReader = new CSVReader(new InputStreamReader(in));
      try {
        fieldNames = csvReader.readNext();
        if (fieldNames == null
            || fieldNames.length == 0
            || fieldNames.length == 1 && fieldNames[0].isEmpty()) {
View Full Code Here

        return null;
    }

    private int importCsv(FileItem item) throws IOException, TranslatableException {
        CSVReader csvReader = new CSVReader(new InputStreamReader(item.getInputStream()));
        DataPointDao dataPointDao = new DataPointDao();
        PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");

        // Basic validation
        String[] nextLine = csvReader.readNext();
        if (nextLine == null)
            throw new TranslatableException(new TranslatableMessage("dataImport.import.noData"));
        if (nextLine.length < 2)
            throw new TranslatableException(new TranslatableMessage("dataImport.import.noPoints"));

        // Find the points by XID
        DataPointVO[] vos = new DataPointVO[nextLine.length - 1];

        for (int i = 1; i < nextLine.length; i++) {
            if (StringUtils.isBlank(nextLine[i]))
                throw new TranslatableException(new TranslatableMessage("dataImport.import.badXid", i));

            DataPointVO vo = dataPointDao.getDataPoint(nextLine[i]);
            if (vo == null)
                throw new TranslatableException(new TranslatableMessage("dataImport.import.xidNotFound", nextLine[i]));

            vos[i - 1] = vo;
        }

        // Find the RTs for the points if they are enabled
        DataPointRT[] rts = new DataPointRT[vos.length];
        for (int i = 0; i < vos.length; i++)
            rts[i] = Common.runtimeManager.getDataPoint(vos[i].getId());

        // Import the data
        int count = 0;
        while ((nextLine = csvReader.readNext()) != null) {
            // The first value is always a date.
            long time = dtf.parseDateTime(nextLine[0]).getMillis();

            // The rest of the values are point samples.
            for (int i = 1; i < nextLine.length; i++) {
View Full Code Here

         FileOutputStream fileOutputStream = new FileOutputStream(uploadedFile);
         IOUtils.copy(request.body, fileOutputStream);
         fileOutputStream.close();
          
          
           CSVReader csvReader = new CSVReader(new FileReader(uploadedFile));
          
           int lineNum = 1;
          
           HashMap<Integer, TripPatternStop> columnStopIndex = new HashMap<Integer, TripPatternStop>();
          
           HashMap<Integer, Integer> columnStopDelta = new HashMap<Integer, Integer>();
          
           Integer cumulativeTime = 0;
          
           List<TripPatternStop> patternStops = TripPatternStop.find("pattern = ? order by stopSequence", pattern).fetch();
          
           for(String[] csvLine : csvReader.readAll())
           {
             int columnIndex = 0;
          
             if(lineNum == 3)
             {
               if(!csvLine[5].equals("stop_id"))
                 throw new Exception("Invalid stop_id row.");
              
               for(String column : csvLine
               {
                 if(columnIndex > 5)
                 {
                   Stop stop = Stop.findById(Long.parseLong(column));
                  
                   TripPatternStop patternStop = patternStops.get(0);
                   patternStops = patternStops.subList(1, patternStops.size());
                  
                   if(!patternStop.stop.id.equals(stop.id))
                     throw new Exception("Stop ID " + stop.id + "doesn't match pattern sequence for stop " + patternStop.stop.id);
                  
                   columnStopIndex.put(columnIndex, patternStop);
                  
                   cumulativeTime += patternStop.defaultDwellTime != null? patternStop.defaultDwellTime: 0;
                   cumulativeTime += patternStop.defaultTravelTime != null? patternStop.defaultTravelTime: ;
                  
                   columnStopDelta.put(columnIndex, new Integer(cumulativeTime));
                 }
                
                 columnIndex++;
               }
             }
             else if(lineNum > 6)
             {
               if(!csvLine[0].isEmpty())
               {
                 Trip trip = new Trip();
                
                 trip.pattern = pattern;
                 trip.serviceCalendar = calendar;
                
                 trip.blockId = csvLine[2];
                 trip.tripHeadsign = csvLine[3];
                 trip.tripShortName = csvLine[4];
                
                 trip.useFrequency = false;
                
                 trip.save();
                
                 Integer firstTimepoint = null;
                 Integer columnCount = 0;
                 Integer previousTime = 0;
                 Integer dayOffset = 0;
                
                 for(String column : csvLine
                 { 
                   if(columnIndex > 5)
                   {
                     if(!column.isEmpty())
                     {
                       StopTime stopTime = new StopTime();
                      
                       stopTime.trip = trip;
                      
                       // check for board/alight only flag
                       if(column.contains(">")) {
                        
                         column = column.replace(">", "");
                        
                         // board only
                         stopTime.dropOffType = StopTimePickupDropOffType.NONE;
                       }
                      
                       if(column.contains("<")) {
                        
                         column = column.replace("<", "");
                        
                         // alight only
                         stopTime.pickupType = StopTimePickupDropOffType.NONE;
                       }
                      
                       column = column.trim();
                      
                       if(column.equals("+"))
                         stopTime.departureTime = firstTimepoint + columnStopDelta.get(columnIndex);
                       else if(column.equals("-"))
                         stopTime.departureTime = null;
                       else
                       {
                         Integer currentTime;
                          
                        
                         try
                         {
                           currentTime = (dfTime.parse(column).getHours() * 60 * 60 ) + (dfTime.parse(column).getMinutes() * 60) + (dfTime.parse(column).getSeconds());
                         }
                         catch(ParseException e)
                         {
                           try
                           {
                             currentTime = (dfsTime.parse(column).getHours() * 60 * 60 ) + (dfsTime.parse(column).getMinutes() * 60) + (dfsTime.parse(column).getSeconds());
                           }
                           catch(ParseException e2)
                           {
                             continue;
                           }
                         }
                        
                         // in case of time that decreases add a day to offset for trips that cross midnight boundary
                         if(previousTime > currentTime)                       
                           dayOffset += 24 * 60 * 60;
                          
                         stopTime.departureTime = currentTime + dayOffset;
                        
                         previousTime = currentTime;
                        
                         if(firstTimepoint == null)
                           {
                           firstTimepoint = stopTime.departureTime;
                           }
                        
                       }
                      
                       stopTime.arrivalTime = stopTime.departureTime;
                      
                       stopTime.patternStop = columnStopIndex.get(columnIndex);
                       stopTime.stop = columnStopIndex.get(columnIndex).stop;
                       stopTime.stopSequence = columnCount + 1;
                      
                       stopTime.save();
                      
                       columnCount++;
                     }
                   }
                    
                   columnIndex++;
                 }             
               }
             }
            
             lineNum++;
           }
          
           csvReader.close();
       }
       catch(Exception e)
       {
         Logger.error(e.toString());
       }
View Full Code Here

TOP

Related Classes of au.com.bytecode.opencsv.CSVReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.