Skip to content
Browse files

Params and object factory normalization.

  • Loading branch information...
1 parent 415ad97 commit 0c7741dbea76e06c3cf93cd2be1c3de816044799 @sushantk committed
View
BIN external/commons-codec-1.4.jar
Binary file not shown.
View
BIN external/commons-logging-1.1.1.jar
Binary file not shown.
View
BIN external/httpasyncclient-4.0-alpha3.jar
Binary file not shown.
View
BIN external/httpclient-4.1.2.jar
Binary file not shown.
View
BIN external/httpcore-4.2-alpha2.jar
Binary file not shown.
View
BIN external/httpcore-nio-4.2-alpha2.jar
Binary file not shown.
View
BIN external/jackson-core-lgpl-1.9.1.jar
Binary file not shown.
View
BIN external/jackson-mapper-lgpl-1.9.1.jar
Binary file not shown.
View
BIN external/logback-classic-0.9.30.jar
Binary file not shown.
View
BIN external/logback-core-0.9.30.jar
Binary file not shown.
View
17 external/logback.xml
@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<configuration>
-
- <appender name="STDOUT"
- class="ch.qos.logback.core.ConsoleAppender">
- <!-- encoders are assigned by default the type
- ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
- <encoder>
- <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
- </encoder>
- </appender>
-
- <root level="trace">
- <appender-ref ref="STDOUT" />
- </root>
-</configuration>
-
View
BIN external/slf4j-api-1.6.3.jar
Binary file not shown.
View
8 src/siri/Configurable.java
@@ -13,4 +13,12 @@ public Configurable() {
public void setTag(String a_tagName) { m_tagName = a_tagName; }
public void setId(String a_id) { m_id = a_id; }
+
+ public String toDebugString() {
+ return this.toString();
+ }
+
+ public String toString() {
+ return m_tagName + "<" + this.getClass().getName() + ", " + m_id + ">";
+ }
}
View
14 src/siri/Consts.java
@@ -12,15 +12,19 @@
// attributes
public static final String id = "@id";
- public static final String _class = "@class";
+ public static final String _class = "@class";
+ public static final String Value = "Value";
public static final String renderer_id = "renderer_id";
public static final String _default = "default";
// other
- public static final String defaultClass = "siri.StringDefault";
- public static String comma = ",";
+ public static final String defaultAddMethod = "addParameter";
+ public static final String defaultSetValueMethod = "setValue";
+ public static final String defaultStringClass = "siri.StringDefault";
+ public static final String defaultParamsClass = "siri.ParamsDefault";
+ public static final boolean defaultRequired = false;
+ public static final boolean defaultList = false;
+ public static final String comma = ",";
-
-
}
View
4 src/siri/Context.java
@@ -9,7 +9,7 @@
private boolean m_logging;
- Context(RequestContext a_requestContext, String a_iid, String a_moduleId) {
+ public Context(RequestContext a_requestContext, String a_iid, String a_moduleId) {
m_requestContext = a_requestContext;
m_iid = a_iid;
@@ -47,7 +47,7 @@ public String toString() {
return m_string;
}
- m_string = "Context[ " + m_moduleId + " ][ " + m_iid + " ]";
+ m_string = "Context<" + m_moduleId + ", " + m_iid + ">";
return m_string;
}
View
3 src/siri/IConfigurable.java
@@ -7,5 +7,6 @@
void setTag(String a_tagName);
void setId(String a_id);
-
+
+ String toDebugString();
}
View
167 src/siri/ObjectFactory.java
@@ -3,6 +3,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -17,8 +18,10 @@
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.MappingJsonFactory;
+import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;
+import org.codehaus.jackson.node.ValueNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -28,6 +31,8 @@
static JsonFactory s_jsonMapper = new MappingJsonFactory();
static JsonNodeFactory s_jsonFactory = JsonNodeFactory.instance;
+ int m_nextId = 0;
+
public ObjectTree parse(String a_json) {
JsonNode node = null;
try {
@@ -42,69 +47,146 @@ public ObjectTree parse(String a_json) {
return null;
}
- return new ObjectTree(node);
+ this.normalize((ObjectNode)node);
+ s_logger.debug("Normalized tree: ");
+ s_logger.debug(node.toString());
+ return new ObjectTree((ObjectNode)node);
}
public Result build(IConfigurable a_object, ObjectTree a_tree) {
Stack<String> tagStack = new Stack<String>();
- tagStack.push(a_object.getTag() + "<" + a_object.getId() + ">");
+ tagStack.push(a_object.toString());
try {
this.build(a_object, a_tree.getNode(), tagStack);
return Result.SUCCESS;
} catch(Exception ex) {
- String context = Util.implode(tagStack, ":");
+ String context = Util.implode(tagStack, "=>");
s_logger.error("Failed to build: {}", context);
s_logger.error("Reason: ", ex);
}
return Result.INVALID_OBJECT_TREE;
}
+
+ private void normalize(ObjectNode a_node) {
+ Iterator<Map.Entry<String, JsonNode>> it = a_node.getFields();
+ while(it.hasNext()) {
+ Entry<String, JsonNode> entry = it.next();
+ String tagName = entry.getKey();
+ if(tagName.startsWith("@")) {
+ continue; // attributes
+ }
+
+ JsonNode childNode = entry.getValue();
+ if(childNode.isObject()) {
+ this.normalize((ObjectNode)childNode);
+ } else if(childNode.isArray()) {
+ ObjectNode arrayNode = this.normalizeArray((ArrayNode)childNode);
+ a_node.put(tagName, arrayNode);
+ } else {
+ ObjectNode stringNode = this.normalizeValue((ValueNode)childNode);
+ a_node.put(tagName, stringNode);
+ }
+ }
+ }
+
+ private ObjectNode normalizeArray(ArrayNode a_node) {
+ ObjectNode arrayNode = s_jsonFactory.objectNode();
+
+ Iterator<JsonNode> arrayit = a_node.iterator();
+ while(arrayit.hasNext()) {
+ JsonNode childNode = arrayit.next();
+
+ ObjectNode objChildNode = null;
+ String id = null;
+ if(childNode.isObject()) {
+ objChildNode = (ObjectNode)childNode;
+ this.normalize(objChildNode);
+ } else if(childNode.isArray()) {
+ objChildNode = this.normalizeArray((ArrayNode)childNode);
+ } else {
+ objChildNode = this.normalizeValue((ValueNode)childNode);
+ }
+
+ id = this.getId(objChildNode);
+ arrayNode.put(id, objChildNode);
+ }
+
+ return arrayNode;
+ }
+
+ private ObjectNode normalizeValue(ValueNode a_node) {
+ ObjectNode stringNode = s_jsonFactory.objectNode();
+
+ String textValue = a_node.asText();
+ stringNode.put(Consts.Value, textValue);
+ stringNode.put(Consts._class, Consts.defaultStringClass);
+
+ return stringNode;
+ }
- private void build(IConfigurable a_object, JsonNode a_node, Stack<String> a_tagStack) throws Exception {
+ private void build(IConfigurable a_object, ObjectNode a_node, Stack<String> a_tagStack) throws Exception {
Class<?> objClass = a_object.getClass();
+ Method objectSetMethod = null;
+ SiriObject siriObject = objClass.getAnnotation(SiriObject.class);
+ if((null != siriObject) && !siriObject.hasSetMethods()) {
+ objectSetMethod = this.findSetMethod(objClass, Consts.defaultAddMethod);
+ }
+
Set<String> setMethods = new TreeSet<String>();
Iterator<Map.Entry<String, JsonNode>> it = a_node.getFields();
while(it.hasNext()) {
Entry<String, JsonNode> entry = it.next();
String tagName = entry.getKey();
- if(tagName.startsWith("@")) {
- // attributes
- continue;
- }
-
- JsonNode childNode = entry.getValue();
+ if(tagName.startsWith("@")) continue; // attributes
s_logger.debug("Building object: {}", tagName);
-
- String setMethodName = "set" + tagName;
- Method setMethod = this.findSetMethod(objClass, setMethodName);
+ JsonNode childNode = entry.getValue();
+
+ boolean isList = false;
+ String defaultClassName = Consts.defaultParamsClass;
+
+ Method setMethod = objectSetMethod;
if(null == setMethod) {
- // ignored to have tags that may be used by tools
- s_logger.info("Ignored tag: {}", tagName);
- continue;
+ String setMethodName = "set" + tagName;
+ setMethod = this.findSetMethod(objClass, setMethodName);
+ if(null == setMethod) {
+ // ignored to have tags that may be used by tools
+ s_logger.info("Ignored tag: {}", tagName);
+ continue;
+ }
+
+ SiriParameter parameter = setMethod.getAnnotation(SiriParameter.class);
+ if(null != parameter) {
+ defaultClassName = parameter.defaultClass();
+ isList = parameter.list();
+ }
+
+ setMethods.add(setMethodName);
}
- setMethods.add(setMethodName);
- String defaultClassName = Consts.defaultClass;
- SiriParameter parameter = setMethod.getAnnotation(SiriParameter.class);
- if(null != parameter) {
- defaultClassName = parameter.defaultClass();
- }
-
- if(childNode.isArray()) {
- Map<String, IConfigurable> childMap = new TreeMap<String, IConfigurable>();
- Iterator<JsonNode> arrayit = childNode.iterator();
- while(arrayit.hasNext()) {
- JsonNode arrayNode = arrayit.next();
- IConfigurable arrayChildObject = this.build(tagName, arrayNode, defaultClassName, a_tagStack);
- childMap.put(arrayChildObject.getId(), arrayChildObject);
+ if(childNode.isValueNode()) {
+ setMethod.invoke(a_object, childNode.asText());
+ } else if(isList) {
+ List<IConfigurable> childList = new ArrayList<IConfigurable>();
+
+ Iterator<Map.Entry<String, JsonNode>> listit = childNode.getFields();
+ while(listit.hasNext()) {
+ Entry<String, JsonNode> listEntry = listit.next();
+ String listEntryId = listEntry.getKey();
+ ObjectNode listChildNode = (ObjectNode)listEntry.getValue(); // its normalized
+
+ IConfigurable listChildObject = this.build(tagName, listChildNode, defaultClassName, a_tagStack);
+ listChildObject.setId(listEntryId); // tag is the id in this case
+ childList.add(listChildObject);
}
- setMethod.invoke(a_object, childMap);
+
+ setMethod.invoke(a_object, childList);
} else {
- IConfigurable childObject = this.build(tagName, childNode, defaultClassName, a_tagStack);
+ IConfigurable childObject = this.build(tagName, (ObjectNode)childNode, defaultClassName, a_tagStack);
setMethod.invoke(a_object, childObject);
}
}
@@ -116,26 +198,20 @@ private void build(IConfigurable a_object, JsonNode a_node, Stack<String> a_tagS
}
}
- private IConfigurable build(String a_tagName, JsonNode a_node, String a_defaultClassName, Stack<String> a_tagStack) throws Exception {
- String id = ObjectFactory.getId(a_node);
- a_tagStack.push(a_tagName + "<" + id + ">");
+ private IConfigurable build(String a_tagName, ObjectNode a_node, String a_defaultClassName, Stack<String> a_tagStack) throws Exception {
IConfigurable childObject = ObjectFactory.createObject(a_node, a_defaultClassName);
childObject.setTag(a_tagName);
- childObject.setId(id);
+ childObject.setId(this.getId(a_node));
- if(a_node.isObject()) {
- this.build(childObject, a_node, a_tagStack);
- } else {
- Method valueMethod = childObject.getClass().getMethod("setValue", String.class);
- valueMethod.invoke(childObject, a_node.asText());
- }
+ a_tagStack.push(childObject.toString());
+ this.build(childObject, a_node, a_tagStack);
a_tagStack.pop();
return childObject;
}
-
- private static String getId(JsonNode a_node) {
+
+ private String getId(ObjectNode a_node) {
String id = "";
JsonNode idNode = a_node.get(Consts.id);
if(null != idNode) {
@@ -143,7 +219,8 @@ private static String getId(JsonNode a_node) {
}
if(id.isEmpty()) {
- // id.format();
+ id = String.format("_auto_%d", m_nextId++);
+ a_node.put(Consts.id, id);
}
return id;
View
8 src/siri/ObjectTree.java
@@ -1,16 +1,16 @@
package siri;
-import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.node.ObjectNode;
public final class ObjectTree {
- JsonNode m_node;
+ ObjectNode m_node;
- public ObjectTree(JsonNode a_node) {
+ public ObjectTree(ObjectNode a_node) {
m_node = a_node;
}
- JsonNode getNode() {
+ ObjectNode getNode() {
return m_node;
}
}
View
5 src/siri/SiriParameter.java
@@ -6,6 +6,7 @@
@Target(ElementType.METHOD)
public @interface SiriParameter {
- String defaultClass() default Consts.defaultClass;
- boolean required() default false;
+ String defaultClass() default Consts.defaultParamsClass;
+ boolean required() default Consts.defaultRequired;
+ boolean list() default Consts.defaultList; // if true, this method expects list of parameters as a pure "List" rather than IParams
}
View
3 src/siri/SourceWebService.java
@@ -39,6 +39,9 @@ public void setUrl(IString a_url) {
m_iurl = a_url;
}
+ public void setHeaders(IConfigurable a_params) {
+ }
+
@Override
public Result get(Context a_context, IRequestCallback a_callback) {
View
3 src/siri/StringDefault.java
@@ -13,9 +13,10 @@
public StringDefault() {
}
+ @SiriParameter(required=true)
public void setValue(String a_value) {
m_value = a_value;
- // see if this needs evaluation or is a literal
+ //TODO: check if this needs evaluation or is a literal
}
@Override

0 comments on commit 0c7741d

Please sign in to comment.
Something went wrong with that request. Please try again.