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
8 package org.opendaylight.yangtools.yang.model.export;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.io.OutputStream;
14 import java.util.Optional;
15 import javax.xml.stream.XMLEventReader;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.transform.OutputKeys;
18 import javax.xml.transform.Transformer;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.transform.TransformerFactory;
21 import javax.xml.transform.stax.StAXSource;
22 import javax.xml.transform.stream.StreamResult;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.common.YangConstants;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
28 public final class YinExportUtils {
29 private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
31 private YinExportUtils() {
36 * Returns well-formed file name of YIN file as defined in RFC6020.
38 * @param name Module or submodule name
39 * @param revision Revision of module or submodule
40 * @return well-formed file name of YIN file as defined in RFC6020.
42 public static String wellFormedYinName(final String name, final Optional<Revision> revision) {
43 return wellFormedYinName(name, revision.map(Revision::toString).orElse(null));
47 * Returns well-formed file name of YIN file as defined in RFC6020.
49 * @param name name Module or submodule name
50 * @param revision Revision of module or submodule
51 * @return well-formed file name of YIN file as defined in RFC6020.
53 public static String wellFormedYinName(final String name, final String revision) {
54 if (revision == null) {
55 return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
57 return requireNonNull(name) + '@' + revision + YangConstants.RFC6020_YIN_FILE_EXTENSION;
61 * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
62 * {@link ModuleEffectiveStatement} trait.
64 * @param module Module to be exported
65 * @throws IllegalArgumentException if the module's declared representation is not available.
66 * @throws NullPointerException if any of of the parameters is null
67 * @throws XMLStreamException if an input-output error occurs
70 public static void writeModuleAsYinText(final ModuleEffectiveStatement module, final OutputStream output)
71 throws XMLStreamException {
72 writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(module), output);
76 * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
77 * {@link SubmoduleEffectiveStatement} trait.
79 * @param parentModule Parent module
80 * @param submodule Submodule to be exported
81 * @throws IllegalArgumentException if the parent module's or submodule's declared representation is not available
82 * @throws NullPointerException if any of of the parameters is null
83 * @throws XMLStreamException if an input-output error occurs
86 public static void writeSubmoduleAsYinText(final ModuleEffectiveStatement parentModule,
87 final SubmoduleEffectiveStatement submodule, final OutputStream output) throws XMLStreamException {
88 writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(parentModule, submodule),
92 private static void writeReaderToOutput(final XMLEventReader reader, final OutputStream output)
93 throws XMLStreamException {
95 final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
96 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
97 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
98 transformer.transform(new StAXSource(reader), new StreamResult(output));
99 } catch (TransformerException e) {
100 throw new XMLStreamException("Failed to stream XML events", e);