Ejemplo n.º 1
0
 /**
  * Reads a tab-separated file and puts the contents into a map.
  *
  * <p>We give a few options: - You can set the index for the key to the map. If the key is not
  * zero, we only add the first column as a value to this map (and so setting overwrite to true in
  * this case doesn't make a whole lot of sense - just use readMapFromTsv instead). - If overwrite
  * is true, we don't bother checking to see if the key is already in the map. This will speed up
  * the processing if you know that your file only has one line per unique key. - You can provide a
  * LineFilter object that wlil be called with each line to determine if it should be skipped.
  */
 public Map<String, List<String>> readMapListFromTsvReader(
     BufferedReader reader, int keyIndex, boolean overwrite, LineFilter filter)
     throws IOException {
   Map<String, List<String>> map = Maps.newHashMap();
   String line;
   while ((line = reader.readLine()) != null) {
     String[] fields = line.split("\t");
     if (filter != null && filter.filter(fields)) continue;
     String key = fields[keyIndex];
     List<String> list;
     if (overwrite) {
       list = Lists.newArrayList();
       map.put(key, list);
     } else {
       list = map.get(key);
       if (list == null) {
         list = Lists.newArrayList();
         map.put(key, list);
       }
     }
     if (keyIndex == 0) {
       for (int i = 1; i < fields.length; i++) {
         list.add(fields[i]);
       }
     } else {
       list.add(fields[0]);
     }
   }
   return map;
 }
Ejemplo n.º 2
0
 /**
  * Constructor.
  *
  * @param csvFile the csv file
  * @param block the block to read, <code>null</code> to read default block
  */
 public CsvFile(File csvFile, String block, LineFilter filter) throws Exception {
   BufferedReader reader = new BufferedReader(new FileReader(csvFile));
   String line = null;
   int lineNr = 0;
   boolean inBlock = block == null;
   while ((line = reader.readLine()) != null) {
     ++lineNr;
     if (line.trim().length() == 0) {
       continue;
     }
     if (line.trim().startsWith(BLOCK_INDICATOR)) {
       if (inBlock) {
         break;
       }
       String blockName = line.trim().substring(BLOCK_INDICATOR.length()).trim();
       inBlock = block.equals(blockName);
       continue;
     }
     if (line.trim().startsWith("#include ")) {
       String includeFile = line.trim().substring(9).trim();
       rows.addAll(new CsvFile(new File(csvFile.getParent() + File.separator + includeFile)).rows);
       continue;
     }
     if (line.trim().startsWith("#")) {
       continue;
     }
     if (!inBlock) {
       continue;
     }
     List<String> row = new ArrayList<String>();
     String[] col = decodeLine(line);
     for (int i = 0; i < col.length; ++i) {
       String s = col[i];
       row.add(s.trim());
     }
     while (row.size() < 100) {
       row.add("");
     }
     Line cvsLine = new Line("line " + lineNr + ", file " + csvFile.getName(), row);
     if (filter == null || filter.accept(cvsLine)) {
       rows.add(cvsLine);
     }
   }
   reader.close();
 }