Update yang-parser-api
[yangtools.git] / yang / yang-parser-api / src / main / java / org / opendaylight / yangtools / yang / 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.parser.api;
9
10 import com.google.common.annotations.Beta;
11 import java.util.Optional;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
15
16 @Beta
17 public class YangSyntaxErrorException extends YangParserException {
18     private static final long serialVersionUID = 2L;
19
20     private final SourceIdentifier source;
21     private final int line;
22     private final int charPositionInLine;
23
24     public YangSyntaxErrorException(final @Nullable SourceIdentifier source, final int line,
25             final int charPositionInLine, final String message) {
26         this(source, line, charPositionInLine, message, null);
27     }
28
29     public YangSyntaxErrorException(final @Nullable SourceIdentifier source, final int line,
30             final int charPositionInLine, final String message, final @Nullable Throwable cause) {
31         super(message, cause);
32         this.source = source;
33         this.line = line;
34         this.charPositionInLine = charPositionInLine;
35     }
36
37     public final Optional<SourceIdentifier> getSource() {
38         return Optional.ofNullable(source);
39     }
40
41     public final int getLine() {
42         return line;
43     }
44
45     public final int getCharPositionInLine() {
46         return charPositionInLine;
47     }
48
49     public @NonNull String getFormattedMessage() {
50         final StringBuilder sb = new StringBuilder(getMessage());
51         if (source != null) {
52             sb.append(" in source ").append(source);
53         }
54         if (line != 0) {
55             sb.append(" on line ").append(line);
56             if (charPositionInLine != 0) {
57                 sb.append(" character ").append(charPositionInLine);
58             }
59         }
60         return sb.toString();
61     }
62
63     @Override
64     public String toString() {
65         return this.getClass().getName() + ": " + getFormattedMessage();
66     }
67 }