f783d4c69519fee55e79660399150b2f22d5cd0d
[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     // For Generic Purpose
41     public static String escapeSQL(String s) {
42         if (s == null) {
43             return null;
44         }
45
46         int length = s.length();
47         int newLength = length;
48         for (int i = 0; i < length; i++) {
49             char c = s.charAt(i);
50             switch (c) {
51             case '\\':
52             case '\"':
53             case '\'':
54             case '\0': {
55                 newLength += 1;
56             }
57                 break;
58             }
59         }
60         if (length == newLength) {
61             // nothing to escape in the string
62             return s;
63         }
64         StringBuffer sb = new StringBuffer(newLength);
65         for (int i = 0; i < length; i++) {
66             char c = s.charAt(i);
67             switch (c) {
68             case '\\': {
69                 sb.append("\\\\");
70             }
71                 break;
72             case '\"': {
73                 sb.append("\\\"");
74             }
75                 break;
76             case '\'': {
77                 sb.append("\\\'");
78             }
79                 break;
80             case '\0': {
81                 sb.append("\\0");
82             }
83                 break;
84             default: {
85                 sb.append(c);
86             }
87             }
88         }
89         return sb.toString();
90     }
91 }