Eliminate SourceException throws declarations
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / YangStatementSourceImpl.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.stmt.rfc6020;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.URISyntaxException;
14 import org.antlr.v4.runtime.ANTLRInputStream;
15 import org.antlr.v4.runtime.CommonTokenStream;
16 import org.antlr.v4.runtime.tree.ParseTreeWalker;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementLexer;
20 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
21 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
22 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.parser.impl.YangErrorListener;
25 import org.opendaylight.yangtools.yang.parser.impl.YangStatementParserListenerImpl;
26 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
27 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
30 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
31
32 /**
33  *
34  * This class represents implementation of StatementStreamSource
35  * in order to emit YANG statements using supplied StatementWriter
36  *
37  */
38 public final class YangStatementSourceImpl implements StatementStreamSource {
39
40     private YangStatementParserListenerImpl yangStatementModelParser;
41     private YangStatementParser.StatementContext statementContext;
42     private ParseTreeWalker walker;
43     private String sourceName;
44 //    private String source;
45 //    private InputStream sourceStream;
46     private static final Logger LOG = LoggerFactory.getLogger(YangStatementSourceImpl.class);
47
48     public YangStatementSourceImpl(final String fileName, boolean isAbsolute) {
49         try {
50             statementContext = parseYangSource(loadFile(fileName, isAbsolute));
51             walker = new ParseTreeWalker();
52             yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
53         } catch (Exception e) {
54             logError(e);
55         }
56     }
57
58     public YangStatementSourceImpl(final InputStream inputStream) {
59         try {
60             statementContext = parseYangSource(inputStream);
61             walker = new ParseTreeWalker();
62             yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
63         } catch (Exception e) {
64             logError(e);
65         }
66     }
67
68     public YangStatementSourceImpl(SourceIdentifier identifier, YangStatementParser.StatementContext statementContext) {
69         try {
70             this.statementContext = statementContext;
71             this.sourceName = identifier.getName();
72             walker = new ParseTreeWalker();
73             yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
74         } catch (Exception e) {
75             logError(e);
76         }
77     }
78
79     @Override
80     public void writeLinkage(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
81         yangStatementModelParser.setAttributes(writer, stmtDef);
82         walker.walk(yangStatementModelParser, statementContext);
83     }
84
85     @Override
86     public void writeLinkageAndStatementDefinitions(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
87         yangStatementModelParser.setAttributes(writer, stmtDef, prefixes);
88         walker.walk(yangStatementModelParser, statementContext);
89     }
90
91     @Override
92     public void writeFull(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
93         yangStatementModelParser.setAttributes(writer, stmtDef, prefixes);
94         walker.walk(yangStatementModelParser, statementContext);
95     }
96
97     private NamedFileInputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException,
98             IOException {
99         //TODO: we need absolute path first!
100         return isAbsolute ? new NamedFileInputStream(new File(fileName), fileName) : new NamedFileInputStream(new File
101                 (getClass().getResource(fileName).toURI()), fileName);
102
103 //        final File file = new File(fileName);
104 //        final ByteSource byteSource = BuilderUtils.fileToByteSource(file);
105 //        source = byteSource.asCharSource(Charsets.UTF_8).read();
106 //        return isAbsolute ? new NamedFileInputStream(file, fileName) : new NamedFileInputStream(new File
107 //                (getClass().getResource(fileName).toURI()), fileName);
108     }
109
110     private YangStatementParser.StatementContext parseYangSource(final InputStream stream) throws IOException,
111             YangSyntaxErrorException {
112         final YangStatementLexer lexer = new YangStatementLexer(new ANTLRInputStream(stream));
113         final CommonTokenStream tokens = new CommonTokenStream(lexer);
114         final YangStatementParser parser = new YangStatementParser(tokens);
115         //disconnect from console error output
116         parser.removeErrorListeners();
117
118         final YangErrorListener errorListener = new YangErrorListener();
119         parser.addErrorListener(errorListener);
120
121         if(stream instanceof NamedFileInputStream) {
122             sourceName = stream.toString();
123         } else {
124             sourceName = null;
125         }
126
127 //        sourceStream = stream;
128 //        sourceName = parser.getSourceName();
129 //
130 //        if (sourceName == null) {
131 //            sourceName = stream.toString();
132 //        }
133         final StatementContext result = parser.statement();
134         errorListener.validate();
135
136         return result;
137     }
138
139     public YangStatementParser.StatementContext getYangAST() {
140         return statementContext;
141     }
142
143     @Override
144     public String toString() {
145         return sourceName;
146     }
147
148 //    public InputStream getSourceStream() {
149 //        return sourceStream;
150 //    }
151
152     private static void logError(Exception e) {
153         if (e instanceof YangSyntaxErrorException) {
154             LOG.error(((YangSyntaxErrorException) e).getFormattedMessage(), e);
155         } else {
156             LOG.error(e.getMessage(), e);
157         }
158     }
159 }