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, 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, String, int)}- provides source and line of statement location.
28 * <li>{@link #inSource(String, 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 sourceText;
37 private final String sourceName;
39 DeclarationInTextSource(String sourceName, String sourceText) {
40 this.sourceName = sourceName;
41 this.sourceText = sourceText;
44 public String getSourceName() {
48 public String getSourceText() {
53 public StatementSource getStatementSource() {
54 return StatementSource.DECLARATION;
58 public abstract String toString();
60 public static final DeclarationInTextSource inSource(String sourceName, String sourceText) {
61 return new InSource(sourceName, sourceText);
64 public static final DeclarationInTextSource atLine(String sourceName, String sourceText, int line) {
65 return new AtLine(sourceName, sourceText, line);
68 public static final DeclarationInTextSource atPosition(String sourceName, String sourceText, int line, int position) {
69 return new AtPosition(sourceName, sourceText, line,position);
72 private static class InSource extends DeclarationInTextSource {
74 public InSource(String sourceName, String sourceText) {
75 super(sourceName, sourceText);
79 public String toString() {
80 return getSourceName();
85 private static class AtLine extends DeclarationInTextSource {
87 private final int line;
89 public AtLine(String sourceName, String sourceText, int line) {
90 super(sourceName, sourceText);
95 public String toString() {
96 return String.format("%s:%d", getSourceName(),line);
101 private static class AtPosition extends DeclarationInTextSource {
104 private int character;
106 public AtPosition(String sourceName, String sourceText, int line, int character) {
107 super(sourceName, sourceText);
109 this.character = character;
113 public String toString() {
114 return String.format("%s:%d:%d", getSourceName(),line,character);