5c88626adfb39792d260376d56e64c0231bc3f30
[yangtools.git] /
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.stmt.ModuleEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
27
28 public final class YinExportUtils {
29     private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
30
31     private YinExportUtils() {
32         // Hidden on purpose
33     }
34
35     /**
36      * Returns well-formed file name of YIN file as defined in RFC6020.
37      *
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.
41      */
42     public static String wellFormedYinName(final String name, final Optional<Revision> revision) {
43         return wellFormedYinName(name, revision.map(Revision::toString).orElse(null));
44     }
45
46     /**
47      * Returns well-formed file name of YIN file as defined in RFC6020.
48      *
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.
52      */
53     public static String wellFormedYinName(final String name, final String revision) {
54         if (revision == null) {
55             return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
56         }
57         return requireNonNull(name) + '@' + revision +  YangConstants.RFC6020_YIN_FILE_EXTENSION;
58     }
59
60     /**
61      * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
62      * {@link ModuleEffectiveStatement} trait.
63      *
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
68      */
69     @Beta
70     public static void writeModuleAsYinText(final ModuleEffectiveStatement module, final OutputStream output)
71             throws XMLStreamException {
72         writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(module), output);
73     }
74
75     /**
76      * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
77      * {@link SubmoduleEffectiveStatement} trait.
78      *
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
84      */
85     @Beta
86     public static void writeSubmoduleAsYinText(final ModuleEffectiveStatement parentModule,
87             final SubmoduleEffectiveStatement submodule, final OutputStream output) throws XMLStreamException {
88         writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(parentModule, submodule),
89             output);
90     }
91
92     private static void writeReaderToOutput(final XMLEventReader reader, final OutputStream output)
93             throws XMLStreamException {
94         try {
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);
101         }
102     }
103 }