Change the header to SO
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / mybatis / URNMapping.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.core.mybatis;
22
23 /**
24  * A bean that represents a single URN mapping.
25  */
26 public class URNMapping {
27         private String name;
28         private String value;
29         private String rev;
30
31         /**
32          * Get the name.
33          * @return the name
34          */
35         public String getName() {
36                 return name;
37         }
38
39         /**
40          * Set the name.
41          * @param name the name
42          */
43         public void setName(String name) {
44                 this.name = name;
45         }
46
47         /**
48          * Get the value mapped to the name.
49          * @return the value mapped to the name
50          */
51         public String getValue() {
52                 return value;
53         }
54
55         /**
56          * Set the value mapped to the name.
57          * @param value the value mapped to the name
58          */
59         public void setValue(String value) {
60                 this.value = value;
61         }
62
63         /**
64          * Get the revision attribute (currently unused).
65          * @return the revision attribute
66          */
67         public String getRev() {
68                 return rev;
69         }
70
71         /**
72          * Set the revision attribute (currently unused).
73          * @param rev the revision attribute
74          */
75         public void setRev(String rev) {
76                 this.rev = rev;
77         }
78         
79         /**
80          * Converts a URN to "normal" form so it can used as a java or groovy
81          * variable identifier.  This is done in a way that makes the identifier
82          * as readable as possible, but note that it might result in a loss of
83          * uniqueness.
84          * <ol>
85          * <li> URN_ is prepended </li>
86          * <li> All characters that are not letters or digits are converted to
87          *      underscore characters </li>
88          * <li> Sequences of multiple underscores are collapsed to a single
89          *      underscore character </li>
90          * </ol>
91          * Examples:
92          * <p>
93          * aai:endpoint becomes URN_aai_endpoint <br/>
94          * ae:internal-reporting becomes URN_ae_internal_reporting <br/>
95          * 
96          * @param name the URN
97          * @return a normalized identifier
98          */
99         public static String createIdentifierFromURN(String urn) {
100                 StringBuilder builder = new StringBuilder();
101                 builder.append("URN_");
102                 char last = builder.charAt(builder.length() - 1);
103
104                 int len = urn.length();
105
106                 for (int i = 0; i < len; i++) {
107                         char c = urn.charAt(i);
108
109                         if (!Character.isLetterOrDigit(c) && c != '_') {
110                                 c = '_';
111                         }
112
113                         if (!(c == '_' && last == '_')) {
114                                 builder.append(c);
115                         }
116
117                         last = c;
118                 }
119
120                 return builder.toString();
121         }
122 }