Improve NonJavaCharsConverter
[mdsal.git] / binding2 / mdsal-binding2-java-api-generator / src / main / twirl / org / opendaylight / mdsal / binding / javav2 / java / api / generator / yangModuleInfoTemplate.scala.txt
1 @*
2  * Copyright (c) 2016 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
9 @import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier.CLASS
10 @import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer.normalizeSpecificIdentifier
11 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getFormattedRevision
12 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getSourcePath
13 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.renderers.YangModuleInfoTemplateRenderer.getSortedQName
14 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.getRootPackageName
15 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.MODULE_INFO_CLASS_NAME
16 @import org.opendaylight.yangtools.yang.model.api.Module
17 @import org.opendaylight.yangtools.yang.model.api.SchemaContext
18
19 @(module: Module, ctx: SchemaContext, importedNames: Map[String, String])
20 @if(module != null && ctx != null) {
21 public final class @{MODULE_INFO_CLASS_NAME} implements @{importedNames.get("yangModuleInfo")} {
22
23     private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
24
25     private final @{importedNames.get("string")} name = "@{module.getName}";
26     private final @{importedNames.get("string")} namespace = "@{module.getNamespace.toString}";
27     private final @{importedNames.get("string")} revision = "@{getFormattedRevision(module.getRevision)}";
28     private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module)}";
29     private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
30
31     public static @{importedNames.get("yangModuleInfo")} getInstance() {
32         return INSTANCE;
33     }
34
35     @{classBody(module, MODULE_INFO_CLASS_NAME)}
36 }
37 }
38
39 @generateSubInfo(module: Module) = {
40 @for(submodule <- module.getSubmodules) {
41     private static final class @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info implements @{importedNames.get("yangModuleInfo")} {
42
43         private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info();
44
45         private final @{importedNames.get("string")} name = "@{submodule.getName}";
46         private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}";
47         private final @{importedNames.get("string")} revision = "@{getFormattedRevision(submodule.getRevision)}";
48         private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule)}";
49         private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
50
51         public static @{importedNames.get("yangModuleInfo")} getInstance() {
52             return INSTANCE;
53         }
54
55         @{classBody(submodule, normalizeSpecificIdentifier(submodule.getName + "Info", CLASS))}
56     }
57 }
58 }
59
60 @classBody(module: Module, className: String) = {
61     private @{className}() {
62         @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) {
63             @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>();
64         }
65         @if(!module.getImports.isEmpty) {
66             @for(moduleImport <- module.getImports) {
67                 @if(moduleImport.getRevision == null) {
68                     set.add(@{getRootPackageName(getSortedQName(ctx.getModules, moduleImport.getModuleName))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
69                 } else {
70                     set.add(@{getRootPackageName(ctx.findModuleByName(moduleImport.getModuleName, moduleImport.getRevision))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
71                 }
72             }
73         }
74         @if(!module.getSubmodules.isEmpty) {
75             @for(submodule <- module.getSubmodules) {
76                 set.add(@{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info.getInstance());
77             }
78         }
79         @if(!module.getImports.isEmpty && !module.getSubmodules.isEmpty) {
80             importedModules = @{importedNames.get("collections")}.emptySet();
81         } else {
82             importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
83         }
84
85         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
86         if (stream == null) {
87             throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
88         }
89         try {
90             stream.close();
91         } catch (@{importedNames.get("iOException")} e) {
92         // Resource leak, but there is nothing we can do
93         }
94     }
95
96     @@Override
97     public @{importedNames.get("string")} getName() {
98         return name;
99     }
100
101     @@Override
102     public @{importedNames.get("string")} getRevision() {
103         return revision;
104     }
105
106     @@Override
107     public @{importedNames.get("string")} getNamespace() {
108         return namespace;
109     }
110
111     @@Override
112     public @{importedNames.get("inputStream")} getModuleSourceStream() throws IOException {
113         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
114         if (stream == null) {
115             throw new @{importedNames.get("iOException")}("Resource " + resourcePath + " is missing");
116         }
117         return stream;
118     }
119
120     @@Override
121     public @{importedNames.get("set")}<@{importedNames.get("YangModuleInfo")}> getImportedModules() {
122         return importedModules;
123     }
124
125     @@Override
126     public @{importedNames.get("string")} toString() {
127         @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
128         sb.append("[");
129         sb.append("name = " + name);
130         sb.append(", namespace = " + namespace);
131         sb.append(", revision = " + revision);
132         sb.append(", resourcePath = " + resourcePath);
133         sb.append(", imports = " + importedModules);
134         sb.append("]");
135         return sb.toString();
136     }
137
138     @{generateSubInfo(module)}
139 }