Fix yang-export warnings
[yangtools.git] / model / yang-model-export / src / main / java / org / opendaylight / yangtools / yang / model / export / YinExportUtils.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 package org.opendaylight.yangtools.yang.model.export;
9
10 import static java.util.Objects.requireNonNull;
11
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.Module;
26 import org.opendaylight.yangtools.yang.model.api.Submodule;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
29
30 public final class YinExportUtils {
31     private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
32
33     private YinExportUtils() {
34         // Hidden on purpose
35     }
36
37     /**
38      * Returns well-formed file name of YIN file as defined in RFC6020.
39      *
40      * @param name Module or submodule name
41      * @param revision Revision of module or submodule
42      * @return well-formed file name of YIN file as defined in RFC6020.
43      */
44     public static String wellFormedYinName(final String name, final Optional<Revision> revision) {
45         return wellFormedYinName(name, revision.map(Revision::toString).orElse(null));
46     }
47
48     /**
49      * Returns well-formed file name of YIN file as defined in RFC6020.
50      *
51      * @param name name Module or submodule name
52      * @param revision Revision of module or submodule
53      * @return well-formed file name of YIN file as defined in RFC6020.
54      */
55     public static String wellFormedYinName(final String name, final String revision) {
56         if (revision == null) {
57             return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
58         }
59         return requireNonNull(name) + '@' + revision +  YangConstants.RFC6020_YIN_FILE_EXTENSION;
60     }
61
62     /**
63      * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
64      * {@link ModuleEffectiveStatement} trait.
65      *
66      * @param module Module to be exported
67      * @throws IllegalArgumentException if the module is not an ModuleEffectiveStatement or if it declared
68      *                                  representation is not available.
69      * @throws NullPointerException if any of of the parameters is null
70      * @throws XMLStreamException if an input-output error occurs
71      * @deprecated Prefer {@link #writeModuleAsYinText(ModuleEffectiveStatement, OutputStream)}.
72      */
73     @Beta
74     @Deprecated(forRemoval = true)
75     public static void writeModuleAsYinText(final Module module, final OutputStream output) throws XMLStreamException {
76         writeModuleAsYinText(module.asEffectiveStatement(), output);
77     }
78
79     /**
80      * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
81      * {@link ModuleEffectiveStatement} trait.
82      *
83      * @param module Module to be exported
84      * @throws IllegalArgumentException if the module's declared representation is not available.
85      * @throws NullPointerException if any of of the parameters is null
86      * @throws XMLStreamException if an input-output error occurs
87      */
88     @Beta
89     public static void writeModuleAsYinText(final ModuleEffectiveStatement module, final OutputStream output)
90             throws XMLStreamException {
91         writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(module), output);
92     }
93
94     /**
95      * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
96      * {@link SubmoduleEffectiveStatement} trait.
97      *
98      * @param parentModule Parent module
99      * @param submodule Submodule to be exported
100      * @throws IllegalArgumentException if the parent module is not a ModuleEffectiveStatement, if the submodule is not
101      *                                  a SubmoduleEffectiveStatement or if its declared representation is not available
102      * @throws NullPointerException if any of of the parameters is null
103      * @throws XMLStreamException if an input-output error occurs
104      * @deprecated Prefer {@link #writeSubmoduleAsYinText(ModuleEffectiveStatement, SubmoduleEffectiveStatement,
105      *             OutputStream)}.
106      */
107     @Beta
108     @Deprecated(forRemoval = true)
109     public static void writeSubmoduleAsYinText(final Module parentModule, final Submodule submodule,
110             final OutputStream output) throws XMLStreamException {
111         writeSubmoduleAsYinText(parentModule.asEffectiveStatement(), submodule.asEffectiveStatement(), output);
112     }
113
114     /**
115      * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
116      * {@link SubmoduleEffectiveStatement} trait.
117      *
118      * @param parentModule Parent module
119      * @param submodule Submodule to be exported
120      * @throws IllegalArgumentException if the parent module's or submodule's declared representation is not available
121      * @throws NullPointerException if any of of the parameters is null
122      * @throws XMLStreamException if an input-output error occurs
123      */
124     @Beta
125     public static void writeSubmoduleAsYinText(final ModuleEffectiveStatement parentModule,
126             final SubmoduleEffectiveStatement submodule, final OutputStream output) throws XMLStreamException {
127         writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(parentModule, submodule),
128             output);
129     }
130
131     private static void writeReaderToOutput(final XMLEventReader reader, final OutputStream output)
132             throws XMLStreamException {
133         try {
134             final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
135             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
136             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
137             transformer.transform(new StAXSource(reader), new StreamResult(output));
138         } catch (TransformerException e) {
139             throw new XMLStreamException("Failed to stream XML events", e);
140         }
141     }
142 }