Skip to content

Package: XMIStringConverterHelper

XMIStringConverterHelper

nameinstructionbranchcomplexitylinemethod
deserializeObject(String)
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getSerializedEObject(EObject)
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.spreadsheet.core.converter;
15:
16: import java.io.IOException;
17: import java.io.StringWriter;
18:
19: import org.eclipse.emf.common.util.URI;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.resource.Resource;
22: import org.eclipse.emf.ecore.resource.ResourceSet;
23: import org.eclipse.emf.ecore.resource.URIConverter.ReadableInputStream;
24: import org.eclipse.emf.ecore.resource.URIConverter.WriteableOutputStream;
25: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
26: import org.eclipse.emf.ecore.util.EcoreUtil;
27: import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
28:
29: /**
30: * Helper class for transforming EObjects to a string and vice versa.
31: *
32: * @author jfaltermeier
33: *
34: */
35: public final class XMIStringConverterHelper {
36:
37:         private XMIStringConverterHelper() {
38:
39:         }
40:
41:         /**
42:          * Uses EMF serialization to transform the object to a string.
43:          *
44:          * @param eObject the object
45:          * @return the string
46:          * @throws IOException in case of an error during save
47:          */
48:         public static String getSerializedEObject(EObject eObject) throws IOException {
49:                 final ResourceSet rs = new ResourceSetImpl();
50:                 rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()); //$NON-NLS-1$
51:                 final Resource resource = rs.createResource(URI.createURI("VIRTAUAL_URI")); //$NON-NLS-1$
52:                 resource.getContents().add(EcoreUtil.copy(eObject));
53:
54:                 final StringWriter sw = new StringWriter();
55:                 final WriteableOutputStream os = new WriteableOutputStream(sw, "UTF-8"); //$NON-NLS-1$
56:
57:                 resource.save(os, null);
58:                 final String value = sw.getBuffer().toString();
59:                 return value;
60:         }
61:
62:         /**
63:          * Gets an EObject from an XMI string.
64:          *
65:          * @param object the XMI string
66:          * @return the object
67:          * @throws IOException in case the object could not be deserialized
68:          */
69:         public static EObject deserializeObject(String object) throws IOException {
70:                 final ResourceSet rs = new ResourceSetImpl();
71:                 rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()); //$NON-NLS-1$
72:                 final Resource resource = rs.createResource(URI.createURI("VIRTAUAL_URI")); //$NON-NLS-1$
73:
74:                 final ReadableInputStream is = new ReadableInputStream(object, "UTF-8"); //$NON-NLS-1$
75:                 resource.load(is, null);
76:                 return resource.getContents().get(0);
77:         }
78:
79: }