Skip to main content
summaryrefslogtreecommitdiffstats
blob: 255302d2ef88ea6b5490db0e7d2cba9e108701aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*******************************************************************************
 * Copyright (c) 2017 Thales Global Services S.A.S.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Abel Hegedus (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.persistence.emf

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
import java.nio.charset.StandardCharsets
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.diffmerge.patch.api.ModelPatch
import org.eclipse.emf.diffmerge.patch.api.ModelPatchException
import org.eclipse.emf.diffmerge.patch.persistence.emf.emodelpatch.EModelPatch
import org.eclipse.emf.diffmerge.patch.serializer.IModelPatchSerializer
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl

class EMFModelPatchSerializer implements IModelPatchSerializer {

  val persister = new EMFModelPatchPersister

  override load(File file) throws ModelPatchException {
    try{
      val rs = new ResourceSetImpl
      val r = rs.getResource(URI.createFileURI(file.path), true)
      val emodelpatch = r.contents.head
      if(emodelpatch instanceof EModelPatch){
        val modelpatch = persister.load(emodelpatch)
        return modelpatch
      }
    } catch(Exception e){
      throw new ModelPatchException("Exception occurred while loading model patch", e)
    }
  }

  override load(String source) throws ModelPatchException {
    var InputStream stream
    try{
      stream = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8))
      val reader = stream
      val rs = new ResourceSetImpl
      val r = rs.createResource(URI.createFileURI("file://dummy.emodelpatch")) => [
        load(reader, #{})
      ]
      val emodelpatch = r.contents.head
      if(emodelpatch instanceof EModelPatch){
        val modelpatch = persister.load(emodelpatch)
        return modelpatch
      }
    } catch(Exception e){
      throw new ModelPatchException("Exception occurred while loading model patch", e)
    } finally {
      stream.close
    }
  }

  override serialize(ModelPatch modelPatch, File file) throws ModelPatchException {
    try{
      val rs = new ResourceSetImpl
      val r = rs.createResource(URI.createFileURI(file.path))
      val emodelpatch = persister.persist(modelPatch)
      r.contents.add(emodelpatch)
      r.save(#{})
    } catch(Exception e){
      throw new ModelPatchException("Exception occurred while loading model patch", e)
    }
  }

  override serialize(ModelPatch modelPatch) throws ModelPatchException {
    var ByteArrayOutputStream stream
    var String result = null
    try{
      val rs = new ResourceSetImpl
      val r = rs.createResource(URI.createFileURI("file://dummy.emodelpatch"))
      val emodelpatch = persister.persist(modelPatch)
      r.contents.add(emodelpatch)
      stream = new ByteArrayOutputStream()
      r.save(stream, #{})
      result = stream.toString(StandardCharsets.UTF_8.name)
    } catch(Exception e){
      throw new ModelPatchException("Exception occurred while loading model patch", e)
    } finally {
      stream.close
    }
    return result
  }

  override getPreferredFileExtension() {
    return "emodelpatch"
  }

}

Back to the top