Search in sources :

Example 6 with IModelParser

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

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

the class ModelSequencerOld method execute.

@Override
public boolean execute(Property inputProperty, Node outputNode, Context context) throws Exception {
    Binary binaryValue = inputProperty.getBinary();
    CheckArg.isNotNull(binaryValue, "binary");
    ModelInfo modelResource = null;
    try {
        IModelParser parser = ModelParserFactory.instance().getParser(outputNode.getPath());
        modelResource = parser.parse(binaryValue.getStream());
        outputNode.setProperty("vorto:description", modelResource.getDescription() != null ? modelResource.getDescription() : "");
        outputNode.setProperty("vorto:type", modelResource.getType().name());
        outputNode.setProperty("vorto:displayname", modelResource.getDisplayName());
        outputNode.setProperty("vorto:version", modelResource.getId().getVersion());
        outputNode.setProperty("vorto:namespace", modelResource.getId().getNamespace());
        outputNode.setProperty("vorto:name", modelResource.getId().getName());
        if (outputNode.hasProperty("vorto:references")) {
            // first remove any previous references of the node.
            outputNode.getProperty("vorto:references").remove();
            outputNode.getSession().save();
        }
        ModelReferencesHelper referencesHelper = new ModelReferencesHelper(modelResource.getReferences());
        if (referencesHelper.hasReferences()) {
            List<Value> references = new ArrayList<Value>();
            for (ModelId modelId : referencesHelper.getReferences()) {
                ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
                Node referencedFolder = outputNode.getSession().getNode(modelIdHelper.getFullPath());
                Node reference = referencedFolder.getNodes().nextNode();
                references.add(context.valueFactory().createValue(reference));
            }
            outputNode.setProperty("vorto:references", references.toArray(new Value[references.size()]));
        }
    } catch (Throwable couldNotParse) {
        outputNode.setProperty("vorto:description", "Erronous Model !!!!");
        outputNode.setProperty("vorto:type", ModelType.create(outputNode.getPath()).name());
        outputNode.setProperty("vorto:displayname", "");
        outputNode.setProperty("vorto:version", "1.0.0");
        outputNode.setProperty("vorto:namespace", "erronous");
        outputNode.setProperty("vorto:name", outputNode.getName());
    }
    return true;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ModelReferencesHelper(org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper) Node(javax.jcr.Node) Value(javax.jcr.Value) ArrayList(java.util.ArrayList) Binary(javax.jcr.Binary) ModelId(org.eclipse.vorto.model.ModelId)

Example 8 with IModelParser

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

Example 9 with IModelParser

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

the class ModelParserTest method testModelWithInvalidReference.

@Test(expected = ValidationException.class)
public void testModelWithInvalidReference() throws IOException {
    IModelParser parser = modelParserFactory.getParser("InfoModelWithoutNamespace.infomodel");
    parser.enableValidation();
    parser.parse(new ClassPathResource("sample_models/InfoModelWithoutNamespace.infomodel").getInputStream());
}
Also used : IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

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