Search in sources :

Example 6 with PropertyDefinitionKV

use of org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV 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);
                }
            }
        }
    }
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV) QName(javax.xml.namespace.QName) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Namespace(org.eclipse.winery.common.ids.Namespace) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Aggregations

PropertyDefinitionKV (org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV)6 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition)4 PropertyDefinitionKVList (org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKVList)3 QName (javax.xml.namespace.QName)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)2 Namespace (org.eclipse.winery.common.ids.Namespace)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 JAXBElement (javax.xml.bind.JAXBElement)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 XSSimpleType (org.apache.xerces.impl.dv.XSSimpleType)1 XSComplexTypeDefinition (org.apache.xerces.xs.XSComplexTypeDefinition)1 XSElementDeclaration (org.apache.xerces.xs.XSElementDeclaration)1 XSModel (org.apache.xerces.xs.XSModel)1 XSModelGroup (org.apache.xerces.xs.XSModelGroup)1 XSObjectList (org.apache.xerces.xs.XSObjectList)1 XSParticle (org.apache.xerces.xs.XSParticle)1 XSTerm (org.apache.xerces.xs.XSTerm)1