Search in sources :

Example 1 with IModelParser

use of org.eclipse.vorto.repository.core.impl.parser.IModelParser in project vorto by eclipse.

the class AbstractModelImporter method parseDSL.

protected ModelResource parseDSL(String fileName, byte[] content, Collection<FileContent> fileReferences) {
    IModelParser modelParser = modelParserFactory.getParser(fileName);
    modelParser.setWorkspace(new LocalModelWorkspace(modelRepoFactory, fileReferences));
    modelParser.enableValidation();
    return (ModelResource) modelParser.parse(new ByteArrayInputStream(content));
}
Also used : LocalModelWorkspace(org.eclipse.vorto.repository.core.impl.parser.LocalModelWorkspace) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 2 with IModelParser

use of org.eclipse.vorto.repository.core.impl.parser.IModelParser in project vorto by eclipse.

the class ModelSequencer method execute.

@Override
public boolean execute(Property inputProperty, Node fileNode, Context context) throws Exception {
    final Node folderNode = fileNode.getParent();
    Binary binaryValue = inputProperty.getBinary();
    CheckArg.isNotNull(binaryValue, "binary");
    IModelParser parser = ModelParserFactory.instance().getParser(fileNode.getPath());
    ModelInfo modelResource = parser.parse(binaryValue.getStream());
    fileNode.setProperty("vorto:description", modelResource.getDescription() != null ? modelResource.getDescription() : "");
    fileNode.setProperty("vorto:type", modelResource.getType().name());
    fileNode.setProperty("vorto:displayname", modelResource.getDisplayName());
    fileNode.setProperty("vorto:version", modelResource.getId().getVersion());
    fileNode.setProperty("vorto:namespace", modelResource.getId().getNamespace());
    fileNode.setProperty("vorto:name", modelResource.getId().getName());
    if (modelResource.getType() == ModelType.Mapping) {
        MappingModel mappingModel = (MappingModel) ((ModelResource) modelResource).getModel();
        fileNode.setProperty("vorto:targetplatform", mappingModel.getTargetPlatform());
    }
    folderNode.addMixin("mix:referenceable");
    folderNode.addMixin("vorto:meta");
    folderNode.setProperty("vorto:namespace", modelResource.getId().getNamespace());
    folderNode.setProperty("vorto:type", modelResource.getType().name());
    folderNode.setProperty("vorto:name", modelResource.getId().getName());
    if (folderNode.hasProperty("vorto:references")) {
        // first remove any previous references of the node.
        folderNode.getProperty("vorto:references").remove();
        folderNode.getSession().save();
    }
    Optional<Value[]> referencesAsValues = getReferencesAsValues(folderNode, modelResource.getReferences());
    if (referencesAsValues.isPresent()) {
        folderNode.setProperty("vorto:references", referencesAsValues.get());
    }
    return true;
}
Also used : IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) Node(javax.jcr.Node) Binary(javax.jcr.Binary) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 3 with IModelParser

use of org.eclipse.vorto.repository.core.impl.parser.IModelParser in project vorto by eclipse.

the class ModelRepository method getEMFResource.

@Override
public ModelResource getEMFResource(ModelId modelId) {
    return doInSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            if (!folderNode.getNodes(FILE_NODES).hasNext()) {
                LOGGER.warn("Folder Node :" + folderNode + " does not have any files as children. Cannot load EMF Model.");
                return null;
            }
            Node fileNode = (Node) folderNode.getNodes(FILE_NODES).next();
            Node fileItem = (Node) fileNode.getPrimaryItem();
            InputStream is = fileItem.getProperty(JCR_DATA).getBinary().getStream();
            IModelParser parser = modelParserFactory.getParser(fileNode.getName());
            return (ModelResource) parser.parse(is);
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    });
}
Also used : IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 4 with IModelParser

use of org.eclipse.vorto.repository.core.impl.parser.IModelParser in project vorto by eclipse.

the class ModelRepository method save.

@Override
public ModelInfo save(ModelId modelId, byte[] content, String fileName, IUserContext userContext, boolean validate) {
    Objects.requireNonNull(content);
    Objects.requireNonNull(modelId);
    IModelParser parser = modelParserFactory.getParser("model" + ModelType.fromFileName(fileName).getExtension());
    if (validate) {
        parser.enableValidation();
    }
    ModelResource modelInfo = (ModelResource) parser.parse(new ByteArrayInputStream(content));
    save(modelInfo, userContext);
    return modelInfo;
}
Also used : IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 5 with IModelParser

use of org.eclipse.vorto.repository.core.impl.parser.IModelParser in project vorto by eclipse.

the class ModelRepository method getModelContent.

@Override
public ModelFileContent getModelContent(ModelId modelId, boolean validate) {
    return doInSession(session -> {
        try {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node folderNode = session.getNode(modelIdHelper.getFullPath());
            Node fileNode = (Node) folderNode.getNodes(FILE_NODES).next();
            Node fileItem = (Node) fileNode.getPrimaryItem();
            InputStream is = fileItem.getProperty(JCR_DATA).getBinary().getStream();
            final String fileContent = IOUtils.toString(is);
            IModelParser parser = modelParserFactory.getParser(fileNode.getName());
            ModelResource resource = (ModelResource) parser.parse(IOUtils.toInputStream(fileContent));
            return new ModelFileContent(resource.getModel(), fileNode.getName(), fileContent.getBytes());
        } catch (IOException e) {
            throw new FatalModelRepositoryException("Something went wrong accessing the repository", e);
        }
    });
}
Also used : IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

IModelParser (org.eclipse.vorto.repository.core.impl.parser.IModelParser)9 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)3 LocalModelWorkspace (org.eclipse.vorto.repository.core.impl.parser.LocalModelWorkspace)3 ModelIdHelper (org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper)3 InputStream (java.io.InputStream)2 Binary (javax.jcr.Binary)2 Node (javax.jcr.Node)2 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 ArrayList (java.util.ArrayList)1 Value (javax.jcr.Value)1 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)1 ModelId (org.eclipse.vorto.model.ModelId)1 ModelResource (org.eclipse.vorto.repository.core.ModelResource)1 ModelReferencesHelper (org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper)1 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)1 BulkUploadException (org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)1 NotAuthorizedException (org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)1