001/* 002MIT License 003 004Copyright (c) 2020 FBSQL Team 005 006Permission is hereby granted, free of charge, to any person obtaining a copy 007of this software and associated documentation files (the "Software"), to deal 008in the Software without restriction, including without limitation the rights 009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010copies of the Software, and to permit persons to whom the Software is 011furnished to do so, subject to the following conditions: 012 013The above copyright notice and this permission notice shall be included in all 014copies or substantial portions of the Software. 015 016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 022SOFTWARE. 023 024Home: https://fbsql.github.io 025E-Mail: fbsql.team@gmail.com 026*/ 027 028package org.fbsql.servlet; 029 030import java.io.IOException; 031import java.io.InputStream; 032import java.nio.charset.StandardCharsets; 033import java.nio.file.Files; 034import java.nio.file.Path; 035import java.text.MessageFormat; 036import java.util.Scanner; 037 038public class StringUtils { 039 040 /** 041 * Escape JSON string 042 * 043 * @param s - source string 044 * @return - escaped string 045 */ 046 public static String escapeJson(String s) { 047 s = s.replace("\\", "\\\\"); 048 s = s.replace("\"", "\\\""); 049 s = s.replace("\b", "\\b"); 050 s = s.replace("\f", "\\f"); 051 s = s.replace("\n", "\\n"); 052 s = s.replace("\r", "\\r"); 053 s = s.replace("\t", "\\t"); 054 return s; 055 } 056 057 /** 058 * Format and escape message 059 * 060 * @param s - source string (message) 061 * @param objs - parameters 062 * @return - formatted and JSON escaped string 063 */ 064 public static String formatMessage(String s, Object... objs) { 065 return escapeJson(MessageFormat.format(s, objs)); 066 } 067 068 /** 069 * Wrap string into double quotes 070 * This is a helper method used to build JSON string 071 * 072 * @param s - source string 073 * @return - quoted string 074 */ 075 public static String q(String s) { 076 return '"' + escapeJson(s) + '"'; 077 } 078 079 /** 080 * Take given string and replace names like ${myvar} and $[myvar] 081 * with appropriate Java System Properties values or OS environment variables 082 * Syntax ${myvar} is used for Java System Properties values 083 * Syntax $[myvar] is used for OS environment variables 084 * 085 * @param s - source String 086 * @return - string with replaced values 087 */ 088 public static String putVars(String s) { 089 // 090 // replace system properties 091 // 092 while (true) { 093 int pos = s.indexOf("${"); // name begin 094 if (pos == -1) 095 break; 096 String rest = s.substring(pos + 2); 097 int pos2 = rest.indexOf("}"); // name end 098 String varName = rest.substring(0, pos2); 099 String replacer = System.getProperty(varName); 100 if (replacer == null) 101 throw new IllegalArgumentException(varName); 102 s = s.substring(0, pos) + replacer + s.substring(pos + varName.length() + 3); 103 } 104 // 105 // replace OS environment variables 106 // 107 while (true) { 108 int pos = s.indexOf("$["); // name begin 109 if (pos == -1) 110 break; 111 String rest = s.substring(pos + 2); 112 int pos2 = rest.indexOf("]"); // name end 113 String varName = rest.substring(0, pos2); 114 String replacer = System.getenv(varName); 115 if (replacer == null) 116 throw new IllegalArgumentException(varName); 117 s = s.substring(0, pos) + replacer + s.substring(pos + varName.length() + 3); 118 } 119 return s; 120 } 121 122 /** 123 * 124 * @param path 125 * @return 126 */ 127 public static String readAsText(Path path) { 128 try { 129 return new String(Files.readAllBytes(path), StandardCharsets.UTF_8); 130 } catch (IOException e) { 131 return ""; 132 } 133 } 134 135 /** 136 * 137 * @param s 138 * @return 139 */ 140 public static String unquote(String s) { 141 char quote = s.charAt(0); 142 if (quote == '\'' || quote == '"') 143 return s.substring(1, s.length() - 1).trim(); 144 return s; 145 } 146 147 public static String inputSreamToString(InputStream is) { 148 Scanner s = new Scanner(is, StandardCharsets.UTF_8.name()).useDelimiter("\\A"); 149 return s.hasNext() ? s.next().trim() : ""; 150 } 151 152} 153 154/* 155Please contact FBSQL Team by E-Mail fbsql.team@gmail.com 156or visit https://fbsql.github.io if you need additional 157information or have any questions. 158*/ 159 160/* EOF */