+ private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
+ String primaryKey) {
+ JsonNode tmp = tmpJsonNode;
+ ObjectNode objectNode = (ObjectNode) tmp;
+ JsonNode jsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ JsonNode tempRuleJsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ String[] tempSt = parseKey.split("\\.");
+ for (String value : tempSt) {
+ if (value.contains("[")) {
+ if (tempRuleJsonNode instanceof ArrayNode) {
+ JsonNode tempRuleNode = tempRuleJsonNode.get(item);
+ ((ArrayNode) tempRuleJsonNode).removeAll();
+ ((ArrayNode) tempRuleJsonNode).add(tempRuleNode);
+ objectNode = (ObjectNode) tempRuleJsonNode.get(item);
+ }
+ String key = value.substring(0, value.indexOf('['));
+ objectNode.remove(key);
+ objectNode.put(key, sb.toString());
+ return tmp;
+ } else {
+ jsonNode = jsonNode.get(value);
+ if (jsonNode instanceof ArrayNode) {
+ tempRuleJsonNode = jsonNode;
+ jsonNode = jsonNode.get(item);
+ }
+ }
+ }
+ return tmp;
+ }
+
+ public boolean checkPattern(String patternString) {
+ return PATTERN.matcher(patternString).find();
+ }
+
+ /**
+ * Construct rule.
+ *
+ * @param sb the sb
+ * @param jsonNode the json node
+ * @param sigRules2 the sig rules 2
+ */
+ public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
+ int count = 0;
+ String cleanJsonNode = jsonNode.replace("\"\"", " ");
+ cleanJsonNode = cleanJsonNode.replaceAll("\"", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\(", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\)", "");
+ boolean flag = false;
+ if (cleanJsonNode.contains("OR")) {
+ sb.append("(");
+ flag = true;
+ }
+ for (String rowValue : cleanJsonNode.split(" ")) {
+ if (checkPattern(rowValue)) {
+ String value = sigRules2.get(rowValue);
+ LOGGER.info(" Value is:" + value);
+ constructRule(sb, value, sigRules2);
+ } else {
+ if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
+ sb.append("(");
+ }
+ count++;
+ LOGGER.info(" " + rowValue + " ");
+ sb.append(" " + rowValue + " ");
+ if (count % 3 == 0) {
+ sb.append(")");
+ count = 0;
+ }
+ }
+ }
+ if (flag) {
+ sb.append(")");
+ }
+ }
+
+ /**
+ * Parses the data.
+ *
+ * @param jsonNode the json node
+ * @param string the string
+ */
+ public void parseData(JsonNode jsonNode, String string) {
+ if (string.contains(".")) {
+ String firstIndex = string.substring(0, string.indexOf('.'));
+ JsonNode signtures = jsonNode.get(firstIndex);
+ String subIndex = string.substring(firstIndex.length() + 1);
+ if (signtures instanceof ArrayNode) {
+ for (int i = 0; i < signtures.size(); i++) {
+ parseData(signtures.get(i), subIndex);
+ }
+ } else {
+ parseData(signtures, subIndex);
+ }
+ } else {
+ if (string.contains("[")) {
+ String ruleIndex = string.substring(0, string.indexOf('['));
+ String[] keys = string.substring(string.indexOf('[') + 1, string.lastIndexOf(']')).split(",");
+ String key = "A" + Integer.valueOf(sigRules.size() + 1);
+ JsonNode node = jsonNode.get(ruleIndex);
+ StringBuilder sb = new StringBuilder("(");
+ for (int i = 0; i < keys.length; i++) {
+ sb.append(node.get(keys[i].trim()));
+ }
+ sb.append(")");
+ sigRules.put(key, sb.toString());
+ }
+ }
+ }
+