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