Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.common / src / main / java / org / eclipse / winery / common / ids / IdUtil.java
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
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.common.ids;
13
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;
17
18 /**
19  * Helper methods for Winery's Id system
20  */
21 public class IdUtil {
22         
23         /**
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.
28          * 
29          * @param id the id refering to an element, where the namespace has to be
30          *            checked for
31          * @return the namespace of the element denoted by id
32          */
33         public static Namespace getNamespace(GenericId id) {
34                 if (id instanceof TOSCAComponentId) {
35                         return ((TOSCAComponentId) id).getNamespace();
36                 } else {
37                         return IdUtil.getNamespace(id.getParent());
38                 }
39         }
40         
41         /**
42          * Executes the real conversion to a path fragment
43          * 
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
47          * @return
48          */
49         private static String getPathFragment(final GenericId id, final boolean doubleEncode) {
50                 String toInsert;
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();
56                         if (doubleEncode) {
57                                 toInsert = Util.URLencode(toInsert);
58                         }
59                         res = res + toInsert + "/";
60                         toInsert = tId.getXmlId().getEncoded();
61                         if (doubleEncode) {
62                                 toInsert = Util.URLencode(toInsert);
63                         }
64                         res = res + toInsert + "/";
65                         return res;
66                 } else if (id instanceof TOSCAElementId) {
67                         toInsert = id.getXmlId().getEncoded();
68                         if (doubleEncode) {
69                                 toInsert = Util.URLencode(toInsert);
70                         }
71                         return IdUtil.getPathFragment(id.getParent()) + toInsert + "/";
72                 } else {
73                         throw new IllegalStateException("Unknown subclass of GenericId " + id.getClass());
74                 }
75         }
76         
77         /**
78          * Returns the fragment of the path belonging to the id
79          * 
80          * For instance, an Id of type ServiceTemplateId has
81          * <code>servicetemplates/{encoded ns}/{encoded name}/</code>
82          * 
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
85          *         inside a URL
86          */
87         public static String getPathFragment(GenericId id) {
88                 return IdUtil.getPathFragment(id, false);
89         }
90         
91         /**
92          * Returns the fragment of the URL path belonging to the id
93          * 
94          * For instance, an Id of type ServiceTemplateId has
95          * <code>servicetemplates/{double encoded ns}/{double encoded name}/</encode>
96          * 
97          * @param id the element to return the path fragment for
98          * @return the path fragment to be used inside an URL
99          */
100         public static String getURLPathFragment(GenericId id) {
101                 return IdUtil.getPathFragment(id, true);
102         }
103         
104 }