Coverage Report - org.yaml.snakeyaml.error.Mark
 
Classes in this File Line Coverage Branch Coverage Complexity
Mark
98%
55/56
90%
18/20
2.222
 
 1  
 /**
 2  
  * Copyright (c) 2008-2012, 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  2231458
         super();
 34  2231458
         this.name = name;
 35  2231458
         this.index = index;
 36  2231458
         this.line = line;
 37  2231458
         this.column = column;
 38  2231458
         this.buffer = buffer;
 39  2231458
         this.pointer = pointer;
 40  2231458
     }
 41  
 
 42  
     private boolean isLineBreak(char ch) {
 43  4587
         return Constant.NULL_OR_LINEBR.has(ch);
 44  
     }
 45  
 
 46  
     public String get_snippet(int indent, int max_length) {
 47  230
         if (buffer == null) {
 48  0
             return null;
 49  
         }
 50  230
         float half = max_length / 2 - 1;
 51  230
         int start = pointer;
 52  230
         String head = "";
 53  1514
         while ((start > 0) && (!isLineBreak(buffer.charAt(start - 1)))) {
 54  1292
             start -= 1;
 55  1292
             if (pointer - start > half) {
 56  8
                 head = " ... ";
 57  8
                 start += 5;
 58  8
                 break;
 59  
             }
 60  
         }
 61  230
         String tail = "";
 62  230
         int end = pointer;
 63  3258
         while ((end < buffer.length()) && (!isLineBreak(buffer.charAt(end)))) {
 64  3058
             end += 1;
 65  3058
             if (end - pointer > half) {
 66  30
                 tail = " ... ";
 67  30
                 end -= 5;
 68  30
                 break;
 69  
             }
 70  
         }
 71  230
         String snippet = buffer.substring(start, end);
 72  230
         StringBuilder result = new StringBuilder();
 73  1124
         for (int i = 0; i < indent; i++) {
 74  894
             result.append(" ");
 75  
         }
 76  230
         result.append(head);
 77  230
         result.append(snippet);
 78  230
         result.append(tail);
 79  230
         result.append("\n");
 80  2416
         for (int i = 0; i < indent + pointer - start + head.length(); i++) {
 81  2186
             result.append(" ");
 82  
         }
 83  230
         result.append("^");
 84  230
         return result.toString();
 85  
     }
 86  
 
 87  
     public String get_snippet() {
 88  217
         return get_snippet(4, 75);
 89  
     }
 90  
 
 91  
     @Override
 92  
     public String toString() {
 93  214
         String snippet = get_snippet();
 94  214
         StringBuilder where = new StringBuilder(" in ");
 95  214
         where.append(name);
 96  214
         where.append(", line ");
 97  214
         where.append(line + 1);
 98  214
         where.append(", column ");
 99  214
         where.append(column + 1);
 100  214
         if (snippet != null) {
 101  214
             where.append(":\n");
 102  214
             where.append(snippet);
 103  
         }
 104  214
         return where.toString();
 105  
     }
 106  
 
 107  
     public String getName() {
 108  19
         return name;
 109  
     }
 110  
 
 111  
     /**
 112  
      * starts with 0
 113  
      */
 114  
     public int getLine() {
 115  2
         return line;
 116  
     }
 117  
 
 118  
     /**
 119  
      * starts with 0
 120  
      */
 121  
     public int getColumn() {
 122  2
         return column;
 123  
     }
 124  
 
 125  
     /**
 126  
      * starts with 0
 127  
      */
 128  
     public int getIndex() {
 129  1
         return index;
 130  
     }
 131  
 
 132  
 }