Package org.supercsv.io

Examples of org.supercsv.io.CsvListReader


        return tree;
    }

    private static void addDictionaryEntriesToTree(AhoCorasick tree, String type, InputStream dictionaryInputStream) throws IOException {
        CsvPreference csvPrefs = CsvPreference.EXCEL_PREFERENCE;
        CsvListReader csvReader = new CsvListReader(new InputStreamReader(dictionaryInputStream), csvPrefs);
        List<String> line;
        while ((line = csvReader.read()) != null) {
            if (line.size() != 2) {
                throw new RuntimeException("Invalid number of entries on a line. Expected 2 found " + line.size());
            }
            tree.add(line.get(0), new Match(type, line.get(0), line.get(1)));
        }
View Full Code Here


        }
    }

    private void loadAirports() throws IOException {
        InputStreamReader reader = new InputStreamReader(FlightRepository.class.getResourceAsStream("airports.dat"));
        CsvListReader listReader = new CsvListReader(reader, CsvPreference.STANDARD_PREFERENCE);
        List<String> line;
        while ((line = listReader.read()) != null) {
            String title = line.get(1);
            String airportCode = line.get(5);
            double lat = Double.parseDouble(line.get(6));
            double lon = Double.parseDouble(line.get(7));
            if (airportCode == null) {
View Full Code Here

        }
    }

    private void loadAirlines() throws IOException {
        InputStreamReader reader = new InputStreamReader(FlightRepository.class.getResourceAsStream("airlines.dat"));
        CsvListReader listReader = new CsvListReader(reader, CsvPreference.STANDARD_PREFERENCE);
        List<String> line;
        while ((line = listReader.read()) != null) {
            String title = line.get(1);
            String identPrefix = line.get(4);
            if (identPrefix == null) {
                continue;
            }
View Full Code Here

    }

    private void prepareZipCodeDatabase() {
        try {
            InputStreamReader reader = new InputStreamReader(this.getClass().getResourceAsStream("zipcode.csv"));
            CsvListReader csvReader = new CsvListReader(reader, CsvPreference.STANDARD_PREFERENCE);
            csvReader.read(); // skip title line

            List<String> line;
            while ((line = csvReader.read()) != null) {
                if (line.size() < 5) {
                    continue;
                }
                String zipCode = line.get(0);
                String city = line.get(1);
View Full Code Here

        }
    }

    public static List<String> parseHintedHandoffEnabledDCs(final String dcNames) throws IOException
    {
        final CsvListReader csvListReader = new CsvListReader(new StringReader(dcNames), STANDARD_SURROUNDING_SPACES_NEED_QUOTES);
        return csvListReader.read();
    }
View Full Code Here

        }
    }

    public static List<String> parseHintedHandoffEnabledDCs(final String dcNames) throws IOException
    {
        final CsvListReader csvListReader = new CsvListReader(new StringReader(dcNames), STANDARD_SURROUNDING_SPACES_NEED_QUOTES);
        return csvListReader.read();
    }
View Full Code Here

        }
    }

    public static List<String> parseHintedHandoffEnabledDCs(final String dcNames) throws IOException
    {
        final CsvListReader csvListReader = new CsvListReader(new StringReader(dcNames), STANDARD_SURROUNDING_SPACES_NEED_QUOTES);
        return csvListReader.read();
    }
View Full Code Here

     *
     * @param dataFile
     * @throws IOException
     */
    private void setReader(String inCSV) throws IOException {
        _reader = new CsvListReader(new FileReader(inCSV), CsvPreference.EXCEL_PREFERENCE);
    }
View Full Code Here

  @Override
  public CsvTable unmarshal(String csvContent, boolean hasHeader) {
    CsvTable table = new CsvTable(hasHeader);
    try {
      StringReader sr = new StringReader(csvContent);
      CsvListReader clr = new CsvListReader(sr, CsvPreference.STANDARD_PREFERENCE);
     
      if(hasHeader) {
        table.setHeader(clr.read());
      }
     
      List<String> line = clr.read();
      while (line != null) {
        //Clone of read result here. Get back the same instance for every line???...omg
        table.add(new CsvRow(line));
        line = clr.read();
      }
     
    } catch (Exception e) {
      throw new RuntimeException("Marshelling of CSV content failed.", e);
    }
View Full Code Here

      CsvPreference prefs = CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
      String sep = p.getSeparatorChar();
      String quo = p.getQuotingChar();
      if (sep != null && sep.length() == 1) prefs.setDelimiterChar(sep.charAt(0));
      if (quo != null && quo.length() == 1) prefs.setQuoteChar(quo.charAt(0));
      ICsvListReader csv = new CsvListReader(new InputStreamReader(new ByteArrayInputStream(data),p.getFileEncoding()),prefs);

      List<String> line = csv.read();
     
      // Ggf. erste Zeilen ueberspringen
      for (int i=0;i<p.getSkipLines();++i)
      {
        line = csv.read();
      }

      // Nochmal checken, ob wir nach dem Ueberspringen ueberhaupt noch Zeilen uebrig haben
      if (line == null || line.size() == 0)
        throw new ApplicationException(i18n.tr("CSV-Datei enth�lt keine weiteren Daten"));

      int created       = 0;
      int error         = 0;
      int skipped       = 0;
      DBObject object   = null;
      String value      = null;
      List<Column> cols = p.getColumns();
      ImportListener l  = f.getImportListener();

      DBService service = (DBService) Application.getServiceFactory().lookup(HBCI.class,"database");
      do
      {
        monitor.log(i18n.tr("Importiere Zeile {0}",Integer.toString(csv.getLineNumber())));
        monitor.addPercentComplete(1);

        try
        {
          object = service.createObject(f.getType(),null);
         
          // Spalten zuordnen
          Map<String, Object> values = new HashMap<String,Object>();
          for (int i=0;i<line.size();++i)
          {
            Column column = null;
            for (Column c:cols)
            {
              if (c.getColumn() == i)
              {
                column = c;
                break;
              }
            }
           
            if (column == null)
              continue; // Spalte nicht zugeordnet
           
            // Checken, ob in der Spalte was steht
            value = line.get(i);
            if (value == null)
              continue;
            value = value.trim();
            if (value.length() == 0)
              continue;
           
            try
            {
              // Werte zwischenspeichern
              Object prev = values.get(column.getProperty());
              values.put(column.getProperty(),column.getSerializer().unserialize(prev,value));
            }
            catch (Exception e)
            {
              Logger.error("unable to unserialize " + column.getProperty() + " for line " + csv.getLineNumber() + ", value: " + value,e);
              monitor.log("  " + i18n.tr("Ung�ltiger Wert \"{0}\" in Spalte \"{1}\": {2}",value,column.getName(),e.getMessage()));
            }
          }
         
          // beforeSet-Listener ausloesen
          if (l != null)
          {
            ImportEvent e = new ImportEvent();
            e.context = context;
            e.data = values;
            try
            {
              l.beforeSet(e);
            }
            catch (OperationCanceledException oce)
            {
              skipped++;
              String msg = oce.getMessage();
              if (msg != null && msg.length() > 0)
                monitor.log("  " + msg);
              continue;
            }
          }
         

          // Werte in die Bean uebernehmen
          Iterator<String> it = values.keySet().iterator();
          while (it.hasNext())
          {
            String name = it.next();
            Object o = values.get(name);
            try
            {
              BeanUtil.set(object,name,o);
            }
            catch (Exception e)
            {
              Logger.error("unable to apply property " + name + " for line " + csv.getLineNumber() + ", value: " + o,e);
              String[] s = new String[]{value,
                                        name,
                                        e.getMessage()};
              monitor.log("  " + i18n.tr("Ung�ltiger Wert \"{0}\" in Spalte \"{1}\": {2}",s));
            }
          }
         
         
         
         
          if (l != null)
          {
            ImportEvent e = new ImportEvent();
            e.context = context;
            e.data = object;
            try
            {
              l.beforeStore(e);
            }
            catch (OperationCanceledException oce)
            {
              skipped++;
              String msg = oce.getMessage();
              if (msg != null && msg.length() > 0)
                monitor.log("  " + msg);
              continue;
            }
          }
         
          object.store();
          Application.getMessagingFactory().sendMessage(new ImportMessage(object));
          created++;
        }
        catch (Exception e)
        {
          if (!(e instanceof ApplicationException))
            Logger.error("unable to import line",e);

          monitor.log("  " + i18n.tr("Fehler in Zeile {0}: {1}",new String[]{Integer.toString(csv.getLineNumber()),e.getMessage()}));
          error++;
        }
      }
      while ((line = csv.read()) != null);

      // Fertig.
      monitor.setStatusText(i18n.tr("{0} importiert, {1} fehlerhaft, {2} �bersprungen", new String[]{Integer.toString(created),Integer.toString(error),Integer.toString(skipped)}));
    }
    catch (OperationCanceledException oce)
View Full Code Here

TOP

Related Classes of org.supercsv.io.CsvListReader

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.