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(final 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 DeclarationInTextSource inSource(final String sourceName) {
55 return new InSource(sourceName);
58 public static DeclarationInTextSource atLine(final String sourceName, final int line) {
59 return new AtLine(sourceName, line);
62 public static DeclarationInTextSource atPosition(final String sourceName, final int line, final int position) {
63 return new AtPosition(sourceName, line,position);
66 private static class InSource extends DeclarationInTextSource {
67 InSource(final String sourceName) {
72 public String toString() {
73 return getSourceName();
77 private static class AtLine extends InSource {
79 private final int line;
81 AtLine(final String sourceName, final int line) {
87 public String toString() {
88 return getSourceName() + ':' + line;
91 public int getLine() {
96 private static class AtPosition extends AtLine {
98 private final int character;
100 AtPosition(final String sourceName, final int line, final int character) {
101 super(sourceName, line);
102 this.character = character;
106 public String toString() {
107 return getSourceName() + ':' + getLine() + ':' + character;