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.SourceException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
25 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
31 * This class represents implementation of StatementStreamSource
32 * in order to emit YIN statements using supplied StatementWriter
35 public class YinStatementSourceImpl implements StatementStreamSource {
37 private static final Logger LOG = LoggerFactory.getLogger(YinStatementSourceImpl.class);
38 private static XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
40 private YinStatementParserImpl yinStatementModelParser;
41 private XMLStreamReader streamReader;
42 private InputStream inputStream;
43 private String fileName;
44 private boolean isAbsolute;
46 public YinStatementSourceImpl(final InputStream inputStream) {
47 yinStatementModelParser = new YinStatementParserImpl(inputStream.toString());
48 this.inputStream = inputStream;
51 public YinStatementSourceImpl(final String fileName, final boolean isAbsolute) {
52 yinStatementModelParser = new YinStatementParserImpl(fileName);
53 this.fileName = Preconditions.checkNotNull(fileName);
54 this.isAbsolute = isAbsolute;
58 public void writeLinkage(StatementWriter writer, QNameToStatementDefinition stmtDef) throws SourceException {
60 yinStatementModelParser.setAttributes(writer, stmtDef);
61 yinStatementModelParser.walk(streamReader);
65 public void writeLinkageAndStatementDefinitions(StatementWriter writer, QNameToStatementDefinition stmtDef,
66 PrefixToModule prefixes) throws SourceException {
68 yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
69 yinStatementModelParser.walk(streamReader);
73 public void writeFull(StatementWriter writer, QNameToStatementDefinition stmtDef, PrefixToModule prefixes) throws
76 yinStatementModelParser.setAttributes(writer, stmtDef, prefixes);
77 yinStatementModelParser.walk(streamReader);
81 private void initializeReader() {
83 if (fileName != null) {
84 this.inputStream = loadFile(fileName, isAbsolute);
86 streamReader = xmlInputFactory.createXMLStreamReader(inputStream);
88 } catch (XMLStreamException e) {
89 LOG.warn("Error while creating XMLStreamReader from input stream", e);
90 } catch (URISyntaxException e) {
91 LOG.warn("File name {} cannot be parsed as URI reference", fileName, e);
92 } catch (IOException e) {
93 LOG.warn("File {} cannot be found or read into string ", fileName, e);
97 private void closeReader() {
100 streamReader.close();
101 } catch (XMLStreamException e) {
102 LOG.warn("Error occured while freeing associated resources", e);
103 } catch (IOException e) {
104 LOG.warn("I/O error occured during closing the input stream", e);
108 private InputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException, IOException {
111 file = new File(fileName);
113 file = new File(getClass().getResource(fileName).toURI());
116 return new NamedFileInputStream(file, fileName);