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                try (InputStream theInputStream = AEUtils.getInputStream(aFileName, aBaseDir)) {
065                        String theEncode = aEncode;
066
067                        Node theHead = null;
068
069                        byte[] theBuffer = IOUtils.toByteArray(theInputStream);
070                        theInputStream.read(theBuffer);
071
072                        int theEndHead = 0;
073
074                        String theDocHead = new String(theBuffer, "UTF-8");
075                        if (theDocHead.indexOf("<?xml") == 0) {
076                                theEndHead = theDocHead.indexOf("?>");
077                                EasyParser theParser = new EasyParser();
078                                theHead = theParser.getObject(theDocHead.substring(0, theEndHead) + "/>");
079                                theEncode = theHead.getAttribute("encoding");
080                                theEndHead += 2;
081                        }
082
083                        if (theEncode == null)
084                                theEncode = "UTF-8";
085
086                        return new String(theBuffer, theEncode);
087                }
088
089        }
090
091        public static Map<String, String> convertStringToMap(String value) {
092                Map<String, String> theValue = null;
093                if (value != null) {
094                        Properties props = new Properties();
095                        try {
096                                Map<String, String> map2 = new LinkedHashMap<String, String>();
097                                for (Map.Entry<Object, Object> e : props.entrySet()) {
098                                        map2.put((String) e.getKey(), (String) e.getValue());
099                                }
100                                String replace = value.substring(1, value.length() - 1).replace(", ", "\n");
101                                props.load(new StringReader(replace));
102                                theValue = new TreeMap<>();
103                                Set<Entry<Object, Object>> entrySet = props.entrySet();
104                                for (Entry<Object, Object> entry : entrySet) {
105                                        theValue.put((String) entry.getKey(), (String) entry.getValue());
106                                }
107                        } catch (IOException e1) {
108                                // skip
109                        }
110                }
111
112                return theValue;
113        }
114
115        public static String format(String text) {
116                StringBuilder formatedText = new StringBuilder();
117
118                boolean jsonDetected = false;
119                for (int i = 0; i < text.length(); i++) {
120                        char charAt = text.charAt(i);
121
122                        if (charAt == '{') {
123                                jsonDetected = true;
124                        }
125                        if (!jsonDetected) {
126                                formatedText.append(charAt);
127                        } else {
128                                StringBuilder jsonBody = new StringBuilder();
129
130                                int counter = 0;
131                                for (; i < text.length(); i++) {
132                                        charAt = text.charAt(i);
133
134                                        if (charAt == '{') {
135                                                counter++;
136                                        }
137
138                                        if (charAt == '}') {
139                                                counter--;
140                                        }
141
142                                        jsonBody.append(charAt);
143
144                                        if (counter < 1) {
145                                                jsonDetected = false;
146                                                break;
147                                        }
148                                }
149
150                                int length = formatedText.toString().trim().length();
151                                formatedText.setLength(length);
152                                if (length > 0) {
153                                        formatedText.append("\n");
154                                }
155
156                                try {
157                                        formatedText.append(new JSONObject(jsonBody.toString()).toString(2));
158                                } catch (JSONException e) {
159                                        formatedText.append(jsonBody.toString());
160                                }
161                        }
162                }
163                return formatedText.toString();
164        }
165
166        public static boolean isEmpty(Object value) {
167                boolean result;
168                if (value instanceof String) {
169                        result = StringUtils.isEmpty((String) value);
170                } else if (value instanceof String[]) {
171                        result = ((String[]) value).length == 0;
172                } else {
173                        result = value == null;
174                }
175                return result;
176        }
177
178        public static String maskEmail(String text) {
179                String atSignSymbol = "@";
180                String[] terminator = new String[] { " ", "=", "\"", "'", "\t", "\n", "\r" };
181                int notMaskLength = 5;
182
183                if (StringUtils.indexOf(text, atSignSymbol) > 0) {
184                        String[] split = StringUtils.split(text, atSignSymbol);
185                        boolean skipNext = false;
186                        for (int i = 0; i < split.length; i++) {
187                                String line = split[i];
188                                int index = StringUtils.lastIndexOfAny(line, terminator);
189                                int maskLength = line.length() - index - notMaskLength;
190
191                                if (skipNext) {
192                                        skipNext = false;
193                                } else {
194                                        if (notMaskLength + maskLength <= 1) {
195                                                skipNext = true;
196                                        } else {
197                                                int curMaskLength = maskLength;
198                                                if (i < split.length - 1) {
199                                                        int startMask = index + notMaskLength + 1;
200                                                        if (startMask >= line.length()) {
201                                                                startMask = index + 1;
202                                                                curMaskLength = line.length() - startMask;
203                                                        }
204                                                        split[i] = StringUtils.substring(line, 0, startMask)
205                                                                        + StringUtils.repeat("*", curMaskLength);
206                                                } else {
207                                                        split[i] = line;
208                                                }
209                                                if (i > 0) {
210                                                        line = split[i];
211                                                        index = StringUtils.indexOfAny(line, terminator);
212                                                        if (index < 0) {
213                                                                index = line.length();
214                                                        }
215                                                        index = index - notMaskLength;
216                                                        split[i] = StringUtils.repeat("*", index)
217                                                                        + StringUtils.substring(line, index, line.length());
218                                                }
219                                        }
220                                }
221                        }
222                        text = StringUtils.join(split, atSignSymbol);
223                }
224                return text;
225        }
226
227        public static String getFullClassName(String className, String defaultPackage) {
228                if (!StringUtils.contains(className, '.')) {
229                        className = defaultPackage + "." + className;
230                }
231                return className;
232        }
233
234        public static String getPrjProperty(String name) throws IOException {
235                String result = null;
236                String propertiesFileName = "prj.properties";
237
238                try (InputStream inputStream = AEUtils.class.getClassLoader().getResourceAsStream(propertiesFileName)) {
239                        if (inputStream != null) {
240                                Properties properties = new Properties();
241                                properties.load(inputStream);
242                                result = properties.getProperty(name);
243                        }
244                        return result;
245                }
246        }
247
248}