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 com.google.common.base.Preconditions;
11 import com.google.common.collect.BiMap;
12 import com.google.common.collect.HashBiMap;
13 import java.io.OutputStream;
16 import java.util.Optional;
17 import javax.xml.stream.XMLOutputFactory;
18 import javax.xml.stream.XMLStreamException;
19 import javax.xml.stream.XMLStreamWriter;
20 import org.opendaylight.yangtools.yang.common.Revision;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 public final class YinExportUtils {
28 private YinExportUtils() {
29 throw new UnsupportedOperationException("Utility class");
34 * Returns well-formed file name of YIN file as defined in RFC6020.
37 * Module or submodule name
39 * 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));
48 * Returns well-formed file name of YIN file as defined in RFC6020.
51 * name Module or submodule name
53 * Revision of module or submodule
54 * @return well-formed file name of YIN file as defined in RFC6020.
56 public static String wellFormedYinName(final String name, final String revision) {
57 if (revision == null) {
58 return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
60 return Preconditions.checkNotNull(name) + '@' + revision + YangConstants.RFC6020_YIN_FILE_EXTENSION;
64 * Writes YIN representation of supplied module to specified output stream.
67 * Schema Context which contains module and extension definitions
68 * to be used during export of model.
70 * Module to be exported.
72 * Output stream to which YIN representation of model will be
74 * @throws XMLStreamException
76 public static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final OutputStream str)
77 throws XMLStreamException {
78 writeModuleToOutputStream(ctx, module, str, false);
82 * Writes YIN representation of supplied module to specified output stream.
85 * Schema Context which contains module and extension definitions
86 * to be used during export of model.
88 * Module to be exported.
90 * Output stream to which YIN representation of model will be
92 * @param emitInstantiated
93 * Option to emit also instantiated statements (e.g. statements
94 * added by uses or augment)
95 * @throws XMLStreamException
97 public static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final OutputStream str,
98 final boolean emitInstantiated) throws XMLStreamException {
99 final XMLOutputFactory factory = XMLOutputFactory.newFactory();
100 final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(str);
101 writeModuleToOutputStream(ctx, module, xmlStreamWriter, emitInstantiated);
102 xmlStreamWriter.flush();
105 private static void writeModuleToOutputStream(final SchemaContext ctx, final Module module,
106 final XMLStreamWriter xmlStreamWriter, final boolean emitInstantiated) {
107 final URI moduleNs = module.getNamespace();
108 final Map<String, URI> prefixToNs = prefixToNamespace(ctx, module);
109 final StatementTextWriter yinWriter = SingleModuleYinStatementWriter.create(xmlStreamWriter, moduleNs,
111 SchemaContextEmitter.writeToStatementWriter(module, ctx, yinWriter, emitInstantiated);
114 private static Map<String, URI> prefixToNamespace(final SchemaContext ctx, final Module module) {
115 final BiMap<String, URI> prefixMap = HashBiMap.create(module.getImports().size() + 1);
116 prefixMap.put(module.getPrefix(), module.getNamespace());
117 for (final ModuleImport imp : module.getImports()) {
118 final String prefix = imp.getPrefix();
119 final URI namespace = getModuleNamespace(ctx, imp.getModuleName());
120 prefixMap.put(prefix, namespace);
125 private static URI getModuleNamespace(final SchemaContext ctx, final String moduleName) {
126 for (final Module module : ctx.getModules()) {
127 if (moduleName.equals(module.getName())) {
128 return module.getNamespace();
131 throw new IllegalArgumentException("Module " + moduleName + "does not exists in provided schema context");