use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.
the class ServiceTemplateComplianceRuleRuleChecker method getRuleIds.
public List<ComplianceRuleId> getRuleIds(TServiceTemplate serviceTemplate) {
List<ComplianceRuleId> complianceRules = Lists.newArrayList();
Namespace namespace = new Namespace(serviceTemplate.getTargetNamespace(), false);
Collection<Namespace> componentsNamespaces = RepositoryFactory.getRepository().getComponentsNamespaces(ComplianceRuleId.class);
List<Namespace> relevantNamespaces = componentsNamespaces.stream().filter(ns -> namespace.getDecoded().startsWith(ns.getDecoded())).collect(Collectors.toList());
for (Namespace space : relevantNamespaces) {
complianceRules.addAll((Collection<? extends ComplianceRuleId>) ((FilebasedRepository) RepositoryFactory.getRepository()).getAllIdsInNamespace(ComplianceRuleId.class, space));
}
return complianceRules;
}
use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.
the class Showcases method convert.
public MultiException convert(String path, String namespace, Stream<String> files) throws Exception {
MultiException exception = new MultiException();
files.map(name -> {
try {
return new LinkedHashMap.SimpleEntry<>(name, readServiceTemplate(path + File.separator + name));
} catch (Exception e) {
exception.add(e);
}
return null;
}).filter(Objects::nonNull).map(entry -> new LinkedHashMap.SimpleEntry<>(entry.getKey(), convert(entry.getValue(), entry.getKey(), namespace))).forEach(entry -> WriterUtils.saveDefinitions(entry.getValue(), outPath, namespace, entry.getKey()));
if (exception.hasException()) {
throw exception.getException();
}
return exception;
}
use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.
the class ConsistencyChecker method checkNamespaceUri.
public static void checkNamespaceUri(ConsistencyErrorLogger errorLogger, EnumSet<ConsistencyCheckerVerbosity> verbosity, DefinitionsChildId id) {
Objects.requireNonNull(verbosity);
Objects.requireNonNull(id);
String uriStr = id.getNamespace().getDecoded();
if (!uriStr.trim().equals(uriStr)) {
printAndAddError(errorLogger, verbosity, id, "Namespace starts or ends with white spaces");
}
URI uri;
try {
uri = new URI(uriStr);
} catch (URISyntaxException e) {
LOGGER.debug("Invalid URI", e);
printAndAddError(errorLogger, verbosity, id, "Invalid URI: " + e.getMessage());
return;
}
if (!uri.isAbsolute()) {
printAndAddError(errorLogger, verbosity, id, "URI is relative");
}
if ((uriStr.startsWith("http://www.opentosca.org/") && (!uriStr.toLowerCase().equals(uriStr)))) {
printAndAddError(errorLogger, verbosity, id, "opentosca URI is not lowercase");
}
if (uriStr.endsWith("/")) {
printAndAddError(errorLogger, verbosity, id, "URI ends with a slash");
}
if (uriStr.contains(ARTEFACT_BE)) {
printAndAddError(errorLogger, verbosity, id, "artifact is spelled with i in American English, not artefact as in British English");
}
boolean namespaceUriContainsDifferentType = DefinitionsChildId.ALL_TOSCA_COMPONENT_ID_CLASSES.stream().filter(definitionsChildIdClass -> !definitionsChildIdClass.isAssignableFrom(id.getClass())).flatMap(definitionsChildIdClass -> {
final String lowerCaseIdClass = Util.getTypeForComponentId(definitionsChildIdClass).toLowerCase();
return Stream.of(lowerCaseIdClass + "s", lowerCaseIdClass + "/");
}).anyMatch(definitionsChildName -> uriStr.contains(definitionsChildName));
if (namespaceUriContainsDifferentType) {
printAndAddError(errorLogger, verbosity, id, "Namespace URI contains tosca definitions name from other type. E.g., Namespace is ...servicetemplates..., but the type is an artifact template");
}
}
use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.
the class ConsistencyChecker method checkPropertiesValidation.
public static void checkPropertiesValidation(ConsistencyErrorLogger errorLogger, ConsistencyCheckerConfiguration configuration, DefinitionsChildId id) {
if (id instanceof EntityTemplateId) {
TEntityTemplate entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions(id).getElement();
if (Objects.isNull(entityTemplate.getType())) {
// no printing necessary; type consistency is checked at other places
return;
}
final TEntityType entityType = configuration.getRepository().getTypeForTemplate(entityTemplate);
final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
final TEntityType.PropertiesDefinition propertiesDefinition = entityType.getPropertiesDefinition();
if ((winerysPropertiesDefinition != null) || (propertiesDefinition != null)) {
final TEntityTemplate.Properties properties = entityTemplate.getProperties();
if (properties == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties defined");
return;
}
if (winerysPropertiesDefinition != null) {
Map<String, String> kvProperties = entityTemplate.getProperties().getKVProperties();
if (kvProperties.isEmpty()) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties set (any case)");
return;
}
for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitionKVList().getPropertyDefinitionKVs()) {
String key = propertyDefinitionKV.getKey();
if (kvProperties.get(key) == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Property " + key + " required, but not set.");
} else {
// remove the key from the map to enable checking below whether a property is defined which not requried by the property definition
kvProperties.remove(key);
}
}
// If any key is left, this is a key not defined at the schema
for (Object o : kvProperties.keySet()) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Property " + o + " set, but not defined at schema.");
}
} else if (propertiesDefinition != null) {
@Nullable final Object any = properties.getAny();
if (any == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties defined (any case)");
return;
}
@Nullable final QName element = propertiesDefinition.getElement();
if (element != null) {
final Map<String, RepositoryFileReference> mapFromLocalNameToXSD = configuration.getRepository().getXsdImportManager().getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
final RepositoryFileReference repositoryFileReference = mapFromLocalNameToXSD.get(element.getLocalPart());
if (repositoryFileReference == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "No Xml Schema definition found for " + element);
return;
}
validate(errorLogger, repositoryFileReference, any, configuration, id);
}
}
}
}
}
use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.
the class FilebasedRepository method getAllDefinitionsChildIds.
@Override
public <T extends DefinitionsChildId> SortedSet<T> getAllDefinitionsChildIds(Class<T> idClass) {
SortedSet<T> res = new TreeSet<>();
String rootPathFragment = Util.getRootPathFragment(idClass);
Path dir = this.repositoryRoot.resolve(rootPathFragment);
if (!Files.exists(dir)) {
// return empty list if no ids are available
return res;
}
assert (Files.isDirectory(dir));
final OnlyNonHiddenDirectories onhdf = new OnlyNonHiddenDirectories();
// list all directories contained in this directory
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, onhdf)) {
for (Path nsP : ds) {
// the current path is the namespace
Namespace ns = new Namespace(nsP.getFileName().toString(), true);
try (DirectoryStream<Path> idDS = Files.newDirectoryStream(nsP, onhdf)) {
for (Path idP : idDS) {
XmlId xmlId = new XmlId(idP.getFileName().toString(), true);
Constructor<T> constructor;
try {
constructor = idClass.getConstructor(Namespace.class, XmlId.class);
} catch (Exception e) {
FilebasedRepository.LOGGER.debug("Internal error at determining id constructor", e);
// abort everything, return invalid result
return res;
}
T id;
try {
id = constructor.newInstance(ns, xmlId);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
FilebasedRepository.LOGGER.debug("Internal error at invocation of id constructor", e);
// abort everything, return invalid result
return res;
}
res.add(id);
}
}
}
} catch (IOException e) {
FilebasedRepository.LOGGER.debug("Cannot close ds", e);
}
return res;
}
Aggregations