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;
15 import java.util.Date;
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.SimpleDateFormatUtil;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 public final class YinExportUtils {
27 private YinExportUtils() {
28 throw new UnsupportedOperationException("Utility class");
33 * Returns well-formed file name of YIN file as defined in RFC6020.
36 * Module or submodule name
38 * Revision of module or submodule
39 * @return well-formed file name of YIN file as defined in RFC6020.
41 public static String wellFormedYinName(final String name, final Date revision) {
42 return wellFormedYinName(name, SimpleDateFormatUtil.getRevisionFormat().format(revision));
47 * Returns well-formed file name of YIN file as defined in RFC6020.
50 * name Module or submodule name
52 * Revision of module or submodule
53 * @return well-formed file name of YIN file as defined in RFC6020.
55 public static String wellFormedYinName(final String name, final String revision) {
56 return String.format("%s@%s.yin", Preconditions.checkNotNull(name), Preconditions.checkNotNull(revision));
60 * Writes YIN representation of supplied module to specified output stream.
63 * Schema Context which contains module and extension definitions
64 * to be used during export of model.
66 * Module to be exported.
68 * Output stream to which YIN representation of model will be
70 * @throws XMLStreamException
72 public static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final OutputStream str)
73 throws XMLStreamException {
74 writeModuleToOutputStream(ctx, module, str, false);
78 * Writes YIN representation of supplied module to specified output stream.
81 * Schema Context which contains module and extension definitions
82 * to be used during export of model.
84 * Module to be exported.
86 * Output stream to which YIN representation of model will be
88 * @param emitInstantiated
89 * Option to emit also instantiated statements (e.g. statements
90 * added by uses or augment)
91 * @throws XMLStreamException
93 public static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final OutputStream str,
94 final boolean emitInstantiated) throws XMLStreamException {
95 final XMLOutputFactory factory = XMLOutputFactory.newFactory();
96 final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(str);
97 writeModuleToOutputStream(ctx, module, xmlStreamWriter, emitInstantiated);
98 xmlStreamWriter.flush();
101 private static void writeModuleToOutputStream(final SchemaContext ctx, final Module module,
102 final XMLStreamWriter xmlStreamWriter, final boolean emitInstantiated) {
103 final URI moduleNs = module.getNamespace();
104 final Map<String, URI> prefixToNs = prefixToNamespace(ctx, module);
105 final StatementTextWriter yinWriter = SingleModuleYinStatementWriter.create(xmlStreamWriter, moduleNs,
107 SchemaContextEmitter.writeToStatementWriter(module, ctx, yinWriter, emitInstantiated);
110 private static Map<String, URI> prefixToNamespace(final SchemaContext ctx, final Module module) {
111 final BiMap<String, URI> prefixMap = HashBiMap.create(module.getImports().size() + 1);
112 prefixMap.put(module.getPrefix(), module.getNamespace());
113 for (final ModuleImport imp : module.getImports()) {
114 final String prefix = imp.getPrefix();
115 final URI namespace = getModuleNamespace(ctx, imp.getModuleName());
116 prefixMap.put(prefix, namespace);
121 private static URI getModuleNamespace(final SchemaContext ctx, final String moduleName) {
122 for (final Module module : ctx.getModules()) {
123 if (moduleName.equals(module.getName())) {
124 return module.getNamespace();
127 throw new IllegalArgumentException("Module " + moduleName + "does not exists in provided schema context");