Search in sources :

Example 6 with XSDImportId

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

Example 7 with XSDImportId

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

the class RepositoryBasedXsdImportManager method getMapFromLocalNameToXSD.

@Override
public Map<String, RepositoryFileReference> getMapFromLocalNameToXSD(final Namespace namespace, final boolean getTypes) {
    Set<XSDImportId> importsOfNamespace = this.getImportsOfNamespace(namespace);
    Map<String, RepositoryFileReference> result = new HashMap<>();
    for (XSDImportId imp : importsOfNamespace) {
        final List<String> allDefinedLocalNames = this.getAllDefinedLocalNames(namespace, getTypes);
        Optional<RepositoryFileReference> ref = getXsdFileReference(imp);
        if (!ref.isPresent()) {
            LOGGER.error("Ref is not defined");
        } else {
            for (String localName : allDefinedLocalNames) {
                result.put(localName, ref.get());
            }
        }
    }
    return result;
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 8 with XSDImportId

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

the class CsarImporter method importOtherImport.

/**
 * SIDE EFFECT: modifies the location of imp to point to the correct relative location (when read from the exported
 * CSAR)
 *
 * @param rootPath the absolute path where to resolve files from
 */
private void importOtherImport(Path rootPath, TImport imp, final List<String> errors, String type, boolean overwrite) {
    assert (!type.equals(Namespaces.TOSCA_NAMESPACE));
    String loc = imp.getLocation();
    if (!Util.isRelativeURI(loc)) {
        // This is just an information message
        errors.add("Absolute URIs are not resolved by Winery (" + loc + ")");
        return;
    }
    // location URLs are encoded: http://www.w3.org/TR/2001/WD-charmod-20010126/#sec-URIs, RFC http://www.ietf.org/rfc/rfc2396.txt
    loc = Util.URLdecode(loc);
    Path path;
    try {
        path = rootPath.resolve(loc);
    } catch (Exception e) {
        // java.nio.file.InvalidPathException could be thrown which is a RuntimeException
        errors.add(e.getMessage());
        return;
    }
    if (!Files.exists(path)) {
        // fallback for older CSARs, where the location is given from the root
        path = rootPath.getParent().resolve(loc);
        if (!Files.exists(path)) {
            errors.add(String.format("File %1$s does not exist", loc));
            return;
        }
    }
    String namespace = imp.getNamespace();
    String fileName = path.getFileName().toString();
    String id = fileName;
    id = FilenameUtils.removeExtension(id);
    // Convention: id of import is filename without extension
    GenericImportId rid;
    if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
        rid = new XSDImportId(namespace, id, false);
    } else {
        rid = new GenericImportId(namespace, id, false, type);
    }
    boolean importDataExistsInRepo = RepositoryFactory.getRepository().exists(rid);
    if (!importDataExistsInRepo) {
        // We have to
        // a) create a .definitions file
        // b) put the file itself in the repo
        // Create the definitions file
        TDefinitions defs = BackendUtils.createWrapperDefinitions(rid);
        defs.getImport().add(imp);
        // QUICK HACK: We change the imp object's location here and below again
        // This is "OK" as "storeDefinitions" serializes the current state and not the future state of the imp object
        // change the location to point to the file in the folder of the .definitions file
        imp.setLocation(fileName);
        // put the definitions file to the repository
        CsarImporter.storeDefinitions(rid, defs);
    }
    // put the file itself to the repo
    // ref is required to generate fileRef
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
    RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), fileName);
    // location is relative to Definitions/
    // even if the import already exists, we have to adapt the path
    // URIs are encoded
    String newLoc = "../" + Util.getUrlPath(fileRef);
    imp.setLocation(newLoc);
    if (!importDataExistsInRepo || overwrite) {
        // finally write the file to the storage
        try (InputStream is = Files.newInputStream(path);
            BufferedInputStream bis = new BufferedInputStream(is)) {
            MediaType mediaType;
            if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                mediaType = MediaTypes.MEDIATYPE_XSD;
            } else {
                mediaType = BackendUtils.getMimeType(bis, path.getFileName().toString());
            }
            RepositoryFactory.getRepository().putContentToFile(fileRef, bis, mediaType);
        } catch (IllegalArgumentException | IOException e) {
            throw new IllegalStateException(e);
        }
        // we have to update the cache in case of a new XSD to speedup usage of winery
        if (rid instanceof XSDImportId) {
            // We do the initialization asynchronously
            // We do not check whether the XSD has already been checked
            // We cannot just checck whether an XSD already has been handled since the XSD could change over time
            // Synchronization at org.eclipse.winery.repository.resources.imports.xsdimports.XSDImportResource.getAllDefinedLocalNames(short) also isn't feasible as the backend doesn't support locks
            CsarImporter.xsdParsingService.submit(() -> {
                CsarImporter.LOGGER.debug("Updating XSD import cache data");
                // We call the queries without storing the result:
                // We use the SIDEEFFECT that a cache is created
                final XsdImportManager xsdImportManager = RepositoryFactory.getRepository().getXsdImportManager();
                xsdImportManager.getAllDeclaredElementsLocalNames();
                xsdImportManager.getAllDefinedTypesLocalNames();
                CsarImporter.LOGGER.debug("Updated XSD import cache data");
            });
        }
    }
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager)

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