Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/meta/YamlStringEscapeUtils.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib.meta;
18
19 import org.apache.commons.text.StringEscapeUtils;
20 import org.apache.commons.text.translate.CharSequenceTranslator;
21
22 import java.io.IOException;
23 import java.io.StringWriter;
24 import java.io.Writer;
25
26 public class YamlStringEscapeUtils {
27
28 public static String escapeString(String str) {
29 return escapeJavaStyleString(str, false, false);
30 }
31
32 /**
33 * @param str String to escape values in, may be null
34 * @param escapeSingleQuotes escapes single quotes if <code>true</code>
35 * @param escapeForwardSlash TODO
36 * @return the escaped string
37 */
38 private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) {
39 if (str == null) {
40 return null;
41 }
42 try {
43 StringWriter writer = new StringWriter(str.length() * 2);
44 escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
45 return writer.toString();
46 } catch (IOException ioe) {
47 // this should never ever happen while writing to a StringWriter
48 throw new RuntimeException(ioe);
49 }
50 }
51
52 /**
53 * @param out write to receieve the escaped string
54 * @param str String to escape values in, may be null
55 * @param escapeSingleQuote escapes single quotes if <code>true</code>
56 * @param escapeForwardSlash TODO
57 * @throws IOException if an IOException occurs
58 */
59 private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
60 boolean escapeForwardSlash) throws IOException {
61 if (out == null) {
62 throw new IllegalArgumentException("The Writer must not be null");
63 }
64 if (str == null) {
65 return;
66 }
67 int sz;
68 sz = str.length();
69 for (int i = 0; i < sz; i++) {
70 char ch = str.charAt(i);
71 // "[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]"
72 // handle unicode
73 if (ch > 0xFFFD) {
74 out.write("\\u" + CharSequenceTranslator.hex(ch));
75 } else if (ch > 0xD7FF && ch < 0xE000) {
76 out.write("\\u" + CharSequenceTranslator.hex(ch));
77 } else if (ch > 0x7E && ch != 0x85 && ch < 0xA0) {
78 out.write("\\u00" + CharSequenceTranslator.hex(ch));
79 } else if (ch < 32) {
80 switch (ch) {
81 case '\t' :
82 out.write('\\');
83 out.write('t');
84 break;
85 case '\n' :
86 out.write('\\');
87 out.write('n');
88 break;
89 case '\r' :
90 out.write('\\');
91 out.write('r');
92 break;
93 default :
94 if (ch > 0xf) {
95 out.write("\\u00" + CharSequenceTranslator.hex(ch));
96 } else {
97 out.write("\\u000" + CharSequenceTranslator.hex(ch));
98 }
99 break;
100 }
101 } else {
102 switch (ch) {
103 case '\'' :
104 if (escapeSingleQuote) {
105 out.write('\\');
106 }
107 out.write('\'');
108 break;
109 case '"' :
110 out.write('\\');
111 out.write('"');
112 break;
113 case '\\' :
114 out.write('\\');
115 out.write('\\');
116 break;
117 case '/' :
118 if (escapeForwardSlash) {
119 out.write('\\');
120 }
121 out.write('/');
122 break;
123 default :
124 out.write(ch);
125 break;
126 }
127 }
128 }
129 }
130
131 /**
132 * <p>Unescapes any Java literals found in the <code>String</code>.
133 * For example, it will turn a sequence of <code>'\'</code> and
134 * <code>'n'</code> into a newline character, unless the <code>'\'</code>
135 * is preceded by another <code>'\'</code>.</p>
136 *
137 * @param str the <code>String</code> to unescape, may be null
138 * @return a new unescaped <code>String</code>, <code>null</code> if null string input
139 */
140 public static String unescapeString(String str) {
141 return StringEscapeUtils.unescapeJava(str);
142 }
143 }