]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/direct/ResourceNameUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / direct / ResourceNameUtils.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.layer0.utils.direct;
13
14 import org.simantics.databoard.util.URIStringUtils;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.ServiceException;
19 import org.simantics.db.exception.ValidationException;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.layer0.utils.PropertyReference;
22
23 public final class ResourceNameUtils {
24
25     public static final char   NAME_PATH_DELIMITER        = '/';
26     public static final String NAME_PATH_DELIMITER_STRING = "" + NAME_PATH_DELIMITER;
27
28     public static String getName(ReadGraph g, Resource r) throws ValidationException, ServiceException {
29         Layer0 b = Layer0.getInstance(g);
30         String name = (String) g.getPossibleRelatedValue(r, b.HasName);
31         if (name == null)
32             return "#" + r.getResourceId();
33         else
34             return URIStringUtils.escape(name);
35     }
36
37     public static String getName(ReadGraph g, PropertyReference ref) throws DatabaseException {
38         return getName(g, ref.resources);
39     }
40
41     public static String getName(ReadGraph g, Resource[] ref) throws DatabaseException {
42         StringBuilder b = new StringBuilder();
43         boolean first = true;
44         for (Resource r : ref) {
45             if (first)
46                 first = false;
47             else
48                 b.append(NAME_PATH_DELIMITER);
49             b.append(getName(g, r));
50         }
51         return b.toString();
52     }
53
54 }