Search in sources :

Example 1 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class BackendUtils method getRepositoryFileReference.

public static RepositoryFileReference getRepositoryFileReference(Path rootPath, Path path, DirectoryId directoryId) {
    final Path relativePath = rootPath.relativize(path);
    Path parent = relativePath.getParent();
    if (parent == null) {
        return new RepositoryFileReference(directoryId, path.getFileName().toString());
    } else {
        return new RepositoryFileReference(directoryId, parent, path.getFileName().toString());
    }
}
Also used : Path(java.nio.file.Path) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 2 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference 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 3 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class BackendUtils method getTArtifactTemplate.

/**
 * @param directoryId DirectoryID of the TArtifactTemplate that should be returned.
 * @return The TArtifactTemplate corresponding to the directoryId.
 */
public static TArtifactTemplate getTArtifactTemplate(DirectoryId directoryId) {
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
    try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
        Unmarshaller u = JAXBSupport.createUnmarshaller();
        Definitions defs = ((Definitions) u.unmarshal(is));
        for (TExtensibleElements elem : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
            if (elem instanceof TArtifactTemplate) {
                return (TArtifactTemplate) elem;
            }
        }
    } catch (IOException e) {
        LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
    } catch (JAXBException e) {
        LOGGER.error("Error in XML in " + ref.getFileName(), e);
    }
    return null;
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) JAXBException(javax.xml.bind.JAXBException) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class BackendUtils method synchronizeReferences.

/**
 * Synchronizes the known plans with the data in the XML. When there is a stored file, but no known entry in the
 * XML, we guess "BPEL" as language and "build plan" as type.
 */
public static void synchronizeReferences(ServiceTemplateId id) throws IOException {
    final IRepository repository = RepositoryFactory.getRepository();
    final TServiceTemplate serviceTemplate = repository.getElement(id);
    // locally stored plans
    TPlans plans = serviceTemplate.getPlans();
    // plans stored in the repository
    PlansId plansContainerId = new PlansId(id);
    SortedSet<PlanId> nestedPlans = repository.getNestedIds(plansContainerId, PlanId.class);
    Set<PlanId> plansToAdd = new HashSet<>();
    plansToAdd.addAll(nestedPlans);
    if (nestedPlans.isEmpty()) {
        if (plans == null) {
            // data on the file system equals the data -> no plans
            return;
        } else {
        // noinspection StatementWithEmptyBody
        // we have to check for equality later
        }
    }
    if (plans == null) {
        plans = new TPlans();
        serviceTemplate.setPlans(plans);
    }
    for (Iterator<TPlan> iterator = plans.getPlan().iterator(); iterator.hasNext(); ) {
        TPlan plan = iterator.next();
        if (plan.getPlanModel() != null) {
            // in case, a plan is directly contained in a Model element, we do not need to do anything
            continue;
        }
        TPlan.PlanModelReference planModelReference;
        if ((planModelReference = plan.getPlanModelReference()) != null) {
            String ref = planModelReference.getReference();
            if ((ref == null) || ref.startsWith("../")) {
                // special case (due to errors in the importer): empty PlanModelReference field
                if (plan.getId() == null) {
                    // invalid plan entry: no id.
                    // we remove the entry
                    iterator.remove();
                    continue;
                }
                PlanId planId = new PlanId(plansContainerId, new XmlId(plan.getId(), false));
                if (nestedPlans.contains(planId)) {
                    // everything allright
                    // we do NOT need to add the plan on the HDD to the XML
                    plansToAdd.remove(planId);
                } else {
                    // no local storage for the plan, we remove it from the XML
                    iterator.remove();
                }
            }
        }
    }
    // add all plans locally stored, but not contained in the XML, as plan element to the plans of the service template.
    List<TPlan> thePlans = plans.getPlan();
    for (PlanId planId : plansToAdd) {
        SortedSet<RepositoryFileReference> files = repository.getContainedFiles(planId);
        if (files.size() != 1) {
            throw new IllegalStateException("Currently, only one file per plan is supported.");
        }
        RepositoryFileReference ref = files.iterator().next();
        TPlan plan = new TPlan();
        plan.setId(planId.getXmlId().getDecoded());
        plan.setName(planId.getXmlId().getDecoded());
        plan.setPlanType(org.eclipse.winery.repository.Constants.TOSCA_PLANTYPE_BUILD_PLAN);
        plan.setPlanLanguage(Namespaces.URI_BPEL20_EXECUTABLE);
        // create a PlanModelReferenceElement pointing to that file
        String path = Util.getUrlPath(ref);
        // path is relative from the definitions element
        path = "../" + path;
        TPlan.PlanModelReference pref = new TPlan.PlanModelReference();
        pref.setReference(path);
        plan.setPlanModelReference(pref);
        thePlans.add(plan);
    }
    if (serviceTemplate.getPlans().getPlan().isEmpty()) {
        serviceTemplate.setPlans(null);
    }
    RepositoryFactory.getRepository().setElement(id, serviceTemplate);
}
Also used : TPlans(org.eclipse.winery.model.tosca.TPlans) PlanId(org.eclipse.winery.common.ids.elements.PlanId) PlansId(org.eclipse.winery.common.ids.elements.PlansId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) TPlan(org.eclipse.winery.model.tosca.TPlan) XmlId(org.eclipse.winery.common.ids.XmlId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) HashSet(java.util.HashSet)

Example 5 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class BackendUtils method synchronizeReferences.

public static void synchronizeReferences(ArtifactTemplateId id) throws IOException {
    TArtifactTemplate template = RepositoryFactory.getRepository().getElement(id);
    DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
    SortedSet<RepositoryFileReference> files = RepositoryFactory.getRepository().getContainedFiles(fileDir);
    if (files.isEmpty()) {
        // clear artifact references
        template.setArtifactReferences(null);
    } else {
        TArtifactTemplate.ArtifactReferences artifactReferences = new TArtifactTemplate.ArtifactReferences();
        template.setArtifactReferences(artifactReferences);
        List<TArtifactReference> artRefList = artifactReferences.getArtifactReference();
        for (RepositoryFileReference ref : files) {
            // determine path
            // path relative from the root of the CSAR is ok (COS01, line 2663)
            // double encoded - see ADR-0003
            String path = Util.getUrlPath(ref);
            // put path into data structure
            // we do not use Include/Exclude as we directly reference a concrete file
            TArtifactReference artRef = new TArtifactReference();
            artRef.setReference(path);
            artRefList.add(artRef);
        }
    }
    BackendUtils.persist(id, template);
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference)

Aggregations

RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)51 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)8 Definitions (org.eclipse.winery.model.tosca.Definitions)8 BufferedInputStream (java.io.BufferedInputStream)6 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)6 Test (org.junit.Test)6 Path (java.nio.file.Path)5 JAXBException (javax.xml.bind.JAXBException)5 MediaType (org.apache.tika.mime.MediaType)5 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)5 ZipInputStream (java.util.zip.ZipInputStream)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 QName (javax.xml.namespace.QName)4 XmlId (org.eclipse.winery.common.ids.XmlId)4 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)4 XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)4 PlanId (org.eclipse.winery.common.ids.elements.PlanId)4 PlansId (org.eclipse.winery.common.ids.elements.PlansId)4