Home | History | Annotate | Download | only in error
      1 /**
      2  * Copyright (c) 2008, http://www.snakeyaml.org
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.yaml.snakeyaml.error;
     17 
     18 import org.yaml.snakeyaml.scanner.Constant;
     19 
     20 /**
     21  * It's just a record and its only use is producing nice error messages. Parser
     22  * does not use it for any other purposes.
     23  */
     24 public final class Mark {
     25     private String name;
     26     private int index;
     27     private int line;
     28     private int column;
     29     private String buffer;
     30     private int pointer;
     31 
     32     public Mark(String name, int index, int line, int column, String buffer, int pointer) {
     33         super();
     34         this.name = name;
     35         this.index = index;
     36         this.line = line;
     37         this.column = column;
     38         this.buffer = buffer;
     39         this.pointer = pointer;
     40     }
     41 
     42     private boolean isLineBreak(char ch) {
     43         return Constant.NULL_OR_LINEBR.has(ch);
     44     }
     45 
     46     public String get_snippet(int indent, int max_length) {
     47         if (buffer == null) {
     48             return null;
     49         }
     50         float half = max_length / 2 - 1;
     51         int start = pointer;
     52         String head = "";
     53         while ((start > 0) && !isLineBreak(buffer.charAt(start - 1))) {
     54             start -= 1;
     55             if (pointer - start > half) {
     56                 head = " ... ";
     57                 start += 5;
     58                 break;
     59             }
     60         }
     61         String tail = "";
     62         int end = pointer;
     63         while ((end < buffer.length()) && !isLineBreak(buffer.charAt(end))) {
     64             end += 1;
     65             if (end - pointer > half) {
     66                 tail = " ... ";
     67                 end -= 5;
     68                 break;
     69             }
     70         }
     71         String snippet = buffer.substring(start, end);
     72         StringBuilder result = new StringBuilder();
     73         for (int i = 0; i < indent; i++) {
     74             result.append(" ");
     75         }
     76         result.append(head);
     77         result.append(snippet);
     78         result.append(tail);
     79         result.append("\n");
     80         for (int i = 0; i < indent + pointer - start + head.length(); i++) {
     81             result.append(" ");
     82         }
     83         result.append("^");
     84         return result.toString();
     85     }
     86 
     87     public String get_snippet() {
     88         return get_snippet(4, 75);
     89     }
     90 
     91     @Override
     92     public String toString() {
     93         String snippet = get_snippet();
     94         StringBuilder where = new StringBuilder(" in ");
     95         where.append(name);
     96         where.append(", line ");
     97         where.append(line + 1);
     98         where.append(", column ");
     99         where.append(column + 1);
    100         if (snippet != null) {
    101             where.append(":\n");
    102             where.append(snippet);
    103         }
    104         return where.toString();
    105     }
    106 
    107     public String getName() {
    108         return name;
    109     }
    110 
    111     /**
    112      * starts with 0
    113      */
    114     public int getLine() {
    115         return line;
    116     }
    117 
    118     /**
    119      * starts with 0
    120      */
    121     public int getColumn() {
    122         return column;
    123     }
    124 
    125     /**
    126      * starts with 0
    127      */
    128     public int getIndex() {
    129         return index;
    130     }
    131 
    132 }
    133