View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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
    17  package org.yaml.snakeyaml.error;
    18
    19  import org.yaml.snakeyaml.scanner.Constant;
    20
    21  /**
    22   * It's just a record and its only use is producing nice error messages. Parser
    23   * does not use it for any other purposes.
    24   */
    25  public final class Mark {
    26      private String name;
    27      private int index;
    28      private int line;
    29      private int column;
    30      private String buffer;
    31      private int pointer;
    32
    33      public Mark(String name, int index, int line, int column, String buffer, int pointer) {
    34          super();
    35          this.name = name;
    36          this.index = index;
    37          this.line = line;
    38          this.column = column;
    39          this.buffer = buffer;
    40          this.pointer = pointer;
    41      }
    42
    43      private boolean isLineBreak(char ch) {
    44          return Constant.NULL_OR_LINEBR.has(ch);
    45      }
    46
    47      public String get_snippet(int indent, int max_length) {
    48          if (buffer == null) {
    49              return null;
    50          }
    51          float half = max_length / 2 - 1;
    52          int start = pointer;
    53          String head = "";
    54          while ((start > 0) && (!isLineBreak(buffer.charAt(start - 1)))) {
    55              start -= 1;
    56              if (pointer - start > half) {
    57                  head = " ... ";
    58                  start += 5;
    59                  break;
    60              }
    61          }
    62          String tail = "";
    63          int end = pointer;
    64          while ((end < buffer.length()) && (!isLineBreak(buffer.charAt(end)))) {
    65              end += 1;
    66              if (end - pointer > half) {
    67                  tail = " ... ";
    68                  end -= 5;
    69                  break;
    70              }
    71          }
    72          String snippet = buffer.substring(start, end);
    73          StringBuilder result = new StringBuilder();
    74          for (int i = 0; i < indent; i++) {
    75              result.append(" ");
    76          }
    77          result.append(head);
    78          result.append(snippet);
    79          result.append(tail);
    80          result.append("\n");
    81          for (int i = 0; i < indent + pointer - start + head.length(); i++) {
    82              result.append(" ");
    83          }
    84          result.append("^");
    85          return result.toString();
    86      }
    87
    88      public String get_snippet() {
    89          return get_snippet(4, 75);
    90      }
    91
    92      @Override
    93      public String toString() {
    94          String snippet = get_snippet();
    95          StringBuilder where = new StringBuilder(" in \"");
    96          where.append(name);
    97          where.append("\", line ");
    98          where.append(line + 1);
    99          where.append(", column ");
    100         where.append(column + 1);
    101         if (snippet != null) {
    102             where.append(":\n");
    103             where.append(snippet);
    104         }
    105         return where.toString();
    106     }
    107
    108     public String getName() {
    109         return name;
    110     }
    111
    112     /**
    113      * starts with 0
    114      */
    115     public int getLine() {
    116         return line;
    117     }
    118
    119     /**
    120      * starts with 0
    121      */
    122     public int getColumn() {
    123         return column;
    124     }
    125
    126     /**
    127      * starts with 0
    128      */
    129     public int getIndex() {
    130         return index;
    131     }
    132
    133 }