@Override public RecordSet getRecords() throws MalformedSourceException { try { openReader(); } catch (IOException e) { logger_.info("Problem loading file: " + filename + " (" + e.getMessage() + ")"); throw new MalformedSourceException( "Problem loading file: " + filename + " (" + e.getMessage() + ")"); } catch (InvalidFormatException e) { logger_.info("Problem loading file: " + filename + " (" + e.getMessage() + ")"); throw new MalformedSourceException( "Problem loading file: " + filename + " (" + e.getMessage() + ")"); } RecordSet ret = new RecordSet(); // Currently we need this flag in order for // TransformationEngine not to go into an infinite loop. if (!isRead_) { logger_.info("Opening file: " + filename); int nSheets = wb_.getNumberOfSheets(); logger_.info("number of sheets: " + nSheets); for (int i = 0; i < nSheets; i++) { Sheet cSheet = wb_.getSheetAt(i); String cSheetName = cSheet.getSheetName(); for (int j = skipLines_; j <= cSheet.getLastRowNum(); j++) { if (ignoreLinesAfter_ != 0 && j >= ignoreLinesAfter_) { break; } Row row = cSheet.getRow(j); MapRecord rec = new MapRecord(); for (int k = 0; k < row.getLastCellNum(); k++) { if (!fieldMap_.keySet().contains(k)) { continue; } StringValue val; Cell cCell = row.getCell(k); if (cCell == null) { continue; } int cellType = cCell.getCellType(); switch (cellType) { case Cell.CELL_TYPE_STRING: val = new StringValue(cCell.getStringCellValue()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cCell)) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); val = new StringValue(sdf.format(cCell.getDateCellValue())); } else { val = new StringValue(String.valueOf(cCell.getNumericCellValue())); } break; case Cell.CELL_TYPE_BLANK: val = new StringValue(""); break; case Cell.CELL_TYPE_BOOLEAN: val = new StringValue(String.valueOf(cCell.getBooleanCellValue())); break; default: val = new StringValue("Unsupported cell type"); } rec.addValue(fieldMap_.get(k), val); } // TODO remove the hardcoded value rec.addValue("ExcelSheetName", new StringValue(cSheetName)); ret.addRecord(rec); } } isRead_ = true; } return ret; }
@Override public RecordSet getRecords() throws MalformedSourceException { if (reader_ == null) { throw new EmptySourceException("Input file is not open"); } RecordSet records = new RecordSet(); try { String line; boolean in_record = false; int line_cnt = 0; MapRecord rec = null; String ris_tag = null; while ((line = reader_.readLine()) != null) { line_cnt++; // Ignore empty lines if (line.isEmpty() || line.equals("") || line.matches("^\\s*$")) { continue; } Pattern ris_pattern = Pattern.compile("^([A-Z][A-Z0-9]) - (.*)$"); Matcher ris_matcher = ris_pattern.matcher(line); Value val; if (ris_matcher.matches()) { ris_tag = ris_matcher.group(1); if (!in_record) { // The first tag of the record should be "TY". If we // encounter it we should create a new record. if (ris_tag.equals("TY")) { in_record = true; rec = new MapRecord(); } else { logger_.info( "Line: " + line_cnt + " in file " + filename + " should contain tag \"TY\""); throw new MalformedSourceException( "Line: " + line_cnt + " in file " + filename + " should contain tag \"TY\""); } } // If the tag is the end record tag ("ER") we stop // being in a record. Add the current record in the // record set and skip the rest of the processing. if (ris_tag.equals("ER")) { in_record = false; records.addRecord(rec); rec = null; continue; } // If there is no mapping for the current tag we do not // know what to do with it, so we ignore it. if (!field_map_.containsKey(ris_tag)) { logger_.warn("Tag \"" + ris_tag + "\" is not in the field map. Ignoring"); continue; } val = new StringValue(ris_matcher.group(2)); } else { val = new StringValue(line); } String field = field_map_.get(ris_tag); if (field != null) { rec.addValue(field, val); } } } catch (IOException e) { logger_.info("Error while reading from file " + filename); throw new MalformedSourceException("Error while reading from file " + filename); } return records; }