Search in sources :

Example 1 with LocalModelWorkspace

use of org.eclipse.vorto.repository.core.impl.parser.LocalModelWorkspace 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 LocalModelWorkspace

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

the class BulkUploadHelper method parseZipFile.

private ZipParseResult parseZipFile(byte[] content) {
    if (content == null) {
        throw new FatalModelRepositoryException("Contents of zip file are invalid", null);
    }
    ZipParseResult parsingResult = new ZipParseResult();
    parsingResult.invalidModels = new HashSet<>();
    parsingResult.validModels = new HashSet<>();
    Collection<FileContent> fileContents = getFileContentsFromZip(content);
    LocalModelWorkspace workspace = new LocalModelWorkspace(modelRepoFactory, fileContents);
    fileContents.forEach(fileContent -> {
        try {
            IModelParser parser = ModelParserFactory.instance().getParser(fileContent.getFileName());
            parser.setWorkspace(workspace);
            parser.enableValidation();
            parsingResult.validModels.add(parser.parse(new ByteArrayInputStream(fileContent.getContent())));
        } catch (ValidationException grammarProblem) {
            parsingResult.invalidModels.add(ValidationReport.invalid(trytoCreateModelFromCorruptFile(fileContent.getFileName()), grammarProblem));
        } catch (Exception e) {
            parsingResult.invalidModels.add(ValidationReport.invalid(trytoCreateModelFromCorruptFile(fileContent.getFileName()), "File cannot be processed to a Vorto model."));
        }
    });
    return parsingResult;
}
Also used : LocalModelWorkspace(org.eclipse.vorto.repository.core.impl.parser.LocalModelWorkspace) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) BulkUploadException(org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)

Example 3 with LocalModelWorkspace

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

the class ModelParserTest method testModelParsingWithDependencyPassedIn.

@Test
public void testModelParsingWithDependencyPassedIn() {
    try {
        FileContent file1 = new FileContent("Color.type", IOUtils.toByteArray(new ClassPathResource("sample_models/Color.type").getInputStream()));
        FileContent file2 = new FileContent("Colorlight.fbmodel", IOUtils.toByteArray(new ClassPathResource("sample_models/Colorlight.fbmodel").getInputStream()));
        IModelParser parser = modelParserFactory.getParser("ColorLightIM.infomodel");
        parser.setWorkspace(new LocalModelWorkspace(repositoryFactory, Arrays.asList(file1, file2)));
        parser.enableValidation();
        ModelInfo modelInfo = parser.parse(new ClassPathResource("sample_models/ColorLightIM.infomodel").getInputStream());
        assertTrue(modelInfo != null);
        assertEquals("com.mycompany", modelInfo.getId().getNamespace());
        assertEquals("ColorLightIM", modelInfo.getId().getName());
        assertEquals("1.0.0", modelInfo.getId().getVersion());
    } catch (ValidationException e) {
        fail("There should be no exception");
    } catch (IOException e) {
        fail("Not able to load test file");
    }
}
Also used : LocalModelWorkspace(org.eclipse.vorto.repository.core.impl.parser.LocalModelWorkspace) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

IModelParser (org.eclipse.vorto.repository.core.impl.parser.IModelParser)3 LocalModelWorkspace (org.eclipse.vorto.repository.core.impl.parser.LocalModelWorkspace)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 ModelResource (org.eclipse.vorto.repository.core.ModelResource)1 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)1 BulkUploadException (org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)1 Test (org.junit.Test)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1