36afe76ae69256ff5f0d174773f09cafa3f61a24
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / openecomp / sdnc / config / generator / tool / EscapeUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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.sdnc.config.generator.tool;
22
23 import org.apache.commons.lang3.StringUtils;
24
25 public class EscapeUtils {
26
27     public EscapeUtils() {
28         // TODO Auto-generated constructor stub
29     }
30
31     public static String escapeSql(String str) {
32         if (str == null) {
33             return null;
34         }
35         String searchList[] = new String[]{"'","\\"};
36         String replacementList[] = new String[]{ "''","\\\\"};
37         return StringUtils.replaceEach(str,searchList, replacementList);
38     }
39     
40     public static String unescapeSql(String str) {
41         if (str == null) {
42             return null;
43         }
44         
45         String searchList[] = new String[] {"''"};
46         String replacementList[] = new String[] {"'"};
47         return StringUtils.replaceEach(str, searchList, replacementList);
48     }
49
50
51     // For Generic Purpose
52     public static String escapeSQL(String s) {
53         if (s == null) {
54             return null;
55         }
56
57         int length = s.length();
58         int newLength = length;
59         for (int i = 0; i < length; i++) {
60             char c = s.charAt(i);
61             switch (c) {
62             case '\\':
63             case '\"':
64             case '\'':
65             case '\0': {
66                 newLength += 1;
67             }
68                 break;
69             }
70         }
71         if (length == newLength) {
72             // nothing to escape in the string
73             return s;
74         }
75         StringBuffer sb = new StringBuffer(newLength);
76         for (int i = 0; i < length; i++) {
77             char c = s.charAt(i);
78             switch (c) {
79             case '\\': {
80                 sb.append("\\\\");
81             }
82                 break;
83             case '\"': {
84                 sb.append("\\\"");
85             }
86                 break;
87             case '\'': {
88                 sb.append("\\\'");
89             }
90                 break;
91             case '\0': {
92                 sb.append("\\0");
93             }
94                 break;
95             default: {
96                 sb.append(c);
97             }
98             }
99         }
100         return sb.toString();
101     }
102 }