]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message/src/org/simantics/message/util/ResourceSerializerUtil.java
NatTable based Graph Explorer styling fixes
[simantics/platform.git] / bundles / org.simantics.message / src / org / simantics / message / util / ResourceSerializerUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.util;
13
14 import java.net.URI;
15 import java.net.URISyntaxException;
16
17 import org.simantics.db.Resource;
18 import org.simantics.db.ResourceSerializer;
19 import org.simantics.db.Session;
20 import org.simantics.db.exception.InvalidResourceReferenceException;
21 import org.simantics.db.service.SerialisationSupport;
22 import org.simantics.message.ReferenceSerializationException;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class ResourceSerializerUtil {
28
29     public static Resource deserialize(Session s, URI data) throws ReferenceSerializationException {
30         SerialisationSupport support = s.getService(SerialisationSupport.class);
31         ResourceSerializer rs = support.getResourceSerializer();
32         try {
33             return rs.getResource(data.getSchemeSpecificPart());
34         } catch (InvalidResourceReferenceException e) {
35             throw new ReferenceSerializationException(e);
36         }
37     }
38
39     public static URI serialize(Session s, Resource r) throws ReferenceSerializationException {
40         SerialisationSupport support = s.getService(SerialisationSupport.class);
41         ResourceSerializer rs = support.getResourceSerializer();
42         try {
43             return new URI("resource", rs.createRandomAccessId(r), null);
44         } catch (InvalidResourceReferenceException e) {
45             throw new ReferenceSerializationException(e);
46         } catch (URISyntaxException e) {
47             throw new ReferenceSerializationException(e);
48         }
49     }
50
51 }