2 * Copyright (c) 2015 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.spi.source;
10 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
14 * Reference of statement source present in textual source format
16 * Utility implementation of {@link StatementSourceReference} for textual sources,
17 * this is prefered {@link StatementSourceReference} for implementations
18 * of YANG / YIN statement stream sources.
21 * To create source reference use one of this static factories:
23 * <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location,
24 * this is most prefered since it provides most context to debug YANG model.
26 * <li>{@link #atLine(String, int)}- provides source and line of statement location.
28 * <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous
29 * references are unable to create / derive from source.
34 public abstract class DeclarationInTextSource implements StatementSourceReference {
36 private final String sourceName;
38 DeclarationInTextSource(String sourceName) {
39 this.sourceName = sourceName;
42 public String getSourceName() {
47 public StatementSource getStatementSource() {
48 return StatementSource.DECLARATION;
52 public abstract String toString();
54 public static final DeclarationInTextSource inSource(String sourceName) {
55 return new InSource(sourceName);
58 public static final DeclarationInTextSource atLine(String sourceName, int line) {
59 return new AtLine(sourceName, line);
62 public static final DeclarationInTextSource atPosition(String sourceName, int line, int position) {
63 return new AtPosition(sourceName, line,position);
66 private static class InSource extends DeclarationInTextSource {
68 InSource(String sourceName) {
73 public String toString() {
74 return getSourceName();
79 private static class AtLine extends InSource {
81 private final int line;
83 AtLine(String sourceName, int line) {
89 public String toString() {
90 return String.format("%s:%d", getSourceName(),line);
93 public int getLine() {
99 private static class AtPosition extends AtLine {
101 private final int character;
103 AtPosition(String sourceName, int line, int character) {
104 super(sourceName, line);
105 this.character = character;
109 public String toString() {
110 return String.format("%s:%d:%d", getSourceName(),getLine(),character);