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;
12 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
13 import org.opendaylight.yangtools.yang.model.api.Module;
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
20 public class PackageTranslator {
21 private final Map<String, String> namespacePrefixToPackageMap;
23 public PackageTranslator(Map<String, String> namespacePrefixToPackageMap) {
24 this.namespacePrefixToPackageMap = namespacePrefixToPackageMap;
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' .
35 * @throws IllegalStateException
36 * if there is no mapping found.
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
44 if (namespace.startsWith(entry.getKey())
45 && entry.getKey().length() > longestMatchLength) {
47 longestMatchLength = entry.getKey().length();
50 if (longestMatch != null) {
51 return longestMatch.getValue()
52 + sanitizePackage(namespace.substring(longestMatchLength));
54 return BindingGeneratorUtil.moduleNamespaceToPackageName(module);
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("=", ".");