Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package DataTools;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
Copyright 2016 Alianza Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public class Utils {
public static boolean loggingEnabled = true;
public static boolean debugEnabled = true;
public static boolean logWarningsEnabled = true;
public static int debugLevel = 1;
/**
* Verify actual json array (used to check the values of keys)
* @param array String to check if it's a JSONArray
* @return Boolean
*/
public static boolean isJsonArray(String array) {
//[] is an array, but if there is nothing in it
//if it is only 2 char then not going to be treated like an array
return array.startsWith("[") && array.endsWith("]") && array.length() > 2;
}
/**
* Verify actual json object (used to check the values of keys)
* @param object String to check if it's a JSONObject
* @return Boolean
*/
public static boolean isJSONObject(String object) {
//needs to have a : to seperate fields or be empty, less than 4 i'll say is empty
return object.startsWith("{") && object.endsWith("}") && (object.contains(":") || object.length() < 4);
}
/**
* used when finding elements in path for puts, need the array minus the last one
* the last is removed to use as the key
* @param array string array to start with
* @return String[] smaller array, all but the last item in the array
*/
public static String[] copyArrayExceptLast(String[] array) {
String[] smaller = new String[array.length - 1];
for (int i = 0; i < smaller.length; ++i) {
smaller[i] = array[i];
}
return smaller;
}
/**
* If no level is provided, always print it out as info
* @param msg Message to debug
*/
public static void debug(Object msg) {
debug(msg, "Info");
}
/**
* Shorthand for doing a debug level (ie debug(msg, 3) instead of debug(msg, "Debug3"))
* @param msg Message to debug
* @param level debug level, short for "Debug"+level
*/
public static void debug(Object msg, Integer level) {
if(level > 3) {
level = 3;
}
else if (level <= 0) {
level = 0;
}
String newLevel = "Debug" + level.toString();
debug(msg, newLevel);
}
/**
* Print out and log debug information in useful places, with varying levels (1-3)
* @param msg Message to print out/log
* @param level Debug level for the message (1-3, warning, error, info)
*/
public static void debug(Object msg, String level) {
String newMsg = null;
switch(level.toLowerCase()) {
case "debug1":
if(debugLevel > 0)
newMsg = formatDebugMessage("DEBUG1", msg);
break;
case "debug2":
if(debugLevel > 1)
newMsg = formatDebugMessage("DEBUG2", msg);
break;
case "debug3":
if(debugLevel > 2)
newMsg = formatDebugMessage("DEBUG3", msg);
break;
case "warning":
if(debugLevel > 0 && logWarningsEnabled)
newMsg = formatDebugMessage("WARNING", msg);
break;
case "error":
newMsg = formatDebugMessage("ERROR", msg);
break;
default:
newMsg = formatDebugMessage("INFO", msg);
}
if(newMsg != null) {
// only print debug types if debug is enabled, always print warnings, errors, and info messages
if (level.toLowerCase().contains("debug")) {
if (debugEnabled) {
System.out.println(newMsg);
}
}
else {
System.out.println(newMsg);
}
if(loggingEnabled) {
appendToFile("sensjector.log", newMsg);
}
}
}
/**
* Format our debug message with a timestamp and debug level
* @param debugLevel DEBUG1, DEBUG2, DEBUG3, WARNING, etc.
* @param msg Message to be formatted
* @return String
*/
private static String formatDebugMessage(String debugLevel, Object msg) {
String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH-mm-ss").format(new Date());
return "[" + debugLevel + "] [" + timeStamp + "] " + msg.toString();
}
/**
* Append a line of text to a file
* @param file Filename/path of file to append to
* @param line Line of text to append to file
*/
private static void appendToFile(String file, String line) {
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) {
out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}