Bug 3670 (part 3/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / 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  *
14  * Reference of statement source present in textual source format
15  *
16  * Utility implementation of {@link StatementSourceReference} for textual sources,
17  * this is prefered {@link StatementSourceReference} for implementations
18  * of YANG / YIN statement stream sources.
19  *
20  *
21  *  To create source reference use one of this static factories:
22  *  <ul>
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.
25  *  </li>
26  *  <li>{@link #atLine(String, String, int)}- provides source and line of statement location.
27  *  </li>
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.
30  *  </li>
31  *  </ul>
32  *
33  */
34 public abstract class DeclarationInTextSource implements StatementSourceReference {
35
36     private final String sourceText;
37     private final String sourceName;
38
39     DeclarationInTextSource(String sourceName, String sourceText) {
40         this.sourceName = sourceName;
41         this.sourceText = sourceText;
42     }
43
44     public String getSourceName() {
45         return sourceName;
46     }
47
48     public String getSourceText() {
49         return sourceText;
50     }
51
52     @Override
53     public StatementSource getStatementSource() {
54         return StatementSource.DECLARATION;
55     }
56
57     @Override
58     public abstract String toString();
59
60     public static final DeclarationInTextSource inSource(String sourceName, String sourceText) {
61         return new InSource(sourceName, sourceText);
62     }
63
64     public static final DeclarationInTextSource atLine(String sourceName, String sourceText, int line) {
65         return new AtLine(sourceName, sourceText, line);
66     }
67
68     public static final DeclarationInTextSource atPosition(String sourceName, String sourceText, int line, int position) {
69         return new AtPosition(sourceName, sourceText, line,position);
70     }
71
72     private static class InSource extends DeclarationInTextSource {
73
74         public InSource(String sourceName, String sourceText) {
75             super(sourceName, sourceText);
76         }
77
78         @Override
79         public String toString() {
80             return getSourceName();
81         }
82
83     }
84
85     private static class AtLine extends DeclarationInTextSource {
86
87         private final int line;
88
89         public AtLine(String sourceName, String sourceText, int line) {
90             super(sourceName, sourceText);
91             this.line = line;
92         }
93
94         @Override
95         public String toString() {
96             return String.format("%s:%d", getSourceName(),line);
97         }
98
99     }
100
101     private static class AtPosition extends DeclarationInTextSource {
102
103         private int line;
104         private int character;
105
106         public AtPosition(String sourceName, String sourceText, int line, int character) {
107             super(sourceName, sourceText);
108             this.line = line;
109             this.character = character;
110         }
111
112         @Override
113         public String toString() {
114             return String.format("%s:%d:%d", getSourceName(),line,character);
115         }
116
117     }
118
119 }