Package com.csvreader

Examples of com.csvreader.CsvReader


    }

    public void refreshPreviewTable() {
        if (selectedFile != null && selectedFile.exists()) {
            try {
                CsvReader reader = new CsvReader(new FileInputStream(selectedFile), getSelectedSeparator(), getSelectedCharset());
                reader.setTrimWhitespace(false);
                String[] headers;
                try {
                    reader.readHeaders();
                    headers = reader.getHeaders();
                } catch (Exception ex) {
                    headers = new String[0];//Some charsets can be problematic with unreal columns lenght. Don't show table when there are problems
                }
                columnCount = headers.length;

                //Check for repeated column names:
                Set<String> columnNamesSet = new HashSet<String>();
                columnNamesRepeated = false;
                hasSourceNodeColumn = false;
                hasTargetNodeColumn = false;
                for (String header : headers) {
                    if (header.equalsIgnoreCase("source")) {
                        hasSourceNodeColumn = true;
                    }
                    if (header.equalsIgnoreCase("target")) {
                        hasTargetNodeColumn = true;
                    }
                    if (columnNamesSet.contains(header)) {
                        columnNamesRepeated = true;
                        break;
                    }
                    columnNamesSet.add(header);
                }

                ArrayList<String[]> records = new ArrayList<String[]>();
                if (columnCount > 0) {
                    String[] currentRecord;
                    while (reader.readRecord() && records.size() < MAX_ROWS_PREVIEW) {
                        currentRecord = new String[reader.getColumnCount()];
                        for (int i = 0; i < currentRecord.length; i++) {
                            currentRecord[i] = reader.get(i);
                        }
                        records.add(currentRecord);
                    }
                }
                reader.close();
                final String[] columnNames = headers;
                final String[][] values = records.toArray(new String[0][]);
                previewTable.setModel(new TableModel() {

                    public int getRowCount() {
View Full Code Here


                int returnValue = fileChooser.showOpenDialog(EmailVisualPanel2.this);
                if (returnValue == javax.swing.JFileChooser.APPROVE_OPTION) {
                    NbPreferences.forModule(EmailVisualPanel2.class).put(CSV_LAST_PATH, fileChooser.getCurrentDirectory().getAbsolutePath());
                    File csvFile = fileChooser.getSelectedFile();
                    try {
                        CsvReader csvReader = new CsvReader(new FileInputStream(csvFile), Charset.forName("UTF-8"));
                        csvReader.setSkipEmptyRecords(true);
                        List<String[]> rows = new ArrayList<String[]>();
                        while (csvReader.readRecord()) {
                            String[] values = csvReader.getValues();
                            if (values.length > 0) {
                                String email = values[0];
                                String type = values.length > 1 ? values[1] : "All";
                                if (!email.isEmpty() && (type.equals("From")
                                        || type.equals("To")
                                        || type.equals("Cc")
                                        || type.equals("Bcc")
                                        || type.equals("All"))) {
                                    rows.add(new String[]{email, type});
                                }
                            }
                        }
                        csvReader.close();

                        //Clean Rows
                        filterTableModel.setRowCount(0);

                        //Add
View Full Code Here

    private void loadColumns(JPanel settingsPanel) {
        try {
            JLabel columnsLabel = new JLabel(getMessage("ImportCSVUIVisualPanel2.columnsLabel.text"));
            settingsPanel.add(columnsLabel, "wrap");

            CsvReader reader = new CsvReader(new FileInputStream(file), separator, charset);
            reader.setTrimWhitespace(false);
            reader.readHeaders();
            final String[] columns = reader.getHeaders();
            reader.close();

            boolean sourceFound = false, targetFound = false, typeFound=false;//Only first source and target columns found will be used as source and target nodes ids.
            columnsCheckBoxes = new JCheckBox[columns.length];
            columnsComboBoxes = new JComboBox[columns.length];
            for (int i = 0; i < columns.length; i++) {
View Full Code Here

        if (columnTypes == null || columnNames.length != columnTypes.length) {
            throw new IllegalArgumentException("Column names length must be the same as column types lenght");
        }

        CsvReader reader = null;
        try {
            //Prepare attribute columns for the column names, creating the not already existing columns:
            AttributeTable nodesTable = Lookup.getDefault().lookup(AttributeController.class).getModel().getNodeTable();
            String idColumn = null;
            ArrayList<AttributeColumn> columnsList = new ArrayList<AttributeColumn>();
            for (int i = 0; i < columnNames.length; i++) {
                //Separate first id column found from the list to use as id. If more are found later, the will not be in the list and be ignored.
                if (columnNames[i].equalsIgnoreCase("id")) {
                    if (idColumn == null) {
                        idColumn = columnNames[i];
                    }
                } else if (nodesTable.hasColumn(columnNames[i])) {
                    columnsList.add(nodesTable.getColumn(columnNames[i]));
                } else {
                    columnsList.add(addAttributeColumn(nodesTable, columnNames[i], columnTypes[i]));
                }
            }

            //Create nodes:
            GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
            Graph graph = Lookup.getDefault().lookup(GraphController.class).getModel().getGraph();
            String id = null;
            Node node;
            Attributes nodeAttributes;
            reader = new CsvReader(new FileInputStream(file), separator, charset);
            reader.readHeaders();
            while (reader.readRecord()) {
                //Prepare the correct node to assign the attributes:
                if (idColumn != null) {
                    id = reader.get(idColumn);
                    if (id == null || id.isEmpty()) {
                        node = gec.createNode(null);//id null or empty, assign one
                    } else {
                        graph.readLock();
                        node = graph.getNode(id);
                        graph.readUnlock();
                        if (node != null) {//Node with that id already in graph
                            if (assignNewNodeIds) {
                                node = gec.createNode(null);
                            }
                        } else {
                            node = gec.createNode(null, id);//New id in the graph
                        }
                    }
                } else {
                    node = gec.createNode(null);
                }
                //Assign attributes to the current node:
                nodeAttributes = node.getNodeData().getAttributes();
                for (AttributeColumn column : columnsList) {
                    setAttributeValue(reader.get(column.getTitle()), nodeAttributes, column);
                }
            }
        } catch (FileNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            reader.close();
        }
    }
View Full Code Here

        if (columnTypes == null || columnNames.length != columnTypes.length) {
            throw new IllegalArgumentException("Column names length must be the same as column types lenght");
        }

        CsvReader reader = null;
        try {
            //Prepare attribute columns for the column names, creating the not already existing columns:
            AttributeTable edges = Lookup.getDefault().lookup(AttributeController.class).getModel().getEdgeTable();
            String idColumn = null;
            String sourceColumn = null;
            String targetColumn = null;
            String typeColumn = null;
            ArrayList<AttributeColumn> columnsList = new ArrayList<AttributeColumn>();
            for (int i = 0; i < columnNames.length; i++) {
                //Separate first id column found from the list to use as id. If more are found later, the will not be in the list and be ignored.
                if (columnNames[i].equalsIgnoreCase("id")) {
                    if (idColumn == null) {
                        idColumn = columnNames[i];
                    }
                } else if (columnNames[i].equalsIgnoreCase("source") && sourceColumn == null) {//Separate first source column found from the list to use as source node id
                    sourceColumn = columnNames[i];
                } else if (columnNames[i].equalsIgnoreCase("target") && targetColumn == null) {//Separate first target column found from the list to use as target node id
                    targetColumn = columnNames[i];
                } else if (columnNames[i].equalsIgnoreCase("type") && typeColumn == null) {//Separate first type column found from the list to use as edge type (directed/undirected)
                    typeColumn = columnNames[i];
                } else if (edges.hasColumn(columnNames[i])) {
                    columnsList.add(edges.getColumn(columnNames[i]));
                } else {
                    columnsList.add(addAttributeColumn(edges, columnNames[i], columnTypes[i]));
                }
            }

            //Create edges:
            GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
            Graph graph = Lookup.getDefault().lookup(GraphController.class).getModel().getGraph();
            String id = null;
            Edge edge;
            String sourceId, targetId;
            Node source, target;
            String type;
            boolean directed;
            Attributes edgeAttributes;
            reader = new CsvReader(new FileInputStream(file), separator, charset);
            reader.readHeaders();
            while (reader.readRecord()) {
                sourceId = reader.get(sourceColumn);
                targetId = reader.get(targetColumn);

                if (sourceId == null || sourceId.isEmpty() || targetId == null || targetId.isEmpty()) {
                    continue;//No correct source and target ids were provided, ignore row
                }

                graph.readLock();
                source = graph.getNode(sourceId);
                graph.readUnlock();

                if (source == null) {
                    if (createNewNodes) {//Create new nodes when they don't exist already and option is enabled
                        if (source == null) {
                            source = gec.createNode(null, sourceId);
                        }
                    } else {
                        continue;//Ignore this edge row, since no new nodes should be created.
                    }
                }

                graph.readLock();
                target = graph.getNode(targetId);
                graph.readUnlock();

                if (target == null) {
                    if (createNewNodes) {//Create new nodes when they don't exist already and option is enabled
                        if (target == null) {
                            target = gec.createNode(null, targetId);
                        }
                    } else {
                        continue;//Ignore this edge row, since no new nodes should be created.
                    }
                }

                if (typeColumn != null) {
                    type = reader.get(typeColumn);
                    //Undirected if indicated correctly, otherwise always directed:
                    if (type != null) {
                        directed = !type.equalsIgnoreCase("undirected");
                    } else {
                        directed = true;
                    }
                } else {
                    directed = true;//Directed by default when not indicated
                }

                //Prepare the correct edge to assign the attributes:
                if (idColumn != null) {
                    id = reader.get(idColumn);
                    if (id == null || id.isEmpty()) {
                        edge = gec.createEdge(source, target, directed);//id null or empty, assign one
                    } else {
                        edge = gec.createEdge(id, source, target, directed);
                        if (edge == null) {//Edge with that id already in graph
                            edge = gec.createEdge(source, target, directed);
                        }
                    }
                } else {
                    edge = gec.createEdge(source, target, directed);
                }

                if (edge != null) {//Edge could be created because it does not already exist:
                    //Assign attributes to the current edge:
                    edgeAttributes = edge.getEdgeData().getAttributes();
                    for (AttributeColumn column : columnsList) {
                        setAttributeValue(reader.get(column.getTitle()), edgeAttributes, column);
                    }
                }
            }
        } catch (FileNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            reader.close();
        }
    }
View Full Code Here

    filters.put(placement, filtersThere);
    getMongoDbView().refreshMe();
  }

  private MongoInstance[] loadSavedServers() {
    CsvReader reader = null;
    try {
      IPath libPath = MeclipsePlugin.getDefault().getStateLocation();
      libPath = libPath.append("servers.cfg");
      File file = libPath.toFile();
      if (!file.exists())
        return new MongoInstance[0];

      reader = new CsvReader(new BufferedReader(new FileReader(file)));

      java.util.List<MongoInstance> savedServersList = new ArrayList<MongoInstance>();
      while (reader.readRecord()) {
        MongoInstance server = new MongoInstance(reader.get(0));
        server.setHost(reader.get(1));
        try {
          server.setPort(Integer.valueOf(reader.get(2)));
        } catch (NumberFormatException e) {
          System.out.println(e);
        }
        savedServersList.add(server);
      }
      return savedServersList.toArray(new MongoInstance[savedServersList
          .size()]);
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      if (reader != null) {
        reader.close();
      }
    }

    return new MongoInstance[0];
  }
View Full Code Here

  }

  public static List<Map<String, String>> read(String csv) {
    List<Map<String, String>> csvList = new ArrayList<Map<String, String>>();

    CsvReader csvReader;
    try {
      csvReader = new CsvReader(new StringReader(csv));
      if (csvReader.readHeaders()) {
        String[] headers = csvReader.getHeaders();
        while (csvReader.readRecord()) {
          Map<String, String> csvMap = new HashMap<String, String>();
          for (String head : headers) {
            csvMap.put(head, csvReader.get(head));
          }
          csvList.add(csvMap);
        }
      }
    } catch (FileNotFoundException e) {
View Full Code Here

  }

  public static List<Map<String, String>> read(String csv) {
    List<Map<String, String>> csvList = new ArrayList<Map<String, String>>();

    CsvReader csvReader;
    try {
      csvReader = new CsvReader(new StringReader(csv));
      if (csvReader.readHeaders()) {
        String[] headers = csvReader.getHeaders();
        while (csvReader.readRecord()) {
          Map<String, String> csvMap = new HashMap<String, String>();
          for (String head : headers) {
            csvMap.put(head, csvReader.get(head));
          }
          csvList.add(csvMap);
        }
      }
    } catch (FileNotFoundException e) {
View Full Code Here

    return obs;
  }

  private void commonInvalidTest(String str) throws IOException {
    try {
      CsvReader reader = new CsvReader(new StringReader(str));
      reader.setDelimiter('\t');
      assertTrue(reader.readRecord());
      SimpleTextFormatValidator validator = new SimpleTextFormatValidator(
          reader, 2, 5, NewStarType.NEW_STAR_FROM_SIMPLE_FILE
              .getFieldInfoSource());
      validator.validate();
      // We should have thrown a ObservationValidationError...
View Full Code Here

  public void testRightNumberOfFields2To5() throws IOException {
    String line = "2450001.5\t10.0\n";

    try {
      CsvReader reader = new CsvReader(new StringReader(line));
      reader.setDelimiter('\t');
      assertTrue(reader.readRecord());
      ObservationFieldSplitter splitter = new ObservationFieldSplitter(
          reader, 2, 5);
      String[] fields = splitter.getFields();
      assertEquals(5, fields.length);
View Full Code Here

TOP

Related Classes of com.csvreader.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.