All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.csveed.report.RowReport Maven / Gradle / Ivy

Go to download

Java toolkit for mapping CSV-to-Bean mapping and vice versa. Redirection to Introduction in 5 seconds!

There is a newer version: 0.8.2
Show newest version
package org.csveed.report;

import java.util.ArrayList;
import java.util.List;

public class RowReport {

    private String row;

    private int start;

    private int end;

    public RowReport(String row, int start, int end) {
        this.row = row;
        this.start = start;
        this.end = start == end && start < row.length() ? start + 1 : end;
    }

    public String getRow() {
        return row;
    }

    public int getStart() {
        return start;
    }

    public int getEnd() {
        return end;
    }

    public List tokenize() {
        List lines = new ArrayList();
        if (start > 0) {
            lines.add(new RowPart(row.substring(0, start), false));
        }
        if (end - start > 0) {
            lines.add(new RowPart(row.substring(start, end), true));
        }
        if (end < row.length()) {
            lines.add(new RowPart(row.substring(end), false));
        }
        return lines;
    }

    public List getPrintableLines() {
        List lines = new ArrayList();

        List parts = tokenize();

        lines.add(createContentLine(parts));
        lines.add(createFocusLine(parts));

        return lines;
    }

    private String createContentLine(List parts) {
        StringBuilder line = new StringBuilder();
        for (RowPart token : parts) {
            line.append(token.getToken());
        }
        return line.toString();
    }

    private String createFocusLine(List parts) {
        StringBuilder line = new StringBuilder();
        boolean placedMarkers = false;
        for (RowPart token : parts) {
            if (token.isHighlight()) {
                line.append(printUnderscoredPart(token));
                placedMarkers = true;
            } else {
                line.append(printEmptyPart(token));
            }
        }
        if (!placedMarkers) { // Essentially only at the end-of-line
            line.append('^');
        }
        return line.toString();
    }

    private String printEmptyPart(RowPart token) {
        StringBuilder linePart = new StringBuilder();
        for (int i = 0; i < token.getToken().length(); i++) {
            linePart.append(' ');
        }
        return linePart.toString();
    }

    private String printUnderscoredPart(RowPart token) {
        StringBuilder linePart = new StringBuilder();
        for (int i = 0; i < token.getToken().length(); i++) {
            if (i == 0 || i == token.getToken().length() - 1) {
                linePart.append('^');
            } else {
                linePart.append('-');
            }
        }
        return linePart.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy