/** * Convert 'json object' to a certain type. * * @param clazz Class<T>, the expected type to which the json object will be converted. * @param jsonData Object, the json data object. * @return T, the converted 'json object'; null if something wrong happened. */ public static <T> T convert(Class<T> clazz, Object jsonData) { try { if (clazz == null || jsonData == null) { IndependantLog.error(StringUtils.debugmsg(false) + " converted-clazz or jsonData is null."); return null; } JsonToBeanConverter converter = new JsonToBeanConverter(); return converter.convert(clazz, jsonData); } catch (JsonException e) { IndependantLog.error( StringUtils.debugmsg(false) + "Fail to get convert JSON data to " + clazz.getSimpleName(), e); throw null; } }
/** @return Rectangle, the absolute rectangle on screen for the component */ protected Rectangle getComponentRectangle() { Rectangle compRect = getComponentBounds(component()); if (compRect == null) IndependantLog.warn("Fail to get bounds for " + windowName + ":" + compName + " on screen."); return compRect; }
protected boolean performHoverMouse(Point point, int milliseconds) throws SAFSException { String debugmsg = StringUtils.debugmsg(false); try { // TODO handle the point String locator = component().getLocator(); IndependantLog.debug(debugmsg + " hover on element '" + locator + "'"); selenium.mouseOver(locator); StringUtilities.sleep(milliseconds); selenium.mouseOut(locator); } catch (Exception e) { IndependantLog.error(debugmsg + " fail to hover, due to " + StringUtils.debugmsg(e)); return false; } return true; }
/** @return Rectangle, the absolute rectangle on screen for the window */ protected Rectangle getWindowRectangleOnScreen() { Rectangle rect = getComponentBounds(winObject); if (rect == null) IndependantLog.warn( "Fail to get bounds for " + windowName + ":" + windowName + " on screen."); return rect; }
/** * Read a JSON data file, and convert the data into a Java Map and return it.<br> * * @param file String, the absolute JSON file. * @param encoding String, the JSON file encoding. * @return Map, the JSON data as a Map; null if something wrong happened. */ public static Map<?, ?> readJSONFile(String file, String encoding) { try { return convert(Map.class, FileUtilities.readStringFromEncodingFile(file, encoding)); } catch (Exception e) { IndependantLog.error("Fail to get JSON data as a Map.", e); return null; } }
private SGuiObject component() { String debugmsg = StringUtils.debugmsg(false); if (compObject == null) { IndependantLog.warn( debugmsg + " the 'componet object' is null, use the 'window object' instead."); // winObject will never be null, as in process() it has been checked. return winObject; } return compObject; }
/** * Read String as input to find encoding. If no encoding is detected, the default system encoding * will be returned. {@link http://code.google.com/p/juniversalchardet/} {@link * http://www-archive.mozilla.org/projects/intl/UniversalCharsetDetection.html} * * @param String * @return String, the file encoding, it might be null. * @see FileUtilities#detectFileEncoding(String) */ public static String detectStringEncoding(String str) { String encoding = null; try { UniversalDetector detector = new UniversalDetector(null); detector.handleData(str.getBytes(), 0, str.getBytes().length); detector.dataEnd(); // Get the file encoding string (defined in org.mozilla.universalchardet.Constants) encoding = detector.getDetectedCharset(); detector.reset(); } catch (Exception e) { IndependantLog.warn(StringUtils.debugmsg(false) + StringUtils.debugmsg(e)); } // If no encoding is detected, get the default file encoding try { if (encoding == null || encoding.trim().isEmpty()) encoding = System.getProperty("file.encoding"); if (!Charset.isSupported(encoding)) { String error = "The detected encoding is '" + encoding + "', it is not supported by java.nio.charset.Charset"; IndependantLog.warn( error); // We need to map org.mozilla.universalchardet.Constants and // java.nio.charset.Charset ? throw new IOException(error); } } catch (Exception e) { IndependantLog.warn(StringUtils.debugmsg(false) + StringUtils.debugmsg(e)); } return encoding; }
/** * Opens a FileInputStream and detects its encoding. If no encoding is detected, the default * system encoding will be returned. {@link http://code.google.com/p/juniversalchardet/} {@link * http://www-archive.mozilla.org/projects/intl/UniversalCharsetDetection.html} * * @param filename case-insensitive absolute filename path. * @return String, the file encoding, it might be null. * @see FileUtilities#detectFileEncoding(String) */ public static String detectFileEncoding(String filename) { byte[] buf = new byte[4096]; int nread = 0; int totalReadBytes = 0; String encoding = null; FileInputStream fis = null; try { fis = new FileInputStream(new CaseInsensitiveFile(filename).toFile()); UniversalDetector detector = new UniversalDetector(null); // Feed detector with bytes until it detect the encoding or reach the max bytes to read // if the detector can't get the encoding after reading some bytes, we should stop to save // time. while ((nread = fis.read(buf)) > 0 && !detector.isDone() && totalReadBytes < MAX_BTYES_TO_READ) { totalReadBytes += nread; detector.handleData(buf, 0, nread); } if (!detector.isDone()) { // IndependantLog.warn("Didn't detect file encoding after reading "+totalReadBytes+" // bytes."); } detector.dataEnd(); // Get the file encoding string (defined in org.mozilla.universalchardet.Constants) encoding = detector.getDetectedCharset(); detector.reset(); } catch (Exception e) { IndependantLog.warn(StringUtils.debugmsg(false) + StringUtils.debugmsg(e)); } finally { if (fis != null) try { fis.close(); } catch (Exception e) { } } // If no encoding is detected, test if it is "utf-8" try { if (encoding == null || encoding.trim().isEmpty()) if (FileUtilities.isFileUTF8(filename)) encoding = "UTF-8"; } catch (Exception e) { IndependantLog.warn(StringUtils.debugmsg(false) + StringUtils.debugmsg(e)); } // If no encoding is detected, get the default file encoding try { if (encoding == null || encoding.trim().isEmpty()) encoding = System.getProperty("file.encoding"); if (!Charset.isSupported(encoding)) { String error = "The detected encoding is '" + encoding + "', it is not supported by java.nio.charset.Charset"; IndependantLog.warn( error); // We need to map org.mozilla.universalchardet.Constants and // java.nio.charset.Charset ? throw new IOException(error); } } catch (Exception e) { IndependantLog.warn(StringUtils.debugmsg(false) + StringUtils.debugmsg(e)); } return encoding; }