Search in sources :

Example 6 with XMLResource

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();
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) XMIHelperImpl(org.eclipse.emf.ecore.xmi.impl.XMIHelperImpl) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) XMISaveImpl(org.eclipse.emf.ecore.xmi.impl.XMISaveImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource)

Example 7 with XMLResource

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());
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentRoot(org.jboss.drools.DocumentRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OnEntryScriptType(org.jboss.drools.OnEntryScriptType) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource)

Example 8 with XMLResource

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());
}
Also used : ImportType(org.jboss.drools.ImportType) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentRoot(org.jboss.drools.DocumentRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource)

Example 9 with XMLResource

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");
}
Also used : StringWriter(java.io.StringWriter) MetaDataType(org.jboss.drools.MetaDataType) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentRoot(org.jboss.drools.DocumentRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource)

Example 10 with XMLResource

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());
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentRoot(org.jboss.drools.DocumentRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) GlobalType(org.jboss.drools.GlobalType)

Aggregations

XMLResource (org.eclipse.emf.ecore.xmi.XMLResource)38 HashMap (java.util.HashMap)12 ByteArrayInputStream (java.io.ByteArrayInputStream)8 StringWriter (java.io.StringWriter)8 InputStream (java.io.InputStream)6 EObject (org.eclipse.emf.ecore.EObject)5 DocumentRoot (org.jboss.drools.DocumentRoot)5 DocumentRoot (org.eclipse.bpmn2.DocumentRoot)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)3 IOException (java.io.IOException)2 Bpmn2ResourceFactoryImpl (org.eclipse.bpmn2.util.Bpmn2ResourceFactoryImpl)2 URI (org.eclipse.emf.common.util.URI)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 DroolsResourceFactoryImpl (org.jboss.drools.util.DroolsResourceFactoryImpl)2 BPSimDataType (bpsim.BPSimDataType)1 DocumentRoot (bpsim.DocumentRoot)1 ElementParameters (bpsim.ElementParameters)1 Parameter (bpsim.Parameter)1 Scenario (bpsim.Scenario)1