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