Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1acec9e6ebcd427c90dfca7b31167e00a18df250 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * Copyright (c) 2004 - 2009 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.common.model;

import org.eclipse.net4j.util.WrappedException;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author Eike Stepper
 */
public class TestTransfer
{
  public static void main(String[] args) throws IOException
  {
    // Create packageA
    //
    EAttribute attributeA = EcoreFactory.eINSTANCE.createEAttribute();
    attributeA.setName("attributeA");
    attributeA.setEType(EcorePackage.eINSTANCE.getEString());

    EClass classA = EcoreFactory.eINSTANCE.createEClass();
    classA.setName("classA");
    classA.getEStructuralFeatures().add(attributeA);

    EPackage packageA = EcoreFactory.eINSTANCE.createEPackage();
    packageA.setName("packageA");
    packageA.setNsPrefix("packageA");
    packageA.setNsURI("http://packageA");
    packageA.getEClassifiers().add(classA);

    // Create packageB
    //
    EAttribute attributeB = EcoreFactory.eINSTANCE.createEAttribute();
    attributeB.setName("attributeB");
    attributeB.setEType(EcorePackage.eINSTANCE.getEString());

    EClass classB = EcoreFactory.eINSTANCE.createEClass();
    classB.setName("classA");
    classB.getEStructuralFeatures().add(attributeB);
    classB.getESuperTypes().add(classA);

    EPackage packageB = EcoreFactory.eINSTANCE.createEPackage();
    packageB.setName("packageB");
    packageB.setNsPrefix("packageB");
    packageB.setNsURI("http://packageB");
    packageB.getEClassifiers().add(classB);

    // Simulate generated global packages
    //
    EPackage.Registry.INSTANCE.put(packageA.getNsURI(), packageA);
    EPackage.Registry.INSTANCE.put(packageB.getNsURI(), packageB);

    // Serialize both packages
    //
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serialize(packageA, baos);
    serialize(packageB, baos);
  }

  private static void serialize(EPackage ePackage, OutputStream out) throws IOException
  {
    Resource resource = ePackage.eResource();
    if (resource == null)
    {
      resource = createPackageResource(ePackage.getNsURI());
      resource.getContents().add(ePackage);
    }

    resource.save(out, null);
  }

  private static Resource createPackageResource(String uri)
  {
    Resource.Factory resourceFactory = new EcoreResourceFactoryImpl();

    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", resourceFactory);
    resourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put("*", resourceFactory);

    return resourceSet.createResource(URI.createURI(uri));
  }

  public static class UTIL
  {
    public static byte[] getPackageBytes(EPackage ePackage, boolean zipped)
    {
      try
      {
        Resource resource = ePackage.eResource();
        if (resource == null)
        {
          resource = createPackageResource(ePackage.getNsURI());
          resource.getContents().add(ePackage);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resource.save(baos, null);
        return baos.toByteArray();
      }
      catch (Exception ex)
      {
        throw WrappedException.wrap(ex);
      }
    }

    public static EPackage createPackage(String uri, byte[] bytes, boolean zipped)
    {
      try
      {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        Resource resource = createPackageResource(uri);
        resource.load(bais, null);

        EList<EObject> contents = resource.getContents();
        return (EPackage)contents.get(0);
      }
      catch (Exception ex)
      {
        throw WrappedException.wrap(ex);
      }
    }
  }
}

Back to the top