Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: sushantk/siri
base: 430561127e
...
head fork: sushantk/siri
compare: 7903d4db71
  • 3 commits
  • 17 files changed
  • 0 commit comments
  • 1 contributor
View
BIN  external/jackson-core-lgpl-1.9.1.jar → external/jackson-core-lgpl-1.9.4.jar
Binary file not shown
View
BIN  external/jackson-mapper-lgpl-1.9.1.jar → external/jackson-mapper-lgpl-1.9.4.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
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
8 src/siri/IParams.java
@@ -0,0 +1,8 @@
+package siri;
+
+import java.util.Map;
+
+public interface IParams extends IConfigurable {
+
+ Map<String, Object> getMap(Context a_context);
+}
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
67 src/siri/ParamsDefault.java
@@ -0,0 +1,67 @@
+package siri;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@SiriObject(hasSetMethods=false)
+public class ParamsDefault extends Configurable
+ implements IParams {
+
+ static final Logger s_logger = LoggerFactory.getLogger(StringDefault.class);
+
+ ArrayList<IConfigurable> m_params;
+
+ public ParamsDefault() {
+ m_params = new ArrayList<IConfigurable>();
+ }
+
+ public void addParameter(IConfigurable a_param) {
+ m_params.add(a_param);
+ }
+
+ @Override
+ public Map<String, Object> getMap(Context a_context) {
+
+ TreeMap<String, Object> values = new TreeMap<String, Object>();
+
+ Iterator<IConfigurable> iter = m_params.iterator();
+ while(iter.hasNext()) {
+ IConfigurable param = iter.next();
+ if(param instanceof IString) {
+ IString istring = (IString)param;
+ String string = istring.get(a_context);
+ if(null == string) {
+ s_logger.error("{} - Failed to evaluate parameter: {} inside {}",
+ new Object[]{a_context, param, this});
+ return null;
+ }
+ values.put(param.getTag(), string);
+ } else if(param instanceof IParams) {
+ IParams iparams = (IParams)param;
+ Map<String, Object> childValues = iparams.getMap(a_context);
+ if(null == childValues) {
+ s_logger.error("{} - Failed to evaluate parameter: {} inside {}",
+ new Object[]{a_context, param, this});
+ return null;
+ }
+ values.put(param.getTag(), childValues);
+ }
+ else {
+ s_logger.error("{} - Invalid parameter type, string or map expected: {} inside {}",
+ new Object[]{a_context, param, this});
+ return null;
+ }
+ }
+
+ return values;
+ }
+
+ public String toDebugString() {
+ return m_params.toString();
+ }
+}
View
9 src/siri/SiriObject.java
@@ -0,0 +1,9 @@
+package siri;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface SiriObject {
+ boolean hasSetMethods() default false;
+}
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
View
87 src/siri/test/TestObjectFactory.java
@@ -0,0 +1,87 @@
+package siri.test;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import siri.Configurable;
+import siri.IConfigurable;
+import siri.IParams;
+import siri.IString;
+import siri.SiriParameter;
+import siri.ObjectFactory;
+import siri.Result;
+
+public class TestObjectFactory {
+
+ static final Logger s_logger = LoggerFactory.getLogger(TestObjectFactory.class);
+
+ @Test
+ public void testParse() {
+ ObjectFactory factory = new ObjectFactory();
+ assertNull(factory.parse(""));
+ assertNull(factory.parse("foo"));
+ assertNull(factory.parse("{-}"));
+ assertNotNull(factory.parse("{}"));
+ assertNotNull(factory.parse("{\"foo\" : \"bar\" }"));
+ String tree = "{\"n1\":\"v1\", \"n2\":[\"x1\", {\"@id\":\"i2\", \"x2\":\"y2\"}, [\"x31\", \"x32\"]], \"n3\":{\"m1\":\"n1\"} }";
+ assertNotNull(factory.parse(tree));
+ }
+
+ @Test
+ public void testBuild() {
+ ObjectFactory factory = new ObjectFactory();
+ ObjectBuilder builder = new ObjectBuilder();
+
+ assertTrue(Result.SUCCESS != factory.build(builder, factory.parse("{}")));
+ assertTrue(Result.SUCCESS != factory.build(builder, factory.parse("{\"Unknown\" : {}}")));
+ assertTrue(Result.SUCCESS != factory.build(builder, factory.parse("{\"Child\" : {}}")));
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Child\" : { \"Url\" : \"url\" }}")));
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Child\" : { \"@class\" : \"siri.test.TestObjectFactory$ChildObject2\" }}")));
+ }
+
+ @Test
+ public void testBuildParams() {
+ ObjectFactory factory = new ObjectFactory();
+ ParamsBuilder builder = new ParamsBuilder();
+
+ String params = "\"string\"";
+ assertTrue(Result.SUCCESS != factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ params = "[]";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ params = "[\"p1\", \"p2\"]";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ params = "{\"n1\" : \"v1\", \"n2\" : \"v2\"}";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ params = "{\"n1\" : \"v1\", \"n2\" : {\"x1\" : \"y1\"}}";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ params = "{\"n1\" : \"v1\", \"n2\" : [\"x1\", \"x2\"] }";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ }
+
+ public static class ObjectBuilder extends Configurable {
+ @SiriParameter(required=true, defaultClass="siri.test.TestObjectFactory$ChildObject")
+ public void setChild(IConfigurable a_child) {
+ }
+ }
+
+ public static class ParamsBuilder extends Configurable {
+ @SiriParameter(required=true)
+ public void setParams(IParams a_params) {
+ s_logger.debug("Params: {}", a_params.toDebugString());
+ }
+ }
+
+ public static class ChildObject extends Configurable {
+ @SiriParameter(required=true, defaultClass="siri.StringDefault")
+ public void setUrl(IString a_string) {
+ }
+ }
+
+ public static class ChildObject2 extends Configurable {
+ public void setUrl(IString a_string) {
+ }
+ }
+}
View
70 src/siri/test/TestParamsDefault.java
@@ -0,0 +1,70 @@
+package siri.test;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Map;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import siri.Configurable;
+import siri.Context;
+import siri.IParams;
+import siri.ObjectFactory;
+import siri.RequestContext;
+import siri.Result;
+import siri.SiriParameter;
+
+public class TestParamsDefault {
+
+ static final Logger s_logger = LoggerFactory.getLogger(TestParamsDefault.class);
+
+ @Test
+ public void testParse() {
+ ObjectFactory factory = new ObjectFactory();
+ ParamsBuilder builder = new ParamsBuilder();
+ Context context = new Context(new RequestContext(), "", "");
+
+ Map<String, Object> map;
+ String params = "[]";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ map = builder.getParamsMap(context);
+ assertTrue("Empty params", map.isEmpty());
+ params = "[\"p1\", \"p2\"]";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ map = builder.getParamsMap(context);
+ assertTrue("Array params", (2 == map.size()));
+ params = "{\"n1\" : \"v1\", \"n2\" : {\"x1\" : \"y1\"}}";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ map = builder.getParamsMap(context);
+ assertTrue("Map params", (2 == map.size()));
+ Map<String, Object> childMap = (Map<String, Object>)map.get("n2");
+ assertTrue("Child map params", (1 == childMap.size()));
+ assertTrue("Child map params", (childMap.get("x1").toString().equals("y1")));
+ params = "{\"n1\" : \"v1\", \"n2\" : [\"x1\", \"x2\"] }";
+ assertTrue(Result.SUCCESS == factory.build(builder, factory.parse("{\"Params\" : " + params + " }")));
+ map = builder.getParamsMap(context);
+ assertTrue("Map params", (2 == map.size()));
+ childMap = (Map<String, Object>)map.get("n2");
+ assertTrue("Child map params", (2 == childMap.size()));
+ }
+
+ public static class ParamsBuilder extends Configurable {
+
+ IParams m_params;
+
+ @SiriParameter(required=true)
+ public void setParams(IParams a_params) {
+ m_params = a_params;
+ }
+
+ public Map<String, Object> getParamsMap(Context a_context) {
+ Map<String, Object> map = m_params.getMap(a_context);
+ if(null != map) {
+ s_logger.debug("Map: {}", map);
+ }
+ return map;
+ }
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.