Search in sources :

Example 1 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class BackendUtils method deriveWPD.

/**
 * Derives Winery's Properties Definition from an existing properties definition
 *
 * @param ci     the entity type to try to modify the WPDs
 * @param errors the list to add errors to
 */
public static void deriveWPD(TEntityType ci, List<String> errors) {
    BackendUtils.LOGGER.trace("deriveWPD");
    PropertiesDefinition propertiesDefinition = ci.getPropertiesDefinition();
    QName element = propertiesDefinition.getElement();
    if (element == null) {
        BackendUtils.LOGGER.debug("only works for an element definition, not for types");
    } else {
        BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
        // fetch the XSD defining the element
        final XsdImportManager xsdImportManager = RepositoryFactory.getRepository().getXsdImportManager();
        Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
        RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
        if (ref == null) {
            String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
            BackendUtils.LOGGER.debug(msg);
            errors.add(msg);
            return;
        }
        final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref);
        if (!xsModelOptional.isPresent()) {
            LOGGER.error("no XSModel found");
        }
        XSModel xsModel = xsModelOptional.get();
        XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
        if (elementDeclaration == null) {
            String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
            BackendUtils.LOGGER.debug(msg);
            errors.add(msg);
            return;
        }
        // go through the XSD definition and
        XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
        if (typeDefinition instanceof XSComplexTypeDefinition) {
            XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
            XSParticle particle = cTypeDefinition.getParticle();
            if (particle == null) {
                BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
            } else {
                XSTerm term = particle.getTerm();
                if (term instanceof XSModelGroup) {
                    XSModelGroup modelGroup = (XSModelGroup) term;
                    if (modelGroup.getCompositor() == XSModelGroup.COMPOSITOR_SEQUENCE) {
                        XSObjectList particles = modelGroup.getParticles();
                        int len = particles.getLength();
                        boolean everyThingIsASimpleType = true;
                        PropertyDefinitionKVList list = new PropertyDefinitionKVList();
                        if (len != 0) {
                            for (int i = 0; i < len; i++) {
                                XSParticle innerParticle = (XSParticle) particles.item(i);
                                XSTerm innerTerm = innerParticle.getTerm();
                                if (innerTerm instanceof XSElementDeclaration) {
                                    XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
                                    String name = innerElementDeclaration.getName();
                                    XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
                                    if (innerTypeDefinition instanceof XSSimpleType) {
                                        XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
                                        String typeNS = xsSimpleType.getNamespace();
                                        String typeName = xsSimpleType.getName();
                                        if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                                            PropertyDefinitionKV def = new PropertyDefinitionKV();
                                            def.setKey(name);
                                            // convention at WPD: use "xsd" as prefix for XML Schema Definition
                                            def.setType("xsd:" + typeName);
                                            list.add(def);
                                        } else {
                                            everyThingIsASimpleType = false;
                                            break;
                                        }
                                    } else {
                                        everyThingIsASimpleType = false;
                                        break;
                                    }
                                } else {
                                    everyThingIsASimpleType = false;
                                    break;
                                }
                            }
                        }
                        if (everyThingIsASimpleType) {
                            // everything went allright, we can add a WPD
                            WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
                            wpd.setIsDerivedFromXSD(Boolean.TRUE);
                            wpd.setElementName(element.getLocalPart());
                            wpd.setNamespace(element.getNamespaceURI());
                            wpd.setPropertyDefinitionKVList(list);
                            ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
                            BackendUtils.LOGGER.debug("Successfully generated WPD");
                        } else {
                            BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
                        }
                    } else {
                        BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
                    }
                } else {
                    BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
                }
            }
        } else {
            BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
        }
    }
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSObjectList(org.apache.xerces.xs.XSObjectList) PropertyDefinitionKV(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV) XSParticle(org.apache.xerces.xs.XSParticle) XSTerm(org.apache.xerces.xs.XSTerm) QName(javax.xml.namespace.QName) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Namespace(org.eclipse.winery.common.ids.Namespace) HasTargetNamespace(org.eclipse.winery.model.tosca.HasTargetNamespace) XSComplexTypeDefinition(org.apache.xerces.xs.XSComplexTypeDefinition) XSSimpleType(org.apache.xerces.impl.dv.XSSimpleType) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) PropertiesDefinition(org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) PropertyDefinitionKVList(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKVList) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager) XSModelGroup(org.apache.xerces.xs.XSModelGroup)

Example 2 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class FilebasedRepository method getNamespaces.

private Collection<Namespace> getNamespaces(Collection<Class<? extends DefinitionsChildId>> definitionsChildIds) {
    // we use a HashSet to avoid reporting duplicate namespaces
    Collection<Namespace> res = new HashSet<>();
    for (Class<? extends DefinitionsChildId> id : definitionsChildIds) {
        String rootPathFragment = Util.getRootPathFragment(id);
        Path dir = this.repositoryRoot.resolve(rootPathFragment);
        if (!Files.exists(dir)) {
            continue;
        }
        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);
                res.add(ns);
            }
        } catch (IOException e) {
            FilebasedRepository.LOGGER.debug("Cannot close ds", e);
        }
    }
    return res;
}
Also used : Namespace(org.eclipse.winery.common.ids.Namespace)

Example 3 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class RepositoryBasedXsdImportManager method getAllXsdDefinitions.

/**
 * @param getType true: XSConstants.TYPE_DEFINITION; false: XSConstants.ELEMENT_DECLARATION
 */
private List<NamespaceAndDefinedLocalNames> getAllXsdDefinitions(boolean getType) {
    MutableMultimap<Namespace, String> data = Multimaps.mutable.list.empty();
    SortedSet<XSDImportId> allImports = RepositoryFactory.getRepository().getAllDefinitionsChildIds(XSDImportId.class);
    for (XSDImportId id : allImports) {
        final List<String> allDefinedLocalNames = getAllDefinedLocalNames(id, getType);
        data.putAll(id.getNamespace(), allDefinedLocalNames);
    }
    List<NamespaceAndDefinedLocalNames> result = Lists.mutable.empty();
    data.forEachKeyMultiValues((namespace, strings) -> {
        final NamespaceAndDefinedLocalNames namespaceAndDefinedLocalNames = new NamespaceAndDefinedLocalNames(namespace);
        strings.forEach(localName -> namespaceAndDefinedLocalNames.addLocalName(localName));
        result.add(namespaceAndDefinedLocalNames);
    });
    return result;
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) Namespace(org.eclipse.winery.common.ids.Namespace)

Example 4 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class Showcases method zipTypeTest.

@Test
public void zipTypeTest() throws Exception {
    String name = "Showcase.csar";
    InputStream inputStream = new FileInputStream(path + File.separator + name);
    Converter converter = new Converter();
    converter.convertY2X(inputStream);
    TServiceTemplate serviceTemplate = RepositoryFactory.getRepository().getElement(new ServiceTemplateId(new Namespace(Namespaces.DEFAULT_NS, false), new XmlId("Showcase", false)));
    Assert.assertNotNull(serviceTemplate);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Converter(org.eclipse.winery.yaml.converter.Converter) XmlId(org.eclipse.winery.common.ids.XmlId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) FileInputStream(java.io.FileInputStream) Namespace(org.eclipse.winery.common.ids.Namespace) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) Test(org.junit.Test)

Example 5 with Namespace

use of org.eclipse.winery.common.ids.Namespace in project winery by eclipse.

the class ImportUtilsTest method getLocationForImportTest.

@Test
public void getLocationForImportTest() throws Exception {
    this.setRevisionTo("5fdcffa9ccd17743d5498cab0914081fc33606e9");
    XSDImportId id = new XSDImportId(new Namespace("http://opentosca.org/nodetypes", false), new XmlId("CloudProviderProperties", false));
    Optional<String> importLocation = ImportUtils.getLocation(id);
    Assert.assertEquals(true, importLocation.isPresent());
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) XmlId(org.eclipse.winery.common.ids.XmlId) Namespace(org.eclipse.winery.common.ids.Namespace) Test(org.junit.Test)

Aggregations

Namespace (org.eclipse.winery.common.ids.Namespace)13 QName (javax.xml.namespace.QName)5 XmlId (org.eclipse.winery.common.ids.XmlId)5 RepositoryFactory (org.eclipse.winery.repository.backend.RepositoryFactory)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Collectors (java.util.stream.Collectors)3 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)3 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)3 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)3 PropertyDefinitionKV (org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV)3 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition)3 IRepository (org.eclipse.winery.repository.backend.IRepository)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 FileInputStream (java.io.FileInputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 java.util (java.util)2 Stream (java.util.stream.Stream)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2