Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / source / DeclarationInTextSource.java
1 /*
2  * Copyright (c) 2015 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.spi.source;
9
10 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
11
12 /**
13  * Reference of statement source present in textual source format. Utility implementation
14  * of {@link StatementSourceReference} for textual sources, this is prefered {@link StatementSourceReference}
15  * for implementations of YANG / YIN statement stream sources.
16  *
17  * <p>
18  * To create source reference use one of this static factories:
19  * <ul>
20  * <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location,
21  * this is most prefered since it provides most context to debug YANG model.
22  * </li>
23  * <li>{@link #atLine(String, int)}- provides source and line of statement location.
24  * </li>
25  * <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous
26  * references are unable to create / derive from source.
27  * </li>
28  * </ul>
29  */
30 public abstract class DeclarationInTextSource implements StatementSourceReference {
31     private static class InSource extends DeclarationInTextSource {
32         InSource(final String sourceName) {
33             super(sourceName);
34         }
35
36         @Override
37         public String toString() {
38             return getSourceName();
39         }
40     }
41
42     private static class AtLine extends InSource {
43         private final int line;
44
45         AtLine(final String sourceName, final int line) {
46             super(sourceName);
47             this.line = line;
48         }
49
50         @Override
51         public String toString() {
52             return getSourceName() + ':' + line;
53         }
54
55         int getLine() {
56             return line;
57         }
58     }
59
60     private static final class AtPosition extends AtLine {
61         private final int character;
62
63         AtPosition(final String sourceName, final int line, final int character) {
64             super(sourceName, line);
65             this.character = character;
66         }
67
68         @Override
69         public String toString() {
70             return getSourceName() + ':' + getLine() + ':' + character;
71         }
72     }
73
74     private final String sourceName;
75
76     DeclarationInTextSource(final String sourceName) {
77         this.sourceName = sourceName;
78     }
79
80     public static DeclarationInTextSource inSource(final String sourceName) {
81         return new InSource(sourceName);
82     }
83
84     public static DeclarationInTextSource atLine(final String sourceName, final int line) {
85         return new AtLine(sourceName, line);
86     }
87
88     public static DeclarationInTextSource atPosition(final String sourceName, final int line, final int position) {
89         return new AtPosition(sourceName, line, position);
90     }
91
92     public String getSourceName() {
93         return sourceName;
94     }
95
96     @Override
97     public StatementSource getStatementSource() {
98         return StatementSource.DECLARATION;
99     }
100
101     @Override
102     public abstract String toString();
103 }