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 class YinExportUtils {
28 private YinExportUtils() {
29 throw new UnsupportedOperationException("Utility class");
35 * Returns well-formed file name of YIN file as defined in RFC6020.
37 * @param name Module or submodule name
38 * @param revision 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) throws XMLStreamException {
73 final XMLOutputFactory factory = XMLOutputFactory.newFactory();
74 final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(str);
75 writeModuleToOutputStream(ctx,module, xmlStreamWriter);
78 private static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final XMLStreamWriter xmlStreamWriter) {
79 final URI moduleNs = module.getNamespace();
80 final Map<String, URI> prefixToNs = prefixToNamespace(ctx,module);
81 final StatementTextWriter yinWriter = SingleModuleYinStatementWriter.create(xmlStreamWriter, moduleNs, prefixToNs);
82 SchemaContextEmitter.writeToStatementWriter(module, ctx,yinWriter);
85 private static Map<String, URI> prefixToNamespace(final SchemaContext ctx, final Module module) {
86 final BiMap<String, URI> prefixMap = HashBiMap.create(module.getImports().size() + 1);
87 prefixMap.put(module.getPrefix(), module.getNamespace());
88 for(final ModuleImport imp : module.getImports()) {
89 final String prefix = imp.getPrefix();
90 final URI namespace = getModuleNamespace(ctx,imp.getModuleName());
91 prefixMap.put(prefix, namespace);
96 private static URI getModuleNamespace(final SchemaContext ctx, final String moduleName) {
97 for(final Module module : ctx.getModules()) {
98 if(moduleName.equals(module.getName())) {
99 return module.getNamespace();
102 throw new IllegalArgumentException("Module " + moduleName + "does not exists in provided schema context");