|
| 1 | +package com.checkmarx.ast.codebashing; |
| 2 | + |
| 3 | +import com.checkmarx.ast.predicate.Predicate; |
| 4 | +import com.checkmarx.ast.wrapper.CxBaseObject; |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 7 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | +import com.fasterxml.jackson.databind.JavaType; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 12 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 13 | +import lombok.EqualsAndHashCode; |
| 14 | +import lombok.Value; |
| 15 | +import org.apache.commons.lang3.StringUtils; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +@Value |
| 22 | +@JsonDeserialize() |
| 23 | +@JsonInclude(JsonInclude.Include.NON_NULL) |
| 24 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 25 | +public class CodeBashing { |
| 26 | + String path; |
| 27 | + String cweId; |
| 28 | + String language; |
| 29 | + String queryName; |
| 30 | + |
| 31 | + @JsonCreator |
| 32 | + public CodeBashing(@JsonProperty("path") String path, |
| 33 | + @JsonProperty("cwe_id") String cweId, |
| 34 | + @JsonProperty("lang") String language, |
| 35 | + @JsonProperty("cxQueryName") String queryName) { |
| 36 | + this.path=path; |
| 37 | + this.cweId=cweId; |
| 38 | + this.language=language; |
| 39 | + this.queryName=queryName; |
| 40 | + } |
| 41 | + |
| 42 | + public static <T> T fromLine(String line) { |
| 43 | + return parse(line, TypeFactory.defaultInstance().constructType(Predicate.class)); |
| 44 | + } |
| 45 | + |
| 46 | + public static <T> List<T> listFromLine(String line) { |
| 47 | + return parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, CodeBashing.class)); |
| 48 | + } |
| 49 | + |
| 50 | + protected static <T> T parse(String line, JavaType type) { |
| 51 | + T result = null; |
| 52 | + try { |
| 53 | + if (!StringUtils.isBlank(line) && isValidJSON(line)) { |
| 54 | + result = new ObjectMapper().readValue(line, type); |
| 55 | + |
| 56 | + } |
| 57 | + } catch (IOException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + private static boolean isValidJSON(final String json) { |
| 64 | + try { |
| 65 | + final ObjectMapper mapper = new ObjectMapper(); |
| 66 | + mapper.readTree(json); |
| 67 | + return true; |
| 68 | + } catch (IOException e) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +} |
| 74 | + |
0 commit comments