2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.api;
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;
17 public class YangSyntaxErrorException extends YangParserException {
19 private static final long serialVersionUID = 2L;
21 private final SourceIdentifier sourceId;
22 private final int line;
23 private final int charPositionInLine;
25 public YangSyntaxErrorException(final @Nullable SourceIdentifier sourceId, final int line,
26 final int charPositionInLine, final String message) {
27 this(sourceId, line, charPositionInLine, message, null);
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;
35 this.charPositionInLine = charPositionInLine;
38 public final Optional<SourceIdentifier> getSource() {
39 return Optional.ofNullable(sourceId);
42 public final int getLine() {
46 public final int getCharPositionInLine() {
47 return charPositionInLine;
50 public @NonNull String getFormattedMessage() {
51 final StringBuilder sb = new StringBuilder(getMessage());
52 if (sourceId != null) {
53 sb.append(" in source ").append(sourceId);
56 sb.append(" on line ").append(line);
57 if (charPositionInLine != 0) {
58 sb.append(" character ").append(charPositionInLine);
65 public String toString() {
66 return this.getClass().getName() + ": " + getFormattedMessage();