001package com.ganteater.ae.util; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.FileNotFoundException; 006import java.io.IOException; 007import java.io.InputStream; 008import java.io.StringReader; 009import java.net.MalformedURLException; 010import java.net.URL; 011import java.text.ParseException; 012import java.util.LinkedHashMap; 013import java.util.Map; 014import java.util.Map.Entry; 015import java.util.Properties; 016import java.util.Set; 017import java.util.TreeMap; 018 019import org.apache.commons.io.IOUtils; 020import org.apache.commons.lang.StringUtils; 021import org.apache.commons.lang.Validate; 022import org.apache.sling.commons.json.JSONException; 023import org.apache.sling.commons.json.JSONObject; 024 025import com.ganteater.ae.util.xml.easyparser.EasyParser; 026import com.ganteater.ae.util.xml.easyparser.Node; 027 028@SuppressWarnings("deprecation") 029public class AEUtils { 030 031 public static InputStream getInputStream(String aFileName, File aBaseDir) 032 throws FileNotFoundException, IOException, MalformedURLException { 033 InputStream theInputStream = null; 034 035 if ('/' == aFileName.charAt(0) || '\\' == aFileName.charAt(0)) { 036 theInputStream = new FileInputStream(aFileName); 037 038 } else if (aFileName.startsWith("jar:file:")) { 039 theInputStream = new URL(aFileName.replace('\\', '/')).openStream(); 040 041 } else if (aBaseDir.toString().startsWith("jar:file:")) { 042 043 String basePath = aBaseDir.toString(); 044 basePath = StringUtils.substringBefore(basePath, "!") + "!" 045 + StringUtils.substringAfter(basePath, "!").replace('\\', '/'); 046 String spec = basePath + "/" + aFileName.replace('\\', '/'); 047 theInputStream = new URL(spec).openStream(); 048 049 } else { 050 File file = new File(aFileName); 051 if (!file.exists()) { 052 file = new File(aBaseDir, aFileName); 053 } 054 theInputStream = new FileInputStream(file); 055 056 } 057 return theInputStream; 058 } 059 060 public static String loadResource(String aFileName, File aBaseDir, String aEncode) 061 throws IOException, ParseException { 062 Validate.notEmpty(aFileName, "Request file name is empty."); 063 064 InputStream theInputStream = AEUtils.getInputStream(aFileName, aBaseDir); 065 066 String theEncode = aEncode; 067 068 Node theHead = null; 069 try { 070 071 byte[] theBuffer = IOUtils.toByteArray(theInputStream); 072 theInputStream.read(theBuffer); 073 074 int theEndHead = 0; 075 076 String theDocHead = new String(theBuffer, "UTF-8"); 077 if (theDocHead.indexOf("<?xml") == 0) { 078 theEndHead = theDocHead.indexOf("?>"); 079 EasyParser theParser = new EasyParser(); 080 theHead = theParser.getObject(theDocHead.substring(0, theEndHead) + "/>"); 081 theEncode = theHead.getAttribute("encoding"); 082 theEndHead += 2; 083 } 084 085 if (theEncode == null) 086 theEncode = "UTF-8"; 087 088 return new String(theBuffer, theEncode); 089 090 } finally { 091 if (theInputStream != null) 092 theInputStream.close(); 093 } 094 095 } 096 097 public static Map<String, String> convertStringToMap(String value) { 098 Map<String, String> theValue = null; 099 if (value != null) { 100 Properties props = new Properties(); 101 try { 102 Map<String, String> map2 = new LinkedHashMap<String, String>(); 103 for (Map.Entry<Object, Object> e : props.entrySet()) { 104 map2.put((String) e.getKey(), (String) e.getValue()); 105 } 106 String replace = value.substring(1, value.length() - 1).replace(", ", "\n"); 107 props.load(new StringReader(replace)); 108 theValue = new TreeMap<>(); 109 Set<Entry<Object, Object>> entrySet = props.entrySet(); 110 for (Entry<Object, Object> entry : entrySet) { 111 theValue.put((String) entry.getKey(), (String) entry.getValue()); 112 } 113 } catch (IOException e1) { 114 // skip 115 } 116 } 117 118 return theValue; 119 } 120 121 public static String format(String text) { 122 StringBuilder formatedText = new StringBuilder(); 123 124 boolean jsonDetected = false; 125 for (int i = 0; i < text.length(); i++) { 126 char charAt = text.charAt(i); 127 128 if (charAt == '{') { 129 jsonDetected = true; 130 } 131 if (!jsonDetected) { 132 formatedText.append(charAt); 133 } else { 134 StringBuilder jsonBody = new StringBuilder(); 135 136 int counter = 0; 137 for (; i < text.length(); i++) { 138 charAt = text.charAt(i); 139 140 if (charAt == '{') { 141 counter++; 142 } 143 144 if (charAt == '}') { 145 counter--; 146 } 147 148 jsonBody.append(charAt); 149 150 if (counter < 1) { 151 jsonDetected = false; 152 break; 153 } 154 } 155 156 int length = formatedText.toString().trim().length(); 157 formatedText.setLength(length); 158 if (length > 0) { 159 formatedText.append("\n"); 160 } 161 162 try { 163 formatedText.append(new JSONObject(jsonBody.toString()).toString(2)); 164 } catch (JSONException e) { 165 formatedText.append(jsonBody.toString()); 166 } 167 } 168 } 169 return formatedText.toString(); 170 } 171 172 public static boolean isEmpty(Object value) { 173 boolean result; 174 if (value instanceof String) { 175 result = StringUtils.isEmpty((String) value); 176 } else if (value instanceof String[]) { 177 result = ((String[]) value).length == 0; 178 } else { 179 result = value == null; 180 } 181 return result; 182 } 183 184 public static String maskEmail(String text) { 185 String atSignSymbol = "@"; 186 String[] terminator = new String[] { " ", "=", "\"", "'", "\t", "\n", "\r" }; 187 int notMaskLength = 5; 188 189 if (StringUtils.indexOf(text, atSignSymbol) > 0) { 190 String[] split = StringUtils.split(text, atSignSymbol); 191 boolean skipNext = false; 192 for (int i = 0; i < split.length; i++) { 193 String line = split[i]; 194 int index = StringUtils.lastIndexOfAny(line, terminator); 195 int maskLength = line.length() - index - notMaskLength; 196 197 if (skipNext) { 198 skipNext = false; 199 } else { 200 if (notMaskLength + maskLength <= 1) { 201 skipNext = true; 202 } else { 203 int curMaskLength = maskLength; 204 if (i < split.length - 1) { 205 int startMask = index + notMaskLength + 1; 206 if (startMask >= line.length()) { 207 startMask = index + 1; 208 curMaskLength = line.length() - startMask; 209 } 210 split[i] = StringUtils.substring(line, 0, startMask) 211 + StringUtils.repeat("*", curMaskLength); 212 } else { 213 split[i] = line; 214 } 215 if (i > 0) { 216 line = split[i]; 217 index = StringUtils.indexOfAny(line, terminator); 218 if (index < 0) { 219 index = line.length(); 220 } 221 index = index - notMaskLength; 222 split[i] = StringUtils.repeat("*", index) 223 + StringUtils.substring(line, index, line.length()); 224 } 225 } 226 } 227 } 228 text = StringUtils.join(split, atSignSymbol); 229 } 230 return text; 231 } 232 233 public static String getFullClassName(String className, String defaultPackage) { 234 if (!StringUtils.contains(className, '.')) { 235 className = defaultPackage + "." + className; 236 } 237 return className; 238 } 239 240 public static String getPrjProperty(String name) throws IOException { 241 String result = null; 242 String propertiesFileName = "prj.properties"; 243 244 try (InputStream inputStream = AEUtils.class.getClassLoader().getResourceAsStream(propertiesFileName)) { 245 if (inputStream != null) { 246 Properties properties = new Properties(); 247 properties.load(inputStream); 248 result = properties.getProperty(name); 249 } 250 return result; 251 } 252 } 253 254}