aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/yaml/snakeyaml/error/Mark.java
blob: 367ee6bb1789bc7a45deb4857de8ca412cea6045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.error;

import java.io.Serializable;
import org.yaml.snakeyaml.scanner.Constant;

/**
 * It's just a record and its only use is producing nice error messages. Parser does not use it for
 * any other purposes.
 */
public final class Mark implements Serializable {

  private final String name;
  private final int index;
  private final int line;
  private final int column;
  private final int[] buffer;
  private final int pointer;

  private static int[] toCodePoints(char[] str) {
    int[] codePoints = new int[Character.codePointCount(str, 0, str.length)];
    for (int i = 0, c = 0; i < str.length; c++) {
      int cp = Character.codePointAt(str, i);
      codePoints[c] = cp;
      i += Character.charCount(cp);
    }
    return codePoints;
  }

  public Mark(String name, int index, int line, int column, char[] str, int pointer) {
    this(name, index, line, column, toCodePoints(str), pointer);
  }

  /*
   * Existed in older versions but replaced with {@code char[]}-based constructor. Restored in v1.22
   * for backwards compatibility.
   *
   * @deprecated Since restored in v1.22. Use {@link Mark#Mark(String, int, int, int, char[], int)}.
   */
  @Deprecated
  public Mark(String name, int index, int line, int column, String buffer, int pointer) {
    this(name, index, line, column, buffer.toCharArray(), pointer);
  }

  public Mark(String name, int index, int line, int column, int[] buffer, int pointer) {
    super();
    this.name = name;
    this.index = index;
    this.line = line;
    this.column = column;
    this.buffer = buffer;
    this.pointer = pointer;
  }

  private boolean isLineBreak(int c) {
    return Constant.NULL_OR_LINEBR.has(c);
  }

  public String get_snippet(int indent, int max_length) {
    float half = max_length / 2f - 1f;
    int start = pointer;
    String head = "";
    while ((start > 0) && !isLineBreak(buffer[start - 1])) {
      start -= 1;
      if (pointer - start > half) {
        head = " ... ";
        start += 5;
        break;
      }
    }
    String tail = "";
    int end = pointer;
    while ((end < buffer.length) && !isLineBreak(buffer[end])) {
      end += 1;
      if (end - pointer > half) {
        tail = " ... ";
        end -= 5;
        break;
      }
    }

    StringBuilder result = new StringBuilder();
    for (int i = 0; i < indent; i++) {
      result.append(" ");
    }
    result.append(head);
    for (int i = start; i < end; i++) {
      result.appendCodePoint(buffer[i]);
    }
    result.append(tail);
    result.append("\n");
    for (int i = 0; i < indent + pointer - start + head.length(); i++) {
      result.append(" ");
    }
    result.append("^");
    return result.toString();
  }

  public String get_snippet() {
    return get_snippet(4, 75);
  }

  @Override
  public String toString() {
    String snippet = get_snippet();
    String builder =
        " in " + name + ", line " + (line + 1) + ", column " + (column + 1) + ":\n" + snippet;
    return builder;
  }

  public String getName() {
    return name;
  }

  /**
   * starts with 0
   *
   * @return line number
   */
  public int getLine() {
    return line;
  }

  /**
   * starts with 0
   *
   * @return column number
   */
  public int getColumn() {
    return column;
  }

  /**
   * starts with 0
   *
   * @return character number
   */
  public int getIndex() {
    return index;
  }

  public int[] getBuffer() {
    return buffer;
  }

  public int getPointer() {
    return pointer;
  }
}