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, String>,
17 * where key is namespace prefix and value is package that replaces matched prefix.
19 public class PackageTranslator {
20 private final Map<String, String> namespacePrefixToPackageMap;
22 public PackageTranslator(Map<String, String> namespacePrefixToPackageMap) {
23 this.namespacePrefixToPackageMap = namespacePrefixToPackageMap;
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' .
34 * @throws IllegalStateException
35 * if there is no mapping found.
37 public String getPackageName(Module module) {
38 Entry<String, String> longestMatch = null;
39 int longestMatchLength = 0;
40 String namespace = module.getNamespace().toString();
41 for (Entry<String, String> entry : namespacePrefixToPackageMap
43 if (namespace.startsWith(entry.getKey())
44 && entry.getKey().length() > longestMatchLength) {
46 longestMatchLength = entry.getKey().length();
49 if (longestMatch != null) {
50 return longestMatch.getValue()
51 + sanitizePackage(namespace.substring(longestMatchLength));
53 return BindingGeneratorUtil.moduleNamespaceToPackageName(module);
57 // TODO add to PackageTranslator
58 private static String sanitizePackage(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("=", ".");