use of org.eclipse.emf.ecore.xmi.XMLResource in project statecharts by Yakindu.
the class ShadowModelValidationJob method cloneResource.
protected void cloneResource(final IProgressMonitor monitor, final Resource shadowResource) throws ExecutionException {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(TransactionUtil.getEditingDomain(resource), "", null) {
@Override
protected CommandResult doExecuteWithResult(final IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
try {
XMISaveImpl saver = new XMISaveImpl(new XMIHelperImpl((XMLResource) resource));
saver.save((XMLResource) resource, bout, Collections.emptyMap());
bout.flush();
} catch (Throwable t) {
return CommandResult.newErrorCommandResult(t.getMessage());
}
return CommandResult.newOKCommandResult();
}
};
cmd.execute(monitor, null);
try {
shadowResource.load(new ByteArrayInputStream(bout.toByteArray()), Collections.emptyMap());
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.eclipse.emf.ecore.xmi.XMLResource in project jbpm by kiegroup.
the class BPMN2EmfExtTest method testOnEntryScriptElement.
public void testOnEntryScriptElement() throws Exception {
// write
XMLResource inResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
inResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
inResource.setEncoding("UTF-8");
DocumentRoot documentRoot = DroolsFactory.eINSTANCE.createDocumentRoot();
OnEntryScriptType root = DroolsFactory.eINSTANCE.createOnEntryScriptType();
root.setScript("script");
root.setScriptFormat("format");
documentRoot.setOnEntryScript(root);
inResource.getContents().add(documentRoot);
StringWriter stringWriter = new StringWriter();
inResource.save(stringWriter, null);
assertNotNull(stringWriter.getBuffer().toString());
if (stringWriter.getBuffer().toString().length() < 1) {
fail("generated xml is empty");
}
// read
XMLResource outResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
outResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
outResource.setEncoding("UTF-8");
Map<String, Object> options = new HashMap<String, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
InputStream is = new ByteArrayInputStream(stringWriter.getBuffer().toString().getBytes("UTF-8"));
outResource.load(is, options);
DocumentRoot outRoot = (DocumentRoot) outResource.getContents().get(0);
assertNotNull(outRoot.getOnEntryScript());
OnEntryScriptType scriptType = outRoot.getOnEntryScript();
assertEquals("script", scriptType.getScript());
assertEquals("format", scriptType.getScriptFormat());
}
use of org.eclipse.emf.ecore.xmi.XMLResource in project jbpm by kiegroup.
the class BPMN2EmfExtTest method testImportElement.
public void testImportElement() throws Exception {
// write
XMLResource inResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
inResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
inResource.setEncoding("UTF-8");
DocumentRoot documentRoot = DroolsFactory.eINSTANCE.createDocumentRoot();
ImportType root = DroolsFactory.eINSTANCE.createImportType();
root.setName("import");
documentRoot.setImport(root);
inResource.getContents().add(documentRoot);
StringWriter stringWriter = new StringWriter();
inResource.save(stringWriter, null);
assertNotNull(stringWriter.getBuffer().toString());
if (stringWriter.getBuffer().toString().length() < 1) {
fail("generated xml is empty");
}
// read
XMLResource outResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
outResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
outResource.setEncoding("UTF-8");
Map<String, Object> options = new HashMap<String, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
InputStream is = new ByteArrayInputStream(stringWriter.getBuffer().toString().getBytes("UTF-8"));
outResource.load(is, options);
DocumentRoot outRoot = (DocumentRoot) outResource.getContents().get(0);
assertNotNull(outRoot.getImport());
ImportType importType = outRoot.getImport();
assertEquals("import", importType.getName());
}
use of org.eclipse.emf.ecore.xmi.XMLResource in project jbpm by kiegroup.
the class BPMN2EmfExtTest method testMetadataElement.
@SuppressWarnings("unchecked")
public void testMetadataElement() throws Exception {
// write
XMLResource inResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
inResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
inResource.setEncoding("UTF-8");
DocumentRoot documentRoot = DroolsFactory.eINSTANCE.createDocumentRoot();
MetaDataType metadataType = DroolsFactory.eINSTANCE.createMetaDataType();
metadataType.setName("testvalue");
metadataType.setMetaValue("testentry");
documentRoot.setMetaData(metadataType);
inResource.getContents().add(documentRoot);
StringWriter stringWriter = new StringWriter();
inResource.save(stringWriter, null);
assertNotNull(stringWriter.getBuffer().toString());
if (stringWriter.getBuffer().toString().length() < 1) {
fail("generated xml is empty");
}
// read
XMLResource outResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
outResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
outResource.setEncoding("UTF-8");
Map<String, Object> options = new HashMap<String, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
InputStream is = new ByteArrayInputStream(stringWriter.getBuffer().toString().getBytes("UTF-8"));
outResource.load(is, options);
DocumentRoot outRoot = (DocumentRoot) outResource.getContents().get(0);
assertNotNull(outRoot.getMetaData());
MetaDataType outMetadataType = outRoot.getMetaData();
assertEquals(outMetadataType.getName(), "testvalue");
assertEquals(outMetadataType.getMetaValue(), "testentry");
}
use of org.eclipse.emf.ecore.xmi.XMLResource in project jbpm by kiegroup.
the class BPMN2EmfExtTest method testGlobalElement.
public void testGlobalElement() throws Exception {
// write
XMLResource inResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
inResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
inResource.setEncoding("UTF-8");
DocumentRoot documentRoot = DroolsFactory.eINSTANCE.createDocumentRoot();
GlobalType root = DroolsFactory.eINSTANCE.createGlobalType();
root.setIdentifier("identifier");
root.setType("type");
documentRoot.setGlobal(root);
inResource.getContents().add(documentRoot);
StringWriter stringWriter = new StringWriter();
inResource.save(stringWriter, null);
assertNotNull(stringWriter.getBuffer().toString());
if (stringWriter.getBuffer().toString().length() < 1) {
fail("generated xml is empty");
}
// read
XMLResource outResource = (XMLResource) resourceSet.createResource(URI.createURI("inputStream://dummyUriWithValidSuffix.xml"));
outResource.getDefaultLoadOptions().put(XMLResource.OPTION_ENCODING, "UTF-8");
outResource.setEncoding("UTF-8");
Map<String, Object> options = new HashMap<String, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
InputStream is = new ByteArrayInputStream(stringWriter.getBuffer().toString().getBytes("UTF-8"));
outResource.load(is, options);
DocumentRoot outRoot = (DocumentRoot) outResource.getContents().get(0);
assertNotNull(outRoot.getGlobal());
GlobalType globalType = outRoot.getGlobal();
assertEquals("identifier", globalType.getIdentifier());
assertEquals("type", globalType.getType());
}
Aggregations