@Override public Resource createResource(URI uri) { synchronized (lock) { return super.createResource(uri); } }
public String serializeToDslString(ZenModel zenModel) { // Xtext serializer needs both a resource with non-null URL and a resource set XtextResourceSet resourceSet = new RepreZenXtextResourceSet(); XtextResource resource = ((XtextResource) resourceSet.createResource(URI.createURI("dummy.rapid"))); resource.getContents().add(zenModel); // serialize return serializer.serialize(zenModel); }
private void exportXMI(String absuloteTargetFolderPath) { // change MyLanguage with your language name Injector injector = new MyLanguageStandaloneSetup() .createInjectorAndDoEMFRegistration(); XtextResourceSet resourceSet = injector .getInstance(XtextResourceSet.class); // .ext ist the extension of the model file String inputURI = "file:///" + absuloteTargetFolderPath + "/MyFile.ext"; String outputURI = "file:///" + absuloteTargetFolderPath + "/MyFile.xmi"; URI uri = URI.createURI(inputURI); Resource xtextResource = resourceSet.getResource(uri, true); EcoreUtil.resolveAll(xtextResource); Resource xmiResource = resourceSet .createResource(URI.createURI(outputURI)); xmiResource.getContents().add(xtextResource.getContents().get(0)); try { xmiResource.save(null); } catch (IOException e) { e.printStackTrace(); } }
public Model extract(final String modelName) { FunctionblockPackage.eINSTANCE.eClass(); InformationModelPackage.eINSTANCE.eClass(); ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(this.zipFile)); ZipEntry entry = null; FunctionblockStandaloneSetup.doSetup(); Injector injector = new InformationModelStandaloneSetup().createInjectorAndDoEMFRegistration(); XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE); Resource infoModelResource = null; try { while ((entry = zis.getNextEntry()) != null) { Resource resource = resourceSet.createResource(URI.createURI("fake:/" + entry.getName())); resource.load(new ByteArrayInputStream(copyStream(zis, entry)), resourceSet.getLoadOptions()); if (((Model) resource.getContents().get(0)).getName().equals(modelName)) { infoModelResource = resource; } } } catch (Exception ex) { ex.printStackTrace(); } EcoreUtil2.resolveAll(resourceSet); return (Model)infoModelResource.getContents().get(0); }
protected ZenModel getLibraryModel(org.eclipse.emf.common.util.URI modelURI) { new XtextDslStandaloneSetup().createInjectorAndDoEMFRegistration(); XtextResourceSet resourceSet = new XtextResourceSet(); resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE); org.eclipse.emf.ecore.resource.Resource resource = resourceSet.createResource(modelURI); try { InputStream stream = RestApiXtextPlugin.class.getResourceAsStream("/libraries/" + modelURI.lastSegment()); //$NON-NLS-1$ resource.load(stream, null); } catch (IOException e) { throw new RuntimeException(e); } return (ZenModel) resource.getContents().get(0); }
/** * same as {@link #resourceSet(Pair...)} but without actually loading the created resources. */ public ResourceSet unLoadedResourceSet(@SuppressWarnings("unchecked") Pair<String,? extends CharSequence> ...resources ) throws IOException { XtextResourceSet result = newResourceSetWithUTF8Encoding(); for (Pair<String, ? extends CharSequence> entry : resources) { URI uri = copyToWorkspace(getSourceFolderPath()+"/"+entry.getKey(), entry.getValue()); Resource resource = result.createResource(uri); if (resource == null) throw new IllegalStateException("Couldn't create resource for URI "+uri+". Resource.Factory not registered?"); } return result; }
private void load(IUriProvider uriProvider) { for (URI uri : uriProvider.allUris()) { try { Resource resource = resourceSet.getResource(uri, false); if (resource == null) { resource = resourceSet.createResource(uri); } resource.load(null); EcoreUtil.resolveAll(resource); } catch (IOException e) { throw new RuntimeException(e); } } EcoreUtil.resolveAll(resourceSet); }
while ((entry = zis.getNextEntry()) != null) { if (entry.getName().endsWith(".mapping")) { Resource mappingResource = resourceSet.createResource(URI.createURI("fake:/" + entry.getName())); mappingResource.load(new ByteArrayInputStream(copyStream(zis, entry)),resourceSet.getLoadOptions()); } else { Resource resource = resourceSet.createResource(URI.createURI("fake:/" + entry.getName())); resource.load(new ByteArrayInputStream(copyStream(zis, entry)), resourceSet.getLoadOptions());
protected ResourceSet createResourceSet(@Extension final AbstractHierarchyBuilderTest.HierarchyBuilderTestConfiguration configuration) { try { final XtextResourceSet resourceSet = this.resourceSetProvider.get(); for (final Pair<String, String> model : configuration.models) { { String _key = model.getKey(); URI _createURI = URI.createURI(_key); final Resource resource = resourceSet.createResource(_createURI); String _value = model.getValue(); LazyStringInputStream _lazyStringInputStream = new LazyStringInputStream(_value, "UTF-8"); resource.load(_lazyStringInputStream, null); this._validationTestHelper.assertNoIssues(resource); } } return resourceSet; } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
private XExpression parseScriptIntoXTextEObject(String scriptAsString) throws ScriptParsingException { XtextResourceSet resourceSet = getResourceSet(); Resource resource = resourceSet.createResource(computeUnusedUri(resourceSet)); // IS-A XtextResource try { resource.load(new StringInputStream(scriptAsString, StandardCharsets.UTF_8.name()), resourceSet.getLoadOptions()); } catch (IOException e) { throw new ScriptParsingException( "Unexpected IOException; from close() of a String-based ByteArrayInputStream, no real I/O; how is that possible???", scriptAsString, e); } List<Diagnostic> errors = resource.getErrors(); if (errors.size() != 0) { throw new ScriptParsingException("Failed to parse expression (due to managed SyntaxError/s)", scriptAsString).addDiagnosticErrors(errors); } EList<EObject> contents = resource.getContents(); if (!contents.isEmpty()) { Iterable<Issue> validationErrors = getValidationErrors(contents.get(0)); if (!validationErrors.iterator().hasNext()) { return (XExpression) contents.get(0); } else { throw new ScriptParsingException("Failed to parse expression (due to managed ValidationError/s)", scriptAsString).addValidationIssues(validationErrors); } } else { return null; } }
/** * Serializes an {@link com.modelsolv.reprezen.restapi.ZenModel} at the given path. * * @param path * Location of the RESTApi metamodel. * @param model * {@link com.modelsolv.reprezen.restapi.ZenModel} to serialize. */ @Override public void serialize(String path, ZenModel model) throws RestApiException { XtextResourceSet resourceSet = new XtextDslStandaloneSetup().createInjectorAndDoEMFRegistration() .getInstance(XtextResourceSet.class); org.eclipse.emf.ecore.resource.Resource resource = resourceSet.createResource(URI.createFileURI(path)); resource.getContents().add(model); try { resource.save(SaveOptions.newBuilder().noValidation().getOptions().toOptionsMap()); } catch (IOException e) { throw new RestApiException(String.format("Could not save the ZenModel at location '%s'", path), e); } }
/** * creates a fresh resource set with the given resources * * @param resources - pairs of file names and their contents * @return a ResourceSet, containing the given resources. * @throws IOException if the resource loading fails */ public ResourceSet resourceSet(@SuppressWarnings("unchecked") Pair<String,? extends CharSequence> ...resources ) throws IOException { XtextResourceSet result = newResourceSetWithUTF8Encoding(); FileProjectConfig projectConfig = new FileProjectConfig(new File(workspaceRoot,PROJECT_NAME), PROJECT_NAME); projectConfig.addSourceFolder("src"); ProjectConfigAdapter.install(result, projectConfig); for (Pair<String, ? extends CharSequence> entry : resources) { URI uri = copyToWorkspace(getSourceFolderPath()+"/"+entry.getKey(), entry.getValue()); Resource resource = result.createResource(uri); if (resource == null) throw new IllegalStateException("Couldn't create resource for URI "+uri+". Resource.Factory not registered?"); resource.load(result.getLoadOptions()); } return result; }