Search in sources :

Example 1 with ConfigurationBasedNamespaceManager

use of org.eclipse.winery.repository.backend.filebased.ConfigurationBasedNamespaceManager in project winery by eclipse.

the class CsarImporter method importNamespacePrefixes.

/**
 * Import namespace prefixes. This is kind of a quick hack. TODO: during the import, the prefixes should be
 * extracted using JAXB and stored in the NamespacesResource
 *
 * @param rootPath the root path of the extracted CSAR
 */
private void importNamespacePrefixes(Path rootPath) {
    NamespaceManager namespaceManager = targetRepository.getNamespaceManager();
    Path properties = rootPath.resolve(CsarExporter.PATH_TO_NAMESPACES_PROPERTIES);
    Path json = rootPath.resolve(CsarExporter.PATH_TO_NAMESPACES_JSON);
    if (Files.exists(properties) || Files.exists(json)) {
        NamespaceManager localNamespaceManager;
        if (Files.exists(properties)) {
            PropertiesConfiguration pconf = new PropertiesConfiguration();
            try (final BufferedReader propertyReader = Files.newBufferedReader(properties)) {
                pconf.read(propertyReader);
                localNamespaceManager = new ConfigurationBasedNamespaceManager(pconf);
            } catch (IOException | ConfigurationException e) {
                CsarImporter.LOGGER.debug(e.getMessage(), e);
                return;
            }
        } else {
            localNamespaceManager = new JsonBasedNamespaceManager(json.toFile());
        }
        for (String s : localNamespaceManager.getAllNamespaces().keySet()) {
            boolean addToStorage = false;
            String namespace = s;
            if (namespaceManager.hasPermanentProperties(namespace)) {
                String storedPrefix = namespaceManager.getPrefix(namespace);
                // QUICK HACK to check whether the prefix is a generated one
                // We assume we know the internal generation routine
                Matcher m = CsarImporter.GENERATED_PREFIX_PATTERN.matcher(storedPrefix);
                if (m.matches()) {
                    // the stored prefix is a generated one
                    // replace it by the one stored in the exported properties
                    addToStorage = true;
                }
            } else {
                addToStorage = true;
            }
            if (addToStorage) {
                String prefix = localNamespaceManager.getPrefix(namespace);
                namespaceManager.setNamespaceProperties(namespace, new NamespaceProperties(namespace, prefix));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) JsonBasedNamespaceManager(org.eclipse.winery.repository.backend.filebased.JsonBasedNamespaceManager) ConfigurationBasedNamespaceManager(org.eclipse.winery.repository.backend.filebased.ConfigurationBasedNamespaceManager) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) ConfigurationBasedNamespaceManager(org.eclipse.winery.repository.backend.filebased.ConfigurationBasedNamespaceManager) Matcher(java.util.regex.Matcher) PathMatcher(java.nio.file.PathMatcher) IOException(java.io.IOException) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) JsonBasedNamespaceManager(org.eclipse.winery.repository.backend.filebased.JsonBasedNamespaceManager) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) NamespaceProperties(org.eclipse.winery.repository.backend.filebased.NamespaceProperties) BufferedReader(java.io.BufferedReader)

Aggregations

BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1 Matcher (java.util.regex.Matcher)1 PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)1 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)1 NamespaceManager (org.eclipse.winery.repository.backend.NamespaceManager)1 ConfigurationBasedNamespaceManager (org.eclipse.winery.repository.backend.filebased.ConfigurationBasedNamespaceManager)1 JsonBasedNamespaceManager (org.eclipse.winery.repository.backend.filebased.JsonBasedNamespaceManager)1 NamespaceProperties (org.eclipse.winery.repository.backend.filebased.NamespaceProperties)1