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-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  1579298
         super();
 35  1579298
         this.name = name;
 36  1579298
         this.index = index;
 37  1579298
         this.line = line;
 38  1579298
         this.column = column;
 39  1579298
         this.buffer = buffer;
 40  1579298
         this.pointer = pointer;
 41  1579298
     }
 42  
 
 43  
     private boolean isLineBreak(char ch) {
 44  784
         return Constant.NULL_OR_LINEBR.has(ch);
 45  
     }
 46  
 
 47  
     public String get_snippet(int indent, int max_length) {
 48  36
         if (buffer == null) {
 49  0
             return null;
 50  
         }
 51  36
         float half = max_length / 2 - 1;
 52  36
         int start = pointer;
 53  36
         String head = "";
 54  325
         while ((start > 0) && (!isLineBreak(buffer.charAt(start - 1)))) {
 55  292
             start -= 1;
 56  292
             if (pointer - start > half) {
 57  3
                 head = " ... ";
 58  3
                 start += 5;
 59  3
                 break;
 60  
             }
 61  
         }
 62  36
         String tail = "";
 63  36
         int end = pointer;
 64  483
         while ((end < buffer.length()) && (!isLineBreak(buffer.charAt(end)))) {
 65  450
             end += 1;
 66  450
             if (end - pointer > half) {
 67  3
                 tail = " ... ";
 68  3
                 end -= 5;
 69  3
                 break;
 70  
             }
 71  
         }
 72  36
         String snippet = buffer.substring(start, end);
 73  36
         StringBuilder result = new StringBuilder();
 74  154
         for (int i = 0; i < indent; i++) {
 75  118
             result.append(" ");
 76  
         }
 77  36
         result.append(head);
 78  36
         result.append(snippet);
 79  36
         result.append(tail);
 80  36
         result.append("\n");
 81  446
         for (int i = 0; i < indent + pointer - start + head.length(); i++) {
 82  410
             result.append(" ");
 83  
         }
 84  36
         result.append("^");
 85  36
         return result.toString();
 86  
     }
 87  
 
 88  
     public String get_snippet() {
 89  23
         return get_snippet(4, 75);
 90  
     }
 91  
 
 92  
     @Override
 93  
     public String toString() {
 94  20
         String snippet = get_snippet();
 95  20
         StringBuilder where = new StringBuilder(" in \"");
 96  20
         where.append(name);
 97  20
         where.append("\", line ");
 98  20
         where.append(line + 1);
 99  20
         where.append(", column ");
 100  20
         where.append(column + 1);
 101  20
         if (snippet != null) {
 102  20
             where.append(":\n");
 103  20
             where.append(snippet);
 104  
         }
 105  20
         return where.toString();
 106  
     }
 107  
 
 108  
     public String getName() {
 109  19
         return name;
 110  
     }
 111  
 
 112  
     /**
 113  
      * starts with 0
 114  
      */
 115  
     public int getLine() {
 116  20
         return line;
 117  
     }
 118  
 
 119  
     /**
 120  
      * starts with 0
 121  
      */
 122  
     public int getColumn() {
 123  12
         return column;
 124  
     }
 125  
 
 126  
     /**
 127  
      * starts with 0
 128  
      */
 129  
     public int getIndex() {
 130  1
         return index;
 131  
     }
 132  
 
 133  
 }