private ImportLine processLine(long lineNumber, String line, Map<Integer, String> labelsMap) { ImportLine toReturn = new ImportLine(this, lineNumber); String[] columns = new StrTokenizer(line, this.delimitor) .setIgnoreEmptyTokens(false) .setTrimmerMatcher(StrMatcher.trimMatcher()) .getTokenArray(); for (int i = 0; i < columns.length; i++) { String value = StringUtils.strip(columns[i], "\""); if (this.labels) { toReturn.addColumn(labelsMap.get(i), value); } else { toReturn.addColumn(String.valueOf(i), value); } } return toReturn; }
@Override protected List<ImportLine> buildLignesSpecifique(String fileName) throws ImportException { List<ImportLine> toReturn = new ArrayList<ImportLine>(); long lineNumber = 0; try { FileInputStream f = new FileInputStream(fileName); BufferedReader bufferedReader = null; try { // le fichier est encode en UCS-2 Little Endian if (this.littleEndian) { bufferedReader = new BufferedReader(new UCSReader(f, UCSReader.UCS2LE)); } else { bufferedReader = new BufferedReader(new InputStreamReader(f)); } lineNumber = skipLines(bufferedReader, lineNumber); // on lit les lignes suivantes jusqu'à baliseFin boolean labelsRead = labels ? false : true; Map<Integer, String> labelsMap = null; String line; while ((line = bufferedReader.readLine()) != null && (StringUtils.isBlank(endTag) || !StringUtils.equals(line, endTag))) { lineNumber++; if (!labelsRead) { // On lit les intitulés String[] labelsString = new StrTokenizer(line, this.delimitor) .setIgnoreEmptyTokens(false) .setTrimmerMatcher(StrMatcher.trimMatcher()) .getTokenArray(); labelsMap = new HashMap<Integer, String>(); for (int i = 0; i < labelsString.length; i++) { labelsMap.put(i, labelsString[i]); } } else { if (StringUtils.isNotBlank(line)) { toReturn.add(processLine(lineNumber, line, labelsMap)); } } } if (!StringUtils.isBlank(endTag) && !StringUtils.equals(line, endTag)) { throw new ParseFileImportException( "Impossible de trouver la balise de fin : " + endTag, this); } } finally { bufferedReader.close(); f.close(); } } catch (IOException e) { String message = "Erreur d'E/S lors de la lecture du fichier : " + fileName; throw new ImportException(message, e); } return toReturn; }