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