private static String createSourceCodeErrorMessage()

in freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/CJSONInterpreter.java [2028:2131]


    private static String createSourceCodeErrorMessage(
            String message, String srcCode, int position, String fileName,
            int maxQuotLength) {
        int ln = srcCode.length();
        if (position < 0) {
            position = 0;
        }
        if (position >= ln) {
            if (position == ln) {
                return message + LINE_BREAK
                        + "Error location: The very end of "
                        + (fileName == null ? "the text" : fileName)
                        + ".";
            } else {
                return message + LINE_BREAK
                        + "Error location: ??? (after the end of "
                        + (fileName == null ? "the text" : fileName)
                        + ")";
            }
        }
            
        int i;
        char c;
        int rowBegin = 0;
        int rowEnd;
        int row = 1;
        char lastChar = 0;
        for (i = 0; i <= position; i++) {
            c = srcCode.charAt(i);
            if (lastChar == 0xA) {
                rowBegin = i;
                row++;
            } else if (lastChar == 0xD && c != 0xA) {
                rowBegin = i;
                row++;
            }
            lastChar = c;
        }
        for (i = position; i < ln; i++) {
            c = srcCode.charAt(i);
            if (c == 0xA || c == 0xD) {
                if (c == 0xA && i > 0 && srcCode.charAt(i - 1) == 0xD) {
                    i--;
                }
                break;
            }
        }
        rowEnd = i - 1;
        if (position > rowEnd + 1) {
            position = rowEnd + 1;
        }
        int col = position - rowBegin + 1;
        if (rowBegin > rowEnd) {
            return message + LINE_BREAK
                    + "Error location: line "
                    + row + ", column " + col
                    + (fileName == null ? ":" : " in " + fileName + ":")
                    + LINE_BREAK
                    + "(Can't show the line because it is empty.)";
        }
        String s1 = srcCode.substring(rowBegin, position);
        String s2 = srcCode.substring(position, rowEnd + 1);
        s1 = expandTabs(s1, 8);
        int ln1 = s1.length();
        s2 = expandTabs(s2, 8, ln1);
        int ln2 = s2.length();
        if (ln1 + ln2 > maxQuotLength) {
            int newLn2 = ln2 - ((ln1 + ln2) - maxQuotLength);
            if (newLn2 < 6) {
                newLn2 = 6;
            }
            if (newLn2 < ln2) {
                s2 = s2.substring(0, newLn2 - 3) + "...";
                ln2 = newLn2;
            }
            if (ln1 + ln2 > maxQuotLength) {
                s1 = "..." + s1.substring((ln1 + ln2) - maxQuotLength + 3);
            }
        }
        StringBuilder res = new StringBuilder(message.length() + 80);
        res.append(message);
        res.append(LINE_BREAK);
        res.append("Error location: line ");
        res.append(row);
        res.append(", column ");
        res.append(col);
        if (fileName != null) {
            res.append(" in ");
            res.append(fileName);
        }
        res.append(":");
        res.append(LINE_BREAK);
        res.append(s1);
        res.append(s2);
        res.append(LINE_BREAK);
        int x = s1.length();
        while (x != 0) {
            res.append(' ');
            x--;
        }
        res.append('^');
            
        return res.toString();
    }