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  public class MarkedYAMLException extends YAMLException {
20  
21      private static final long serialVersionUID = -9119388488683035101L;
22      private String context;
23      private Mark contextMark;
24      private String problem;
25      private Mark problemMark;
26      private String note;
27  
28      protected MarkedYAMLException(String context, Mark contextMark, String problem,
29              Mark problemMark, String note) {
30          this(context, contextMark, problem, problemMark, note, null);
31      }
32  
33      protected MarkedYAMLException(String context, Mark contextMark, String problem,
34              Mark problemMark, String note, Throwable cause) {
35          super(context + "; " + problem, cause);
36          this.context = context;
37          this.contextMark = contextMark;
38          this.problem = problem;
39          this.problemMark = problemMark;
40          this.note = note;
41      }
42  
43      protected MarkedYAMLException(String context, Mark contextMark, String problem, Mark problemMark) {
44          this(context, contextMark, problem, problemMark, null, null);
45      }
46  
47      protected MarkedYAMLException(String context, Mark contextMark, String problem,
48              Mark problemMark, Throwable cause) {
49          this(context, contextMark, problem, problemMark, null, cause);
50      }
51  
52      @Override
53      public String toString() {
54          StringBuilder lines = new StringBuilder();
55          if (context != null) {
56              lines.append(context);
57              lines.append("\n");
58          }
59          if (contextMark != null
60                  && (problem == null || problemMark == null
61                          || (contextMark.getName() != problemMark.getName())
62                          || (contextMark.getLine() != problemMark.getLine()) || (contextMark
63                          .getColumn() != problemMark.getColumn()))) {
64              lines.append(contextMark.toString());
65              lines.append("\n");
66          }
67          if (problem != null) {
68              lines.append(problem);
69              lines.append("\n");
70          }
71          if (problemMark != null) {
72              lines.append(problemMark.toString());
73              lines.append("\n");
74          }
75          if (note != null) {
76              lines.append(note);
77              lines.append("\n");
78          }
79          return lines.toString();
80      }
81  
82      public String getContext() {
83          return context;
84      }
85  
86      public Mark getContextMark() {
87          return contextMark;
88      }
89  
90      public String getProblem() {
91          return problem;
92      }
93  
94      public Mark getProblemMark() {
95          return problemMark;
96      }
97  }