Search in sources :

Example 6 with XSDImportId

use of org.eclipse.winery.model.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 = owner.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.model.ids.definitions.imports.XSDImportId) Namespace(org.eclipse.winery.model.ids.Namespace)

Example 7 with XSDImportId

use of org.eclipse.winery.model.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
 * @param options  the set of options applicable while importing a CSAR
 */
private void importOtherImport(Path rootPath, TImport imp, final List<String> errors, String type, CsarImportOptions options) {
    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 = EncodingUtil.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 = targetRepository.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, targetRepository);
        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(targetRepository, 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 || options.isOverwrite()) {
        // 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());
            }
            targetRepository.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 check 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 = targetRepository.getXsdImportManager();
                xsdImportManager.getAllDeclaredElementsLocalNames();
                xsdImportManager.getAllDefinedTypesLocalNames();
                CsarImporter.LOGGER.debug("Updated XSD import cache data");
            });
        }
    }
}
Also used : Path(java.nio.file.Path) XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) AccountabilityException(org.eclipse.winery.accountability.exceptions.AccountabilityException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) GenericImportId(org.eclipse.winery.model.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager)

Example 8 with XSDImportId

use of org.eclipse.winery.model.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.model.ids.definitions.imports.XSDImportId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) HashMap(java.util.HashMap)

Aggregations

XSDImportId (org.eclipse.winery.model.ids.definitions.imports.XSDImportId)8 IOException (java.io.IOException)4 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)4 TImport (org.eclipse.winery.model.tosca.TImport)4 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)4 GenericImportId (org.eclipse.winery.model.ids.definitions.imports.GenericImportId)3 BufferedInputStream (java.io.BufferedInputStream)2 MediaType (org.apache.tika.mime.MediaType)2 Namespace (org.eclipse.winery.model.ids.Namespace)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 ZipInputStream (java.util.zip.ZipInputStream)1 JAXBException (javax.xml.bind.JAXBException)1 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)1