/** * Adds the length of the next field in a fixed-width record. This method can be chained like this: addField("field_1", 5).addField("field_2", 6)... * * @param name the name of the next field. It is not validated. * @param length the length of the next field. It must be greater than 0. * @param padding the representation of unused space in this field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(String name, int length, char padding) { return addField(name, length, FieldAlignment.LEFT, padding); }
/** * Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)... * * @param length the length of the next field. It must be greater than 0. * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int length) { return addField(null, length, FieldAlignment.LEFT, '\0'); }
/** * Creates a new instance initialized with the lengths of all fields in a fixed-width record. * * @param fieldLengths The number lengths of all fields in a fixed-width record. All lengths must be greater than 0. */ public FixedWidthFields(int... fieldLengths) { for (int i = 0; i < fieldLengths.length; i++) { addField(fieldLengths[i]); } }
/** * Adds the length of the next field in a fixed-width record. This method can be chained like this: addField("field_1", 5).addField("field_2", 6)... * * @param name the name of the next field. It is not validated. * @param length the length of the next field. It must be greater than 0. * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(String name, int length) { return addField(name, length, FieldAlignment.LEFT, '\0'); }
/** * Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)... * * @param length the length of the next field. It must be greater than 0. * @param padding the representation of unused space in this field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int length, char padding) { return addField(null, length, FieldAlignment.LEFT, padding); }
/** * Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)... * * @param length the length of the next field. It must be greater than 0. * @param alignment the alignment of the field * @param padding the representation of unused space in this field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int length, FieldAlignment alignment, char padding) { return addField(null, length, alignment, padding); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param name the name of the next field. It is not validated. * @param startPosition starting position of the field. * @param endPosition ending position of the field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(String name, int startPosition, int endPosition) { return addField(name, startPosition, endPosition, FieldAlignment.LEFT, '\0'); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param startPosition starting position of the field. * @param endPosition ending position of the field * @param alignment the alignment of the field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int startPosition, int endPosition, FieldAlignment alignment) { return addField(null, startPosition, endPosition, alignment, '\0'); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param startPosition starting position of the field. * @param endPosition ending position of the field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int startPosition, int endPosition) { return addField(null, startPosition, endPosition, FieldAlignment.LEFT, '\0'); }
/** * Adds the length of the next field in a fixed-width record. This method can be chained like this: addField(5).addField(6)... * * @param length the length of the next field. It must be greater than 0. * @param alignment the alignment of the field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int length, FieldAlignment alignment) { return addField(null, length, alignment, '\0'); }
/** * Adds the length of the next field in a fixed-width record. This method can be chained like this: addField("field_1", 5).addField("field_2", 6)... * * @param name the name of the next field. It is not validated. * @param length the length of the next field. It must be greater than 0. * @param alignment the alignment of the field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(String name, int length, FieldAlignment alignment) { return addField(name, length, alignment, '\0'); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param name the name of the next field. It is not validated. * @param startPosition starting position of the field. * @param endPosition ending position of the field * @param alignment the alignment of the field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(String name, int startPosition, int endPosition, FieldAlignment alignment) { return addField(name, startPosition, endPosition, alignment, '\0'); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param startPosition starting position of the field. * @param endPosition ending position of the field * @param padding the representation of unused space in this field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int startPosition, int endPosition, char padding) { return addField(null, startPosition, endPosition, FieldAlignment.LEFT, padding); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param name the name of the next field. It is not validated. * @param startPosition starting position of the field. * @param endPosition ending position of the field * @param padding the representation of unused space in this field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(String name, int startPosition, int endPosition, char padding) { return addField(name, startPosition, endPosition, FieldAlignment.LEFT, padding); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param startPosition starting position of the field. * @param endPosition ending position of the field * @param alignment the alignment of the field * @param padding the representation of unused space in this field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(int startPosition, int endPosition, FieldAlignment alignment, char padding) { return addField(null, startPosition, endPosition, alignment, padding); }
/** * Adds the range of the next field in a fixed-width record. The given range cannot overlap with previously defined fields. * Blanks will be used to fill any "gap" between record ranges when writing. * * @param name the name of the next field. It is not validated. * @param startPosition starting position of the field. * @param endPosition ending position of the field * @param alignment the alignment of the field * @param padding the representation of unused space in this field * * @return the FixedWidthFields instance itself for chaining. */ public FixedWidthFields addField(String name, int startPosition, int endPosition, FieldAlignment alignment, char padding) { int length = endPosition - startPosition; if (startPosition < totalLength) { throw new IllegalArgumentException("Start position '" + startPosition + "' overlaps with one or more fields"); } else if (startPosition > totalLength) { addField(null, startPosition - totalLength, FieldAlignment.LEFT, '\0'); fieldsToIgnore.set(fieldsToIgnore.size() - 1, Boolean.TRUE); } return addField(name, length, alignment, padding); }
/** * Defines a sequence of field names used to refer to columns in the input/output text of an entity, along with their lengths. * The field names defined will be used as headers, having the same effect of a call to {@link FixedWidthParserSettings#setHeaders(String...)}. * * @param fields a {@link LinkedHashMap} containing the sequence of fields to be associated with each column in the input/output, with their respective length. */ public FixedWidthFields(LinkedHashMap<String, Integer> fields) { if (fields == null || fields.isEmpty()) { throw new IllegalArgumentException("Map of fields and their lengths cannot be null/empty"); } for (Entry<String, Integer> entry : fields.entrySet()) { String fieldName = entry.getKey(); Integer fieldLength = entry.getValue(); addField(fieldName, fieldLength); } }
/** * Defines a sequence of field names used to refer to columns in the input/output text of an entity, along with their lengths. * The field names defined will be used as headers, having the same effect of a call to {@link FixedWidthParserSettings#setHeaders(String...)}. * * @param headers the sequence of fields to be associated with each column in the input/output * @param lengths the sequence of lengths to be associated with each given header. The size of this array must match the number of given headers. */ public FixedWidthFields(String[] headers, int[] lengths) { if (headers == null || headers.length == 0) { throw new IllegalArgumentException("Headers cannot be null/empty"); } if (lengths == null || lengths.length == 0) { throw new IllegalArgumentException("Field lengths cannot be null/empty"); } if (headers.length != lengths.length) { throw new IllegalArgumentException("Sequence of headers and their respective lengths must match. Got " + headers.length + " headers but " + lengths.length + " lengths"); } for (int i = 0; i < headers.length; i++) { addField(headers[i], lengths[i]); } }
/** * 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); }
/** * 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); }