Search in sources :

Example 1 with XSDImportId

use of org.eclipse.winery.common.ids.definitions.imports.XSDImportId in project winery by eclipse.

the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.

public static Definitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
    final Definitions definitions = createWrapperDefinitions(id);
    HasIdInIdOrNameField element;
    if (id instanceof RelationshipTypeImplementationId) {
        element = new TRelationshipTypeImplementation();
    } else if (id instanceof NodeTypeImplementationId) {
        element = new TNodeTypeImplementation();
    } else if (id instanceof RequirementTypeId) {
        element = new TRequirementType();
    } else if (id instanceof NodeTypeId) {
        element = new TNodeType();
    } else if (id instanceof RelationshipTypeId) {
        element = new TRelationshipType();
    } else if (id instanceof CapabilityTypeId) {
        element = new TCapabilityType();
    } else if (id instanceof ArtifactTypeId) {
        element = new TArtifactType();
    } else if (id instanceof PolicyTypeId) {
        element = new TPolicyType();
    } else if (id instanceof PolicyTemplateId) {
        element = new TPolicyTemplate();
    } else if (id instanceof ServiceTemplateId) {
        element = new TServiceTemplate();
    } else if (id instanceof ArtifactTemplateId) {
        element = new TArtifactTemplate();
    } else if (id instanceof XSDImportId) {
        // TImport has no id; thus directly generating it without setting an id
        TImport tImport = new TImport();
        definitions.setElement(tImport);
        return definitions;
    } else {
        throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
    }
    copyIdToFields(element, id);
    definitions.setElement((TExtensibleElements) element);
    return definitions;
}
Also used : ArtifactTypeId(org.eclipse.winery.common.ids.definitions.ArtifactTypeId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TImport(org.eclipse.winery.model.tosca.TImport) TCapabilityType(org.eclipse.winery.model.tosca.TCapabilityType) TRequirementType(org.eclipse.winery.model.tosca.TRequirementType) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TPolicyType(org.eclipse.winery.model.tosca.TPolicyType) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) NodeTypeImplementationId(org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId) XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) RelationshipTypeId(org.eclipse.winery.common.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) RelationshipTypeImplementationId(org.eclipse.winery.common.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) PolicyTypeId(org.eclipse.winery.common.ids.definitions.PolicyTypeId) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) PolicyTemplateId(org.eclipse.winery.common.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.common.ids.definitions.NodeTypeId)

Example 2 with XSDImportId

use of org.eclipse.winery.common.ids.definitions.imports.XSDImportId 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 3 with XSDImportId

use of org.eclipse.winery.common.ids.definitions.imports.XSDImportId in project winery by eclipse.

the class CsarImporter method adjustEntityType.

/**
 * All EntityTypes may contain properties definition. In case a winery properties definition is found, the TOSCA
 * conforming properties definition is removed
 *
 * @param ci      the entity type
 * @param wid     the Winery id of the entitytype
 * @param newDefs the definitions, the entiy type is contained in. The imports might be adjusted here
 * @param errors  Used to collect the errors
 */
private static void adjustEntityType(TEntityType ci, EntityTypeId wid, Definitions newDefs, final List<String> errors) {
    PropertiesDefinition propertiesDefinition = ci.getPropertiesDefinition();
    if (propertiesDefinition != null) {
        WinerysPropertiesDefinition winerysPropertiesDefinition = ModelUtilities.getWinerysPropertiesDefinition(ci);
        boolean deriveWPD;
        if (winerysPropertiesDefinition == null) {
            deriveWPD = true;
        } else {
            if (winerysPropertiesDefinition.getIsDerivedFromXSD() == null) {
                // if the winery's properties are defined by Winery itself,
                // remove the TOSCA conforming properties definition as a Winery properties definition exists (and which takes precedence)
                ci.setPropertiesDefinition(null);
                // no derivation from properties required as the properties are generated by Winery
                deriveWPD = false;
                // we have to remove the import, too
                // Determine the location
                String elementName = winerysPropertiesDefinition.getElementName();
                String loc = BackendUtils.getImportLocationForWinerysPropertiesDefinitionXSD(wid, null, elementName);
                // remove the import matching that location
                List<TImport> imports = newDefs.getImport();
                boolean found = false;
                if (imports != null) {
                    Iterator<TImport> iterator = imports.iterator();
                    TImport imp;
                    while (iterator.hasNext()) {
                        imp = iterator.next();
                        // TODO: add check for QNames.QNAME_WINERYS_PROPERTIES_DEFINITION_ATTRIBUTE instead of import location. The current routine, however, works, too.
                        if (imp.getLocation().equals(loc)) {
                            found = true;
                            break;
                        }
                    }
                    // noinspection StatementWithEmptyBody
                    if (found) {
                        // imp with Winery's k/v location found
                        iterator.remove();
                        // the XSD has been imported in importOtherImport
                        // it was too difficult to do the location check there, therefore we just remove the XSD from the repository here
                        XSDImportId importId = new XSDImportId(winerysPropertiesDefinition.getNamespace(), elementName, false);
                        try {
                            RepositoryFactory.getRepository().forceDelete(importId);
                        } catch (IOException e) {
                            CsarImporter.LOGGER.debug("Could not delete Winery's generated XSD definition", e);
                            errors.add("Could not delete Winery's generated XSD definition");
                        }
                    } else {
                    // K/V properties definition was incomplete
                    }
                }
            } else {
                // winery's properties are derived from an XSD
                // The export does NOT add an imports statement: only the wpd exists
                // We remove that as
                ModelUtilities.removeWinerysPropertiesDefinition(ci);
                // derive the WPDs again from the properties definition
                deriveWPD = true;
            }
        }
        if (deriveWPD) {
            BackendUtils.deriveWPD(ci, errors);
        }
    }
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) PropertiesDefinition(org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) IOException(java.io.IOException)

Example 4 with XSDImportId

use of org.eclipse.winery.common.ids.definitions.imports.XSDImportId in project winery by eclipse.

the class CsarImporter method importTypes.

/**
 * Imports the specified types into the repository. The types are converted to an import statement
 *
 * @param errors Container for error messages
 */
private void importTypes(TDefinitions defs, final List<String> errors) {
    Types typesContainer = defs.getTypes();
    if (typesContainer != null) {
        List<Object> types = typesContainer.getAny();
        for (Object type : types) {
            if (type instanceof Element) {
                Element element = (Element) type;
                // generate id part of ImportId out of definitions' id
                // we do not use the name as the name has to be URLencoded again and we have issues with the interplay with org.eclipse.winery.common.ids.definitions.imports.GenericImportId.getId(TImport) then.
                String id = defs.getId();
                // try to  make the id unique by hashing the "content" of the definition
                id = id + "-" + Integer.toHexString(element.hashCode());
                // set importId
                DefinitionsChildId importId;
                String ns;
                if (element.getNamespaceURI().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                    ns = element.getAttribute("targetNamespace");
                    importId = new XSDImportId(ns, id, false);
                } else {
                    // Quick hack for non-XML-Schema-definitions
                    ns = "unknown";
                    importId = new GenericImportId(ns, id, false, element.getNamespaceURI());
                }
                // Following code is adapted from importOtherImports
                TDefinitions wrapperDefs = BackendUtils.createWrapperDefinitions(importId);
                TImport imp = new TImport();
                String fileName = id + ".xsd";
                imp.setLocation(fileName);
                imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                imp.setNamespace(ns);
                wrapperDefs.getImport().add(imp);
                CsarImporter.storeDefinitions(importId, wrapperDefs);
                // put the file itself to the repo
                // ref is required to generate fileRef
                RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(importId);
                RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), fileName);
                // convert element to document
                // QUICK HACK. Alternative: Add new method RepositoryFactory.getRepository().getOutputStream and transform DOM node to OuptputStream
                String content = Util.getXMLAsString(element);
                try {
                    RepositoryFactory.getRepository().putContentToFile(fileRef, content, MediaTypes.MEDIATYPE_TEXT_XML);
                } catch (IOException e) {
                    CsarImporter.LOGGER.debug("Could not put XML Schema definition to file " + fileRef.toString(), e);
                    errors.add("Could not put XML Schema definition to file " + fileRef.toString());
                }
                // add import to definitions
                // adapt path - similar to importOtherImport
                String newLoc = "../" + Util.getUrlPath(fileRef);
                imp.setLocation(newLoc);
                defs.getImport().add(imp);
            } else {
                // This is a known type. Otherwise JAX-B would render it as Element
                errors.add("There is a Type of class " + type.getClass().toString() + " which is unknown to Winery. The type element is imported as is");
            }
        }
    }
}
Also used : Types(org.eclipse.winery.model.tosca.TDefinitions.Types) MediaTypes(org.eclipse.winery.repository.backend.constants.MediaTypes) XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Element(org.w3c.dom.Element) IOException(java.io.IOException) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId)

Example 5 with XSDImportId

use of org.eclipse.winery.common.ids.definitions.imports.XSDImportId in project winery by eclipse.

the class WriterUtils method storeTypes.

public static void storeTypes(Path path, String namespace, String id) {
    LOGGER.debug("Store type: {}", id);
    try {
        MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
        TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
        builder.setNamespace(namespace);
        builder.setLocation(id + ".xsd");
        GenericImportId rid = new XSDImportId(namespace, id, false);
        TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid);
        definitions.getImport().add(builder.build());
        CsarImporter.storeDefinitions(rid, definitions);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
        List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
        for (File file : files) {
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
            RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
            RepositoryFactory.getRepository().putContentToFile(fileRef, stream, mediaType);
        }
    } catch (IllegalArgumentException | IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) TImport(org.eclipse.winery.model.tosca.TImport) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) MediaType(org.apache.tika.mime.MediaType) Files(java.nio.file.Files) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Aggregations

XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)8 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)4 IOException (java.io.IOException)3 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)3 MediaType (org.apache.tika.mime.MediaType)2 Namespace (org.eclipse.winery.common.ids.Namespace)2 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)2 TImport (org.eclipse.winery.model.tosca.TImport)2 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 Files (java.nio.file.Files)1 ZipInputStream (java.util.zip.ZipInputStream)1 JAXBException (javax.xml.bind.JAXBException)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 XmlId (org.eclipse.winery.common.ids.XmlId)1 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)1 ArtifactTypeId (org.eclipse.winery.common.ids.definitions.ArtifactTypeId)1 CapabilityTypeId (org.eclipse.winery.common.ids.definitions.CapabilityTypeId)1 NodeTypeId (org.eclipse.winery.common.ids.definitions.NodeTypeId)1