blob: a872dc6b7697406aa8905f6f0411d0dcc0886ffd [file] [log] [blame]
/**
********************************************************************************
* Copyright (c) 2019, 2021 Robert Bosch GmbH and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
********************************************************************************
*/
package org.eclipse.app4mc.amalthea.converters.common.utils;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.jdom2.Namespace;
public final class AmaltheaNamespaceRegistry {
private static final LinkedHashMap<String, AmaltheaNamespace> REGISTRY = new LinkedHashMap<>();
private static final String GENERIC_NAMESPACE = "generic";
static {
AmaltheaNamespace generic = new AmaltheaNamespace();
generic.registerMapping("xsi", "http://www.w3.org/2001/XMLSchema-instance");
generic.registerMapping("xmi", "http://www.omg.org/XMI");
REGISTRY.put(GENERIC_NAMESPACE, generic);
AmaltheaNamespace ns070 = new AmaltheaNamespace();
ns070.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.7.0");
REGISTRY.put(ModelVersion._070.getVersion(), ns070);
AmaltheaNamespace ns071 = new AmaltheaNamespace();
ns071.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.7.1");
REGISTRY.put(ModelVersion._071.getVersion(), ns071);
AmaltheaNamespace ns072 = new AmaltheaNamespace();
ns072.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.7.2");
REGISTRY.put(ModelVersion._072.getVersion(), ns072);
AmaltheaNamespace ns080 = new AmaltheaNamespace();
ns080.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.8.0");
REGISTRY.put(ModelVersion._080.getVersion(), ns080);
AmaltheaNamespace ns081 = new AmaltheaNamespace();
ns081.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.8.1");
REGISTRY.put(ModelVersion._081.getVersion(), ns081);
AmaltheaNamespace ns082 = new AmaltheaNamespace();
ns082.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.8.2");
REGISTRY.put(ModelVersion._082.getVersion(), ns082);
AmaltheaNamespace ns083 = new AmaltheaNamespace();
ns083.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.8.3");
REGISTRY.put(ModelVersion._083.getVersion(), ns083);
AmaltheaNamespace ns090 = new AmaltheaNamespace();
ns090.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.0");
REGISTRY.put(ModelVersion._090.getVersion(), ns090);
AmaltheaNamespace ns091 = new AmaltheaNamespace();
ns091.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.1");
REGISTRY.put(ModelVersion._091.getVersion(), ns091);
AmaltheaNamespace ns092 = new AmaltheaNamespace();
ns092.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.2");
REGISTRY.put(ModelVersion._092.getVersion(), ns092);
AmaltheaNamespace ns093 = new AmaltheaNamespace();
ns093.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.3");
REGISTRY.put(ModelVersion._093.getVersion(), ns093);
AmaltheaNamespace ns094 = new AmaltheaNamespace();
ns094.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.4");
REGISTRY.put(ModelVersion._094.getVersion(), ns094);
AmaltheaNamespace ns095 = new AmaltheaNamespace();
ns095.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.5");
REGISTRY.put(ModelVersion._095.getVersion(), ns095);
AmaltheaNamespace ns096 = new AmaltheaNamespace();
ns096.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.6");
REGISTRY.put(ModelVersion._096.getVersion(), ns096);
AmaltheaNamespace ns097 = new AmaltheaNamespace();
ns097.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.7");
REGISTRY.put(ModelVersion._097.getVersion(), ns097);
AmaltheaNamespace ns098 = new AmaltheaNamespace();
ns098.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.8");
REGISTRY.put(ModelVersion._098.getVersion(), ns098);
AmaltheaNamespace ns099 = new AmaltheaNamespace();
ns099.registerMapping("am", "http://app4mc.eclipse.org/amalthea/0.9.9");
REGISTRY.put(ModelVersion._099.getVersion(), ns099);
AmaltheaNamespace ns100 = new AmaltheaNamespace();
ns100.registerMapping("am", "http://app4mc.eclipse.org/amalthea/1.0.0");
REGISTRY.put(ModelVersion._100.getVersion(), ns100);
AmaltheaNamespace ns110 = new AmaltheaNamespace();
ns110.registerMapping("am", "http://app4mc.eclipse.org/amalthea/1.1.0");
REGISTRY.put(ModelVersion._110.getVersion(), ns110);
}
/**
*
* @param namespace The namespace for which the ModelVersion is searched.
* @return The {@link ModelVersion} for the given {@link Namespace}.
*/
public static ModelVersion getModelVersion(Namespace namespace) {
ModelVersion result = null;
if (isNamespaceAvailable(ModelVersion._070, namespace)) {
result = ModelVersion._070;
}
else if (isNamespaceAvailable(ModelVersion._071, namespace)) {
result = ModelVersion._071;
}
else if (isNamespaceAvailable(ModelVersion._072, namespace)) {
result = ModelVersion._072;
}
else if (isNamespaceAvailable(ModelVersion._080, namespace)) {
result = ModelVersion._080;
}
else if (isNamespaceAvailable(ModelVersion._081, namespace)) {
result = ModelVersion._081;
}
else if (isNamespaceAvailable(ModelVersion._082, namespace)) {
result = ModelVersion._082;
}
else if (isNamespaceAvailable(ModelVersion._083, namespace)) {
result = ModelVersion._083;
}
else if (isNamespaceAvailable(ModelVersion._090, namespace)) {
result = ModelVersion._090;
}
else if (isNamespaceAvailable(ModelVersion._091, namespace)) {
result = ModelVersion._091;
}
else if (isNamespaceAvailable(ModelVersion._092, namespace)) {
result = ModelVersion._092;
}
else if (isNamespaceAvailable(ModelVersion._093, namespace)) {
result = ModelVersion._093;
}
else if (isNamespaceAvailable(ModelVersion._094, namespace)) {
result = ModelVersion._094;
}
else if (isNamespaceAvailable(ModelVersion._095, namespace)) {
result = ModelVersion._095;
}
else if (isNamespaceAvailable(ModelVersion._096, namespace)) {
result = ModelVersion._096;
}
else if (isNamespaceAvailable(ModelVersion._097, namespace)) {
result = ModelVersion._097;
}
else if (isNamespaceAvailable(ModelVersion._098, namespace)) {
result = ModelVersion._098;
}
else if (isNamespaceAvailable(ModelVersion._099, namespace)) {
result = ModelVersion._099;
}
else if (isNamespaceAvailable(ModelVersion._100, namespace)) {
result = ModelVersion._100;
}
else if (isNamespaceAvailable(ModelVersion._110, namespace)) {
result = ModelVersion._110;
}
return result;
}
/**
*
* @param version The ModelVersion for the requested Namespace.
* @param prefix The namespace prefix.
* @return The namespace for the given version and prefix.
*/
public static Namespace getNamespace(ModelVersion version, String prefix) {
return getNamespace(version.getVersion(), prefix);
}
/**
*
* @param version The String representation of the ModelVersion for which the
* Namespace is requested.
* @param prefix The namespace prefix.
* @return The namespace for the given version and prefix.
*/
public static Namespace getNamespace(String version, String prefix) {
AmaltheaNamespace ns = REGISTRY.get(version);
if (ns != null) {
return ns.namespaceFor(prefix);
}
return Namespace.getNamespace("", "");
}
public static Namespace[] getAllNamespacesFor(ModelVersion version) {
AmaltheaNamespace ns = REGISTRY.get(version.getVersion());
if (ns != null) {
return ns.getAllNamespaces();
}
return new Namespace[] { Namespace.getNamespace("", "") };
}
public static Namespace[] getAllNamespacesBefore(ModelVersion version, boolean include, boolean includeGeneric) {
ModelVersion[] versionsBefore = ModelVersion.getVersionsBefore(version, include);
List<Namespace> collect = Arrays.stream(versionsBefore)
.map(AmaltheaNamespaceRegistry::getAllNamespacesFor)
.flatMap(Arrays::stream)
.collect(Collectors.toList());
if (includeGeneric) {
AmaltheaNamespace generic = REGISTRY.get(GENERIC_NAMESPACE);
if (generic != null) {
collect.addAll(0, Arrays.asList(generic.getAllNamespaces()));
}
}
if (!collect.isEmpty()) {
return collect.toArray(new Namespace[0]);
}
return new Namespace[] { Namespace.getNamespace("", "") };
}
public static Namespace[] getAllNamespaces() {
List<Namespace> collect = REGISTRY.entrySet().stream()
.map(Map.Entry::getValue)
.map(AmaltheaNamespace::getAllNamespaces)
.flatMap(Arrays::stream)
.collect(Collectors.toList());
if (!collect.isEmpty()) {
return collect.toArray(new Namespace[0]);
}
return new Namespace[] { Namespace.getNamespace("", "") };
}
public static boolean isNamespaceAvailable(ModelVersion version, Namespace namespace) {
if (namespace != null) {
String prefix = namespace.getPrefix();
String uri = namespace.getURI();
AmaltheaNamespace ns = REGISTRY.get(version.getVersion());
if (ns != null && uri.equals(ns.valueOf(prefix))) {
return true;
}
}
return false;
}
public static boolean isPrefixForVersion(ModelVersion version, String prefix) {
AmaltheaNamespace ns = REGISTRY.get(version.getVersion());
return (ns != null && ns.hasPrefix(prefix));
}
public static Namespace getGenericNamespace(String prefix) {
return getNamespace(GENERIC_NAMESPACE, prefix);
}
private AmaltheaNamespaceRegistry() {
// empty constructor for helper class
}
}