- * Given a version string consisting of integers with dots between them, convert it into an array of ints.
- *
- * @param version
- * @return
- * @throws NumberFormatException
- */
- public static int[] versionStringToArray(String version) throws NumberFormatException {
- if (version == null || version.length() == 0) {
- return new int[0];
- }
- String[] stringArray = version.split("\\.");
- int[] resultArray = new int[stringArray.length];
- for (int i = 0; i < stringArray.length; i++) {
- resultArray[i] = Integer.parseInt(stringArray[i]);
- }
- return resultArray;
- }
-}
+ * Validates if the given string is proper JSON format.
+ *
+ * @param data
+ * @return
+ */
+ public static boolean isJSONValid(String data) {
+ try{
+ JsonParser parser = new JsonParser();
+ parser.parse(data);
+ }catch(JsonSyntaxException e){
+ LOGGER.error("Exception Occurred While Validating"+e);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Validates if the given string is proper XML format.
+ *
+ * @param data
+ * @return
+ */
+ public static boolean isXMLValid(String data) {
+ if(data == null || data.isEmpty()){
+ return false;
+ }
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ factory.setValidating(false);
+ factory.setNamespaceAware(true);
+
+ try {
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ SAXParser parser = factory.newSAXParser();
+ XMLReader reader = parser.getXMLReader();
+ reader.setErrorHandler(new XMLErrorHandler());
+ reader.parse(new InputSource(new StringReader(data)));
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured While Validating"+e);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Validates if given string is valid Properties format.
+ *
+ * @param prop
+ * @return
+ */
+ public static boolean isPropValid(String prop) {
+ Scanner scanner = new Scanner(prop);
+ while (scanner.hasNextLine()) {
+ String line = scanner.nextLine();
+ line = line.replaceAll("\\s+", "");
+ if (line.startsWith("#")) {
+ continue;
+ } else {
+ if (line.contains("=")) {
+ String[] parts = line.split("=");
+ if (parts.length < 2) {
+ scanner.close();
+ return false;
+ }
+ } else if(!line.trim().isEmpty()){
+ scanner.close();
+ return false;
+ }
+ }
+ }
+ scanner.close();
+ return true;
+ }
+
+ /**
+ * Given a version string consisting of integers with dots between them, convert it into an array of integers.
+ *
+ * @param version
+ * @return
+ * @throws NumberFormatException
+ */
+ public static int[] versionStringToArray(String version){
+ if (version == null || version.length() == 0) {
+ return new int[0];
+ }
+ String[] stringArray = version.split("\\.");
+ int[] resultArray = new int[stringArray.length];
+ for (int i = 0; i < stringArray.length; i++) {
+ resultArray[i] = Integer.parseInt(stringArray[i].trim());
+ }
+ return resultArray;
+ }
+}
\ No newline at end of file