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