2 * Copyright (c) 2013 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.controller.config.yangjmxgenerator;
11 import java.util.Map.Entry;
13 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
14 import org.opendaylight.yangtools.yang.model.api.Module;
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
21 public class PackageTranslator {
22 private final Map<String, String> namespacePrefixToPackageMap;
24 public PackageTranslator(Map<String, String> namespacePrefixToPackageMap) {
25 this.namespacePrefixToPackageMap = namespacePrefixToPackageMap;
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' .
36 * @throws IllegalStateException
37 * if there is no mapping found.
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
45 if (namespace.startsWith(entry.getKey())
46 && entry.getKey().length() > longestMatchLength) {
48 longestMatchLength = entry.getKey().length();
51 if (longestMatch != null) {
52 return longestMatch.getValue()
53 + sanitizePackage(namespace.substring(longestMatchLength));
55 return BindingGeneratorUtil.moduleNamespaceToPackageName(module);
59 // TODO add to PackageTranslator
60 private static String sanitizePackage(String namespace) {
61 namespace = namespace.replace("://", ".");
62 namespace = namespace.replace("/", ".");
63 namespace = namespace.replace(":", ".");
64 namespace = namespace.replace("-", "_");
65 namespace = namespace.replace("@", ".");
66 namespace = namespace.replace("$", ".");
67 namespace = namespace.replace("#", ".");
68 namespace = namespace.replace("'", ".");
69 namespace = namespace.replace("*", ".");
70 namespace = namespace.replace("+", ".");
71 namespace = namespace.replace(",", ".");
72 namespace = namespace.replace(";", ".");
73 namespace = namespace.replace("=", ".");