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.stmt.rfc6020;
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;
35 * This class represents implementation of StatementStreamSource
36 * in order to emit YANG statements using supplied StatementWriter
39 public final class YangStatementSourceImpl implements StatementStreamSource {
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);
49 public YangStatementSourceImpl(final String fileName, boolean isAbsolute) {
51 statementContext = parseYangSource(loadFile(fileName, isAbsolute));
52 walker = new ParseTreeWalker();
53 yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
54 } catch (Exception e) {
59 public YangStatementSourceImpl(final InputStream inputStream) {
61 statementContext = parseYangSource(inputStream);
62 walker = new ParseTreeWalker();
63 yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
64 } catch (Exception e) {
69 public YangStatementSourceImpl(SourceIdentifier identifier, YangStatementParser.StatementContext statementContext) {
71 this.statementContext = statementContext;
72 this.sourceName = identifier.getName();
73 walker = new ParseTreeWalker();
74 yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
75 } catch (Exception e) {
81 public void writeLinkage(final StatementWriter writer, final QNameToStatementDefinition stmtDef) throws SourceException {
82 yangStatementModelParser.setAttributes(writer, stmtDef);
83 walker.walk(yangStatementModelParser, statementContext);
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);
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);
98 private NamedFileInputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException,
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);
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);
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();
119 final YangErrorListener errorListener = new YangErrorListener();
120 parser.addErrorListener(errorListener);
122 if(stream instanceof NamedFileInputStream) {
123 sourceName = stream.toString();
128 // sourceStream = stream;
129 // sourceName = parser.getSourceName();
131 // if (sourceName == null) {
132 // sourceName = stream.toString();
134 final StatementContext result = parser.statement();
135 errorListener.validate();
140 public YangStatementParser.StatementContext getYangAST() {
141 return statementContext;
145 public String toString() {
149 // public InputStream getSourceStream() {
150 // return sourceStream;
153 private static void logError(Exception e) {
154 if (e instanceof YangSyntaxErrorException) {
155 LOG.error(((YangSyntaxErrorException) e).getFormattedMessage(), e);
157 LOG.error(e.getMessage(), e);