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;
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;
30 * This class represents implementation of StatementStreamSource
31 * in order to emit YIN statements using supplied StatementWriter
34 public class YinStatementSourceImpl implements StatementStreamSource {
36 private static final Logger LOG = LoggerFactory.getLogger(YinStatementSourceImpl.class);
37 private static XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
39 private YinStatementParserImpl yinStatementModelParser;
40 private XMLStreamReader streamReader;
41 private InputStream inputStream;
42 private String fileName;
43 private boolean isAbsolute;
45 public YinStatementSourceImpl(final InputStream inputStream) {
46 yinStatementModelParser = new YinStatementParserImpl(inputStream.toString());
47 this.inputStream = inputStream;
50 public YinStatementSourceImpl(final String fileName, final boolean isAbsolute) {
51 yinStatementModelParser = new YinStatementParserImpl(fileName);
52 this.fileName = Preconditions.checkNotNull(fileName);
53 this.isAbsolute = isAbsolute;
57 public void writeLinkage(StatementWriter writer, QNameToStatementDefinition stmtDef) {
59 yinStatementModelParser.setAttributes(writer, stmtDef);
60 yinStatementModelParser.walk(streamReader);
64 public void writeLinkageAndStatementDefinitions(StatementWriter writer, QNameToStatementDefinition stmtDef,
65 PrefixToModule prefixes) {
67 yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
68 yinStatementModelParser.walk(streamReader);
72 public void writeFull(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) {
74 yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
75 yinStatementModelParser.walk(streamReader);
79 private void initializeReader() {
81 if (fileName != null) {
82 this.inputStream = loadFile(fileName, isAbsolute);
84 streamReader = xmlInputFactory.createXMLStreamReader(inputStream);
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);
95 private void closeReader() {
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);
106 private InputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException, IOException {
109 file = new File(fileName);
111 file = new File(getClass().getResource(fileName).toURI());
114 return new NamedFileInputStream(file, fileName);