Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-api / src / main / java / org / opendaylight / yangtools / yang / model / parser / api / YangSyntaxErrorException.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.parser.api;
9
10 import java.util.Optional;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
14
15 public class YangSyntaxErrorException extends YangParserException {
16     private static final long serialVersionUID = 2L;
17
18     private final SourceIdentifier source;
19     private final int line;
20     private final int charPositionInLine;
21
22     public YangSyntaxErrorException(final @Nullable SourceIdentifier source, final int line,
23             final int charPositionInLine, final String message) {
24         this(source, line, charPositionInLine, message, null);
25     }
26
27     public YangSyntaxErrorException(final @Nullable SourceIdentifier source, final int line,
28             final int charPositionInLine, final String message, final @Nullable Throwable cause) {
29         super(message, cause);
30         this.source = source;
31         this.line = line;
32         this.charPositionInLine = charPositionInLine;
33     }
34
35     public final Optional<SourceIdentifier> getSource() {
36         return Optional.ofNullable(source);
37     }
38
39     public final int getLine() {
40         return line;
41     }
42
43     public final int getCharPositionInLine() {
44         return charPositionInLine;
45     }
46
47     public @NonNull String getFormattedMessage() {
48         final StringBuilder sb = new StringBuilder(getMessage());
49         if (source != null) {
50             sb.append(" in source ");
51             sb.append(source);
52         }
53         if (line != 0) {
54             sb.append(" on line ");
55             sb.append(line);
56             if (charPositionInLine != 0) {
57                 sb.append(" character ");
58                 sb.append(charPositionInLine);
59             }
60         }
61         return sb.toString();
62     }
63
64     @Override
65     public String toString() {
66         return this.getClass().getName() + ": " + getFormattedMessage();
67     }
68 }