blob: 767b3682ac45319a82e318752c3bd83f09351039 [file] [log] [blame]
/*********************************************************************************
* Copyright (c) 2020 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.converters097.utils;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.app4mc.amalthea.converters.common.ServiceConstants;
import org.eclipse.app4mc.amalthea.converters.common.base.ICache;
import org.eclipse.app4mc.amalthea.converters.common.utils.AmaltheaNamespaceRegistry;
import org.eclipse.app4mc.amalthea.converters.common.utils.HelperUtil;
import org.eclipse.app4mc.amalthea.converters.common.utils.ModelVersion;
import org.jdom2.Document;
import org.jdom2.Element;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(
property = ServiceConstants.INPUT_MODEL_VERSION_PROPERTY + "=0.9.6",
service= {ICache.class, ComponentPortInterfaceCacheBuilder.class})
public class ComponentPortInterfaceCacheBuilder implements ICache {
private static final Logger LOGGER = LoggerFactory.getLogger(ComponentPortInterfaceCacheBuilder.class);
public static final String CACHE_KEY = "ComponentPort_Interface_Names";
private final HashMap<File, Map<String, Object>> map = new HashMap<>();
@Override
public void buildCache(Map<File, Document> fileDocumentMapping) {
LOGGER.info("Build up ComponentPortInterfaceNameCache for 0.9.6");
HashSet<String> uniqueInterfaceNames = new HashSet<String>();
for (File targetFile : fileDocumentMapping.keySet()) {
Document document1 = fileDocumentMapping.get(targetFile);
if (document1 == null) {
// log error message
continue;
}
Element rootElement = document1.getRootElement();
StringBuilder xpathStr = new StringBuilder();
xpathStr.append("./componentsModel/components/ports[@interfaceName]");
final List<Element> componentPorts = HelperUtil.getXpathResult(
rootElement,
xpathStr.toString(),
Element.class,
AmaltheaNamespaceRegistry.getNamespace(ModelVersion._097, "am"),
AmaltheaNamespaceRegistry.getGenericNamespace("xsi"));
for (Element port : componentPorts) {
String attrVal = port.getAttributeValue("interfaceName");
if(attrVal != null) {
uniqueInterfaceNames.add(attrVal);
}
}
}
//construct a simple list of all interfaceNames without any duplicate.
//get the first file from the map as the target destination file where all unique interfaces will be created.
HashMap<String, Object> cache = new HashMap<String, Object>();
cache.put(CACHE_KEY, uniqueInterfaceNames);
Entry<File, Document> firstEntry = fileDocumentMapping.entrySet().iterator().next();
File targetFile = firstEntry.getKey();
this.map.put(targetFile, cache);
}
@Override
public Map<File, Map<String, Object>> getCacheMap() {
return this.map;
}
@Override
public void clearCacheMap() {
this.map.clear();
}
}