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