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