Author: rezan
Date: Wed Jul 29 14:26:25 2015
New Revision: 1693271
URL: http://svn.apache.org/r1693271
Log:
non null json get
Modified:
devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
Modified: devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
URL:
http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java?rev=1693271&r1=1693270&r2=1693271&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java Wed Jul 29
14:26:25 2015
@@ -21,11 +21,14 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
public class DeviceMapClient {
public final static String VERSION = "2.0";
+ private static JsonNode nullNode = null;
+
private String domain;
private String domainVersion;
@@ -70,10 +73,10 @@ public class DeviceMapClient {
Main.log(" Loading pattern domain: " + patterns.getDomain() + ", version:
" +
patterns.getDomainVersion() + (patch ? ", patch" : ""));
- if(patterns.getJsonNode().get("inputParser") != null &&
patterns.getJsonNode().get("inputParser").isObject()) {
+ if(get(patterns.getJsonNode(), "inputParser").isObject()) {
JsonNode inputParser = patterns.getJsonNode().get("inputParser");
- if(inputParser.get("transformers") != null &&
inputParser.get("transformers").isArray()) {
+ if(get(inputParser, "transformers").isArray()) {
if(patch) {
transformers = new ArrayList<String>();
}
@@ -87,7 +90,7 @@ public class DeviceMapClient {
}
}
- if(inputParser.get("tokenSeperators") != null &&
inputParser.get("tokenSeperators").isArray()) {
+ if(get(inputParser, "tokenSeperators").isArray()) {
if(patch) {
tokenSeperators = new ArrayList<String>();
}
@@ -101,17 +104,17 @@ public class DeviceMapClient {
}
}
- if(inputParser.get("ngramConcatSize") != null &&
inputParser.get("ngramConcatSize").asInt(0) > 0) {
+ if(get(inputParser, "ngramConcatSize").asInt(0) > 0) {
ngramConcatSize = inputParser.get("ngramConcatSize").asInt(0);
Main.log(" Found ngramConcatSize: " + ngramConcatSize);
}
}
- if(patterns.getJsonNode().get("patternSet") != null &&
patterns.getJsonNode().get("patternSet").isObject()) {
+ if(get(patterns.getJsonNode(), "patternSet").isObject()) {
JsonNode patternSet = patterns.getJsonNode().get("patternSet");
- if(patternSet.get("defaultId") != null &&
patternSet.get("defaultId").isTextual()) {
+ if(get(patternSet, "defaultId").isTextual()) {
defaultId = patternSet.get("defaultId").asText();
Main.log(" Found defaultId: " + defaultId);
@@ -119,7 +122,7 @@ public class DeviceMapClient {
int patternCount = 0;
- if(patternSet.get("patterns") != null &&
patternSet.get("patterns").isArray()) {
+ if(get(patternSet, "patterns").isArray()) {
for(Iterator<JsonNode> i = patternSet.get("patterns").iterator();
i.hasNext();) {
JsonNode tokenSeperator = i.next();
@@ -148,34 +151,34 @@ public class DeviceMapClient {
int attributeCount = 0;
- if(attributes.getJsonNode().get("attributes") != null &&
attributes.getJsonNode().get("attributes").isArray()) {
+ if(get(attributes.getJsonNode(), "attributes").isArray()) {
for(Iterator<JsonNode> i =
attributes.getJsonNode().get("attributes").iterator(); i.hasNext();) {
JsonNode attribute = i.next();
- if(attribute.get("patternId") == null ||
attribute.get("patternId").asText().isEmpty()) {
+ if(get(attribute, "patternId").asText().isEmpty()) {
throw new Exception("Bad patternId found, position: " +
attributeCount);
}
String patternId = attribute.get("patternId").asText();
- if(attribute.get("attributes") != null &&
attribute.get("attributes").isObject()) {
+ if(get(attribute, "attributes").isObject()) {
for(Iterator<String> j =
attribute.get("attributes").getFieldNames(); j.hasNext();) {
String key = j.next();
String value = attribute.get("attributes").get(key).asText();
}
}
- if(attribute.get("attributeTransformers") != null &&
attribute.get("attributeTransformers").isArray()) {
+ if(get(attribute, "attributeTransformers").isArray()) {
for(Iterator<JsonNode> j =
attribute.get("attributeTransformers").iterator(); j.hasNext();) {
JsonNode attributeTransformer = j.next();
- if(attributeTransformer.get("name") == null ||
attributeTransformer.get("name").asText().isEmpty()) {
+ if(get(attributeTransformer, "name").asText().isEmpty()) {
throw new Exception("Bad attributeTransformer name, position:
" + attributeCount);
}
String name = attributeTransformer.get("name").asText();
- if(attributeTransformer.get("transformers") != null &&
attributeTransformer.get("transformers").isArray()) {
+ if(get(attributeTransformer, "transformers").isArray()) {
for(Iterator<JsonNode> k =
attributeTransformer.get("transformers").iterator(); k.hasNext();) {
JsonNode transformer = k.next();
}
@@ -190,4 +193,19 @@ public class DeviceMapClient {
}
}
+ private JsonNode get(JsonNode node, String name) throws Exception {
+ JsonNode ret = node.get(name);
+
+ if(ret != null) {
+ return ret;
+ }
+
+ if(nullNode == null) {
+ ObjectMapper mapper = new ObjectMapper();
+ nullNode = mapper.readTree("null");
+ }
+
+ return nullNode;
+ }
+
}