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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
11 import com.google.common.base.Preconditions;
12 import com.google.common.io.ByteStreams;
13 import java.io.BufferedInputStream;
14 import java.io.ByteArrayInputStream;
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;
34 * This class represents implementation of StatementStreamSource
35 * in order to emit YIN statements using supplied StatementWriter
38 public class YinStatementSourceImpl implements StatementStreamSource {
40 private static final Logger LOG = LoggerFactory.getLogger(YinStatementSourceImpl.class);
41 private static XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
43 private YinStatementParserImpl yinStatementModelParser;
44 private XMLStreamReader streamReader;
45 private InputStream inputStream;
46 private String fileName;
47 private boolean isAbsolute;
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);
55 public YinStatementSourceImpl(final String fileName, final boolean isAbsolute) {
56 yinStatementModelParser = new YinStatementParserImpl(fileName);
57 this.fileName = Preconditions.checkNotNull(fileName);
58 this.isAbsolute = isAbsolute;
62 public void writeLinkage(StatementWriter writer, QNameToStatementDefinition stmtDef) throws SourceException {
64 yinStatementModelParser.setAttributes(writer, stmtDef);
65 yinStatementModelParser.walk(streamReader);
69 public void writeLinkageAndStatementDefinitions(StatementWriter writer, QNameToStatementDefinition stmtDef,
70 PrefixToModule prefixes) throws SourceException {
72 yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
73 yinStatementModelParser.walk(streamReader);
77 public void writeFull(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) throws
80 yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
81 yinStatementModelParser.walk(streamReader);
85 private void initializeReader() {
87 if (fileName != null) {
88 inputStream = loadFile(fileName, isAbsolute);
89 streamReader = xmlInputFactory.createXMLStreamReader(inputStream);
92 streamReader = xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(ByteStreams.toByteArray
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);
104 private void closeReader() {
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);
115 private InputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException, IOException {
118 file = new File(fileName);
120 file = new File(getClass().getResource(fileName).toURI());
123 return new NamedFileInputStream(file, fileName);