Bug 6859 - Binding generator v1 refactoring
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / PackageTranslator.java
1 /*
2  * Copyright (c) 2013 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.controller.config.yangjmxgenerator;
9
10 import java.util.Map;
11 import java.util.Map.Entry;
12 import org.opendaylight.mdsal.binding.generator.util.BindingGeneratorUtil;
13 import org.opendaylight.yangtools.yang.model.api.Module;
14
15 /**
16  * Maps from module namespaces to java package names using a Map<String, String>,
17  * where key is namespace prefix and value is package that replaces matched prefix.
18  */
19 public class PackageTranslator {
20     private final Map<String, String> namespacePrefixToPackageMap;
21
22     public PackageTranslator(final Map<String, String> namespacePrefixToPackageMap) {
23         this.namespacePrefixToPackageMap = namespacePrefixToPackageMap;
24     }
25
26     /**
27      * Based on mapping, find longest matching key and return value plus the
28      * remaining part of namespace, with colons replaced by dots. Example:
29      * Mapping [ 'urn:opendaylight:params:xml:ns:yang:controller' :
30      * 'org.opendaylight.controller'] and module with namespace
31      * 'urn:opendaylight:params:xml:ns:yang:controller:threads:api' will result
32      * in 'org.opendaylight.controller.threads.api' .
33      *
34      * @throws IllegalStateException
35      *             if there is no mapping found.
36      */
37     public String getPackageName(final Module module) {
38         Entry<String, String> longestMatch = null;
39         int longestMatchLength = 0;
40         final String namespace = module.getNamespace().toString();
41         for (final Entry<String, String> entry : this.namespacePrefixToPackageMap
42                 .entrySet()) {
43             if (namespace.startsWith(entry.getKey())
44                     && (entry.getKey().length() > longestMatchLength)) {
45                 longestMatch = entry;
46                 longestMatchLength = entry.getKey().length();
47             }
48         }
49         if (longestMatch != null) {
50             return longestMatch.getValue()
51                     + sanitizePackage(namespace.substring(longestMatchLength));
52         } else {
53             return BindingGeneratorUtil.moduleNamespaceToPackageName(module);
54         }
55     }
56
57     // TODO add to PackageTranslator
58     private static String sanitizePackage(final String namespace) {
59         String newNamespace = namespace;
60         newNamespace = newNamespace.replace("://", ".");
61         newNamespace = newNamespace.replace("/", ".");
62         newNamespace = newNamespace.replace(":", ".");
63         newNamespace = newNamespace.replace("-", "_");
64         newNamespace = newNamespace.replace("@", ".");
65         newNamespace = newNamespace.replace("$", ".");
66         newNamespace = newNamespace.replace("#", ".");
67         newNamespace = newNamespace.replace("'", ".");
68         newNamespace = newNamespace.replace("*", ".");
69         newNamespace = newNamespace.replace("+", ".");
70         newNamespace = newNamespace.replace(",", ".");
71         newNamespace = newNamespace.replace(";", ".");
72         newNamespace = newNamespace.replace("=", ".");
73         return newNamespace;
74     }
75 }