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