@Override protected FixedWidthParserSettings createDefaultParserSettings() { return new FixedWidthParserSettings(); }
skipToNewLine = settings.getSkipTrailingCharsUntilNewline(); recordEndsOnNewLine = settings.getRecordEndsOnNewline(); skipEmptyLines = settings.getSkipEmptyLines(); lengths = settings.getAllLengths(); alignments = settings.getFieldAlignments(); paddings = settings.getFieldPaddings(); ignore = settings.getFieldsToIgnore(); keepPaddingFlags = settings.getKeepPaddingFlags(); keepPadding = settings.getKeepPadding(); lookaheadFormats = settings.getLookaheadFormats(); lookbehindFormats = settings.getLookbehindFormats(); wildcard = settings.getFormat().getLookupWildcard(); FixedWidthFormat format = settings.getFormat(); padding = format.getPadding(); defaultPadding = padding; newLine = format.getNormalizedNewline(); useDefaultPadding = settings.getUseDefaultPaddingForHeaders() && settings.isHeaderExtractionEnabled();
/** * Clones this configuration object to reuse most user-provided settings. Properties that are specific to a given * input (such as header names and selection of fields) will be reset to their defaults. * * To obtain a full copy, use {@link #clone()}. * * @param fields the fixed-width field configuration to be used by the cloned settings object. * * @return a copy of the <em>general</em> configurations applied to the current instance. */ public final FixedWidthParserSettings clone(FixedWidthFields fields) { return clone(true, fields); }
private void init() { fields = new ArrayList<FixedWidthField>(); List<String> headers = new ArrayList<String>(); List<Integer> fieldWidth = new ArrayList<Integer>(); for (String tmp : fieldDescription.split(",")) { String[] fieldTuple = tmp.split(":(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1); FixedWidthField field = new FixedWidthField(); field.setName(fieldTuple[0]); field.setType(fieldTuple[1]); field.setWidth(Integer.parseInt(fieldTuple[2])); headers.add(fieldTuple[0]); fieldWidth.add(Integer.parseInt(fieldTuple[2])); if (field.getType() == FIELD_TYPE.DATE) { if (fieldTuple.length > 3) { field.setDateFormat(fieldTuple[3].replace("\"", "")); } else { logger.error("Date format is missing for the field {}", field.getName()); throw new RuntimeException("Missing date format"); } } fields.add(field); } header = headers.toArray(new String[headers.size()]); int[] width = Ints.toArray(fieldWidth); FixedWidthFields lengths = new FixedWidthFields(header, width); FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths); settings.getFormat().setPadding(this.padding); fixedWidthParser = new FixedWidthParser(settings); }
Lookup[] getLookbehindFormats() { return Lookup.getLookupFormats(lookbehindFormats, getFormat()); }
/** * Returns the hard limit of how many columns a record can have (defaults to a maximum of 512). * You need this to avoid OutOfMemory errors in case of inputs that might be inconsistent with the format you are dealing with. * * <p> This overrides the parent implementation and calculates the absolute minimum number of columns required to store the values of a record * <p> If the sum of all fields is greater than the configured maximum number columns, the calculated amount will be returned. * * @return The maximum number of columns a record can have. */ @Override public int getMaxColumns() { int max = super.getMaxColumns(); int minimum = calculateMaxFieldLengths().length; return max > minimum ? max : minimum; }
/** * Returns the sequence of fields whose padding character must/must not be retained in the parsed value * @return the sequence that have an explicit 'keepPadding' flag. */ Boolean[] getKeepPaddingFlags() { if (fieldLengths == null) { return null; } Boolean[] keepFlags = fieldLengths.getKeepPaddingFlags(); Boolean[] out = new Boolean[keepFlags.length]; Arrays.fill(out, getKeepPadding()); for (int i = 0; i < keepFlags.length; i++) { Boolean flag = keepFlags[i]; if (flag != null) { out[i] = flag; } } return out; }
private void init() { fields = new ArrayList<FixedWidthField>(); List<String> headers = new ArrayList<String>(); List<Integer> fieldWidth = new ArrayList<Integer>(); for (String tmp : fieldDescription.split(",")) { String[] fieldTuple = tmp.split(":(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1); FixedWidthField field = new FixedWidthField(); field.setName(fieldTuple[0]); field.setType(fieldTuple[1]); field.setWidth(Integer.parseInt(fieldTuple[2])); headers.add(fieldTuple[0]); fieldWidth.add(Integer.parseInt(fieldTuple[2])); if (field.getType() == FIELD_TYPE.DATE) { if (fieldTuple.length > 3) { field.setDateFormat(fieldTuple[3].replace("\"", "")); } else { logger.error("Date format is missing for the field {}", field.getName()); throw new RuntimeException("Missing date format"); } } fields.add(field); } header = headers.toArray(new String[headers.size()]); int[] width = Ints.toArray(fieldWidth); FixedWidthFields lengths = new FixedWidthFields(header, width); FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths); settings.getFormat().setPadding(this.padding); fixedWidthParser = new FixedWidthParser(settings); }
/** * Returns the sequence of paddings used by each field of each record. * * @return the sequence of paddings used by each field of each record. */ char[] getFieldPaddings() { if (fieldLengths == null) { return null; } return fieldLengths.getFieldPaddings(this.getFormat()); }
/** * The maximum number of characters allowed for any given value being written/read. Used to avoid OutOfMemoryErrors (defaults to a minimum of 4096 characters). * * <p> This overrides the parent implementation and calculates the absolute minimum number of characters required to store the values of a record * <p> If the sum of all field lengths is greater than the configured maximum number of characters per column, the calculated amount will be returned. * * @return The maximum number of characters allowed for any given value being written/read */ @Override public int getMaxCharsPerColumn() { int max = super.getMaxCharsPerColumn(); int minimum = 0; for (int length : calculateMaxFieldLengths()) { //adding 2 to give room for line breaks in every record (e.g. "\r\n"). minimum += length + 2; } return max > minimum ? max : minimum; }
//define field lengths FixedWidthFields fields = new FixedWidthFields(); accountFields.addField("ID", 10); accountFields.addField("Bank", 8); accountFields.addField("AccountNumber", 15); accountFields.addField("Swift", 12); //configure the parser FixedWidthParserSettings settings = new FixedWidthParserSettings(fields); //many options here, check the tutorial settings.getFormat().setLineSeparator("\n"); //We can now parse all rows FixedWidthParser parser = new FixedWidthParser(settings); List<String[]> rows = parser.parseAll(new File("/path/to/file.txt"));
/** * {@inheritDoc} */ @Override protected FixedWidthParserSettings createParserSettings() { return new FixedWidthParserSettings(createFixedWidthFields()); }
/** * Clones this configuration object to reuse most user-provided settings. This includes the fixed-width field configuration, * but doesn't include other input-specific settings. This method is meant to be used internally only. * * @return a copy of all configurations applied to the current instance. * * @deprecated doesn't really make sense for fixed-width. Use alternative method {@link #clone(FixedWidthFields)}. */ @Deprecated protected final FixedWidthParserSettings clone(boolean clearInputSpecificSettings) { return clone(clearInputSpecificSettings, fieldLengths == null ? null : fieldLengths.clone()); }
FixedWidthFieldLengths lengths = new FixedWidthFieldLengths(4, 5, 40, 40, 8); FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths); //sets the character used for padding unwritten spaces in the file settings.getFormat().setPadding('_'); // creates a fixed-width parser with the given settings FixedWidthParser parser = new FixedWidthParser(settings); // parses all rows in one go. List<String[]> allRows = parser.parseAll(new FileReader(yourFile));
/** * Function to create a univocity Parser */ private void createUnivocityParser() { List<FixedWidthSchema.Field> fields = schema.getFields(); FixedWidthFields fieldWidthFields = new FixedWidthFields(); for (int i = 0; i < fields.size(); i++) { FixedWidthSchema.Field currentField = fields.get(i); int fieldLength = currentField.getFieldLength(); FieldAlignment currentFieldAlignment; if (currentField.getAlignment().equalsIgnoreCase("centre")) { currentFieldAlignment = FieldAlignment.CENTER; } else if (currentField.getAlignment().equalsIgnoreCase("left")) { currentFieldAlignment = FieldAlignment.LEFT; } else { currentFieldAlignment = FieldAlignment.RIGHT; } fieldWidthFields.addField(currentField.getName(), fieldLength, currentFieldAlignment, currentField.getPadding()); } FixedWidthParserSettings settings = new FixedWidthParserSettings(fieldWidthFields); univocityParser = new com.univocity.parsers.fixed.FixedWidthParser(settings); }
// creates the sequence of field lengths in the file to be parsed FixedWidthFields fields = new FixedWidthFields(4, 5, 40, 40, 8); // creates the default settings for a fixed width parser FixedWidthParserSettings settings = new FixedWidthParserSettings(fields); // many settings here, check the tutorial. //sets the character used for padding unwritten spaces in the file settings.getFormat().setPadding('_'); // creates a fixed-width parser with the given settings FixedWidthParser parser = new FixedWidthParser(settings); // parses all rows in one go. List<String[]> allRows = parser.parseAll(new File("path/to/fixed.txt")));
/** * Function to create a univocity Parser */ private void createUnivocityParser() { List<FixedWidthSchema.Field> fields = schema.getFields(); FixedWidthFields fieldWidthFields = new FixedWidthFields(); for (int i = 0; i < fields.size(); i++) { FixedWidthSchema.Field currentField = fields.get(i); int fieldLength = currentField.getFieldLength(); FieldAlignment currentFieldAlignment; if (currentField.getAlignment().equalsIgnoreCase("centre")) { currentFieldAlignment = FieldAlignment.CENTER; } else if (currentField.getAlignment().equalsIgnoreCase("left")) { currentFieldAlignment = FieldAlignment.LEFT; } else { currentFieldAlignment = FieldAlignment.RIGHT; } fieldWidthFields.addField(currentField.getName(), fieldLength, currentFieldAlignment, currentField.getPadding()); } FixedWidthParserSettings settings = new FixedWidthParserSettings(fieldWidthFields); univocityParser = new com.univocity.parsers.fixed.FixedWidthParser(settings); }
Lookup[] getLookaheadFormats() { return Lookup.getLookupFormats(lookaheadFormats, getFormat()); }