1 /*******************************************************************************
2 * Copyright (c) 2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.common.ids;
14 import org.eclipse.winery.common.Util;
15 import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
16 import org.eclipse.winery.common.ids.elements.TOSCAElementId;
19 * Helper methods for Winery's Id system
24 * Returns the namespace where the given Id is nested in. As the id is not a
25 * TOSCAComponentId, it cannot be directly asked for its parent. Merely, the
26 * parent has to be asked for its namespace. The parent, in turn, if it is
27 * no TOSCAComponentId has to ask his parent.
29 * @param id the id refering to an element, where the namespace has to be
31 * @return the namespace of the element denoted by id
33 public static Namespace getNamespace(GenericId id) {
34 if (id instanceof TOSCAComponentId) {
35 return ((TOSCAComponentId) id).getNamespace();
37 return IdUtil.getNamespace(id.getParent());
42 * Executes the real conversion to a path fragment
44 * @param id the id to transform to a path
45 * @param doubleEncode true if each sub fragment should be double encoded,
46 * false if it should be encoded only once
49 private static String getPathFragment(final GenericId id, final boolean doubleEncode) {
51 if (id instanceof TOSCAComponentId) {
52 // @return "[ComponentName]s/{namespace}/{id}/"
53 TOSCAComponentId tId = (TOSCAComponentId) id;
54 String res = Util.getRootPathFragment(tId.getClass());
55 toInsert = tId.getNamespace().getEncoded();
57 toInsert = Util.URLencode(toInsert);
59 res = res + toInsert + "/";
60 toInsert = tId.getXmlId().getEncoded();
62 toInsert = Util.URLencode(toInsert);
64 res = res + toInsert + "/";
66 } else if (id instanceof TOSCAElementId) {
67 toInsert = id.getXmlId().getEncoded();
69 toInsert = Util.URLencode(toInsert);
71 return IdUtil.getPathFragment(id.getParent()) + toInsert + "/";
73 throw new IllegalStateException("Unknown subclass of GenericId " + id.getClass());
78 * Returns the fragment of the path belonging to the id
80 * For instance, an Id of type ServiceTemplateId has
81 * <code>servicetemplates/{encoded ns}/{encoded name}/</code>
83 * @param id the element to return the path fragment for
84 * @return the path fragment. This is <em>not</em> intended to be used
87 public static String getPathFragment(GenericId id) {
88 return IdUtil.getPathFragment(id, false);
92 * Returns the fragment of the URL path belonging to the id
94 * For instance, an Id of type ServiceTemplateId has
95 * <code>servicetemplates/{double encoded ns}/{double encoded name}/</encode>
97 * @param id the element to return the path fragment for
98 * @return the path fragment to be used inside an URL
100 public static String getURLPathFragment(GenericId id) {
101 return IdUtil.getPathFragment(id, true);