3ae5a3e217e015c0b9976109c3bc1d15a1cabb84
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / YinStatementSourceImpl.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10
11 import com.google.common.base.Preconditions;
12 import java.io.File;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.URISyntaxException;
16 import javax.xml.stream.XMLInputFactory;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamReader;
19 import org.opendaylight.yangtools.yang.parser.impl.YinStatementParserImpl;
20 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
21 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
25 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  *
31  * This class represents implementation of StatementStreamSource
32  * in order to emit YIN statements using supplied StatementWriter
33  *
34  */
35 public class YinStatementSourceImpl implements StatementStreamSource {
36
37     private static final Logger LOG = LoggerFactory.getLogger(YinStatementSourceImpl.class);
38     private static XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
39
40     private YinStatementParserImpl yinStatementModelParser;
41     private XMLStreamReader streamReader;
42     private InputStream inputStream;
43     private String fileName;
44     private boolean isAbsolute;
45
46     public YinStatementSourceImpl(final InputStream inputStream) {
47         yinStatementModelParser = new YinStatementParserImpl(inputStream.toString());
48         this.inputStream = inputStream;
49     }
50
51     public YinStatementSourceImpl(final String fileName, final boolean isAbsolute) {
52         yinStatementModelParser = new YinStatementParserImpl(fileName);
53         this.fileName = Preconditions.checkNotNull(fileName);
54         this.isAbsolute = isAbsolute;
55     }
56
57     @Override
58     public void writeLinkage(StatementWriter writer, QNameToStatementDefinition stmtDef) throws SourceException {
59         initializeReader();
60         yinStatementModelParser.setAttributes(writer, stmtDef);
61         yinStatementModelParser.walk(streamReader);
62     }
63
64     @Override
65     public void writeLinkageAndStatementDefinitions(StatementWriter writer, QNameToStatementDefinition stmtDef,
66             PrefixToModule prefixes) throws SourceException {
67         initializeReader();
68         yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
69         yinStatementModelParser.walk(streamReader);
70     }
71
72     @Override
73     public void writeFull(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) throws
74             SourceException {
75         initializeReader();
76         yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
77         yinStatementModelParser.walk(streamReader);
78         closeReader();
79     }
80
81     private void initializeReader() {
82         try {
83             if (fileName != null) {
84                 this.inputStream = loadFile(fileName, isAbsolute);
85             }
86             streamReader = xmlInputFactory.createXMLStreamReader(inputStream);
87
88         } catch (XMLStreamException e) {
89             LOG.warn("Error while creating XMLStreamReader from input stream", e);
90         } catch (URISyntaxException e) {
91             LOG.warn("File name {} cannot be parsed as URI reference", fileName, e);
92         } catch (IOException e) {
93             LOG.warn("File {} cannot be found or read into string ", fileName, e);
94         }
95     }
96
97     private void closeReader() {
98         try {
99             inputStream.close();
100             streamReader.close();
101         } catch (XMLStreamException e) {
102             LOG.warn("Error occured while freeing associated resources", e);
103         } catch (IOException e) {
104             LOG.warn("I/O error occured during closing the input stream", e);
105         }
106     }
107
108     private InputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException, IOException {
109         final File file;
110         if (isAbsolute) {
111             file = new File(fileName);
112         } else {
113             file = new File(getClass().getResource(fileName).toURI());
114         }
115
116         return new NamedFileInputStream(file, fileName);
117     }
118 }