diff options
author | Rafael H. Schloming <rhs@apache.org> | 2008-04-16 13:32:13 +0000 |
---|---|---|
committer | Rafael H. Schloming <rhs@apache.org> | 2008-04-16 13:32:13 +0000 |
commit | bbe30c7875f67547ea9394d4e99b420ac22ef152 (patch) | |
tree | 3bd76d813efccb16ba86f49982e3f24725659174 /qpid/java/common/genutil.py | |
parent | 857ba162eba08e8c210492a0ea265a6bd4439467 (diff) | |
download | qpid-python-bbe30c7875f67547ea9394d4e99b420ac22ef152.tar.gz |
QPID-901: updates to the java client to use the 0-10 final spec instead of the 0-10 preview spec; this includes improvements to the codegen process as well as some modifications to the shared code path in the client to not lose per message state when consumers are closed.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk@648692 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/common/genutil.py')
-rw-r--r-- | qpid/java/common/genutil.py | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/qpid/java/common/genutil.py b/qpid/java/common/genutil.py new file mode 100644 index 0000000000..5206b50bbd --- /dev/null +++ b/qpid/java/common/genutil.py @@ -0,0 +1,207 @@ + +def camel(offset, *args): + parts = [] + for a in args: + parts.extend(a.split("-")) + return "".join(parts[:offset] + [p[0].upper() + p[1:] for p in parts[offset:]]) + +def dromedary(s): + return s[0].lower() + s[1:] + +def scream(*args): + return "_".join([a.replace("-", "_").upper() for a in args]) + +def num(x): + if x is not None and x != "": + return int(x, 0) + else: + return None + +def klass(nd): + parent = nd.parent + while parent is not None: + if hasattr(parent, "name") and parent.name == "class": + return parent + parent = parent.parent + +untyped = -1 + +def code(nd): + global untyped + cd = num(nd["@code"]) + if cd is None: + cd = untyped + untyped -= 1 + return cd + + cls = klass(nd) + if cls: + cd |= (num(cls["@code"]) << 8) + return cd + +def root(nd): + if nd.parent is None: + return nd + else: + return root(nd.parent) + +def qname(nd): + name = nd["@name"] + cls = klass(nd) + if cls != None: + return "%s.%s" % (cls["@name"], name) + else: + return name + +def resolve(node, name): + spec = root(node) + cls = klass(node) + if cls: + for nd in cls.query["#tag"]: + if nd["@name"] == name: + return nd + for nd in spec.query["amqp/#tag"] + spec.query["amqp/class/#tag"]: + if name == qname(nd): + return nd + raise Exception("unresolved name: %s" % name) + +def resolve_type(nd): + name = nd["@type"] + type = resolve(nd, name) + if type.name == "domain" and not type["enum"]: + return resolve_type(type) + else: + return type + +TYPES = { + "bit": "boolean", + "uint8": "short", + "uint16": "int", + "uint32": "long", + "uint64": "long", + "datetime": "long", + "uuid": "UUID", + "sequence-no": "int", + "sequence-set": "RangeSet", # XXX + "byte-ranges": "RangeSet", # XXX + "str8": "String", + "str16": "String", + "vbin8": "byte[]", + "vbin16": "byte[]", + "vbin32": "byte[]", + "struct32": "Struct", + "map": "Map<String,Object>", + "array": "List<Object>" + } + +def cname(nd, field="@name"): + cls = klass(nd) + if cls: + if (nd.name in ("struct", "result") and + cls["@name"] != "session" and + nd[field] != "header"): + return camel(0, nd[field]) + else: + return camel(0, cls["@name"], nd[field]) + else: + return camel(0, nd[field]) + +def jtype(nd): + if nd.name == "struct" or nd["enum"]: + return cname(nd) + else: + return TYPES[nd["@name"]] + +REFS = { + "boolean": "Boolean", + "byte": "Byte", + "short": "Short", + "int": "Integer", + "long": "Long", + "float": "Float", + "double": "Double", + "char": "Character" +} + +def jref(jt): + return REFS.get(jt, jt) + +def jclass(jt): + idx = jt.find('<') + if idx > 0: + return jt[:idx] + else: + return jt + +DEFAULTS = { + "long": 0, + "int": 0, + "short": 0, + "byte": 0, + "char": 0, + "boolean": "false" + } + +class Field: + + def __init__(self, index, nd): + self.index = index + self.name = camel(1, nd["@name"]) + type_node = resolve_type(nd) + tname = cname(type_node) + if type_node.name == "struct": + self.read = "(%s) dec.readStruct(%s.TYPE)" % (tname, tname) + self.write = "enc.writeStruct(%s.TYPE, check(struct).%s)" % (tname, self.name) + elif type_node.name == "domain": + coder = camel(0, resolve_type(type_node)["@name"]) + self.read = "%s.get(dec.read%s())" % (tname, coder) + self.write = "enc.write%s(check(struct).%s.getValue())" % (coder, self.name) + else: + coder = camel(0, type_node["@name"]) + self.read = "dec.read%s()" % coder + self.write = "enc.write%s(check(struct).%s)" % (coder, self.name) + self.type = jtype(type_node) + self.default = DEFAULTS.get(self.type, "null") + self.has = camel(1, "has", self.name) + self.get = camel(1, "get", self.name) + self.set = camel(1, "set", self.name) + self.clear = camel(1, "clear", self.name) + if self.type == "boolean": + self.option = scream(nd["@name"]) + else: + self.option = None + +def get_fields(nd): + fields = [] + index = 0 + for f in nd.query["field"]: + fields.append(Field(index, f)) + index += 1 + return fields + +def get_parameters(fields): + params = [] + options = False + for f in fields: + if f.option: + options = True + else: + params.append("%s %s" % (f.type, f.name)) + if options: + params.append("Option ... _options") + return params + +def get_arguments(fields): + args = [] + options = False + for f in fields: + if f.option: + options = True + else: + args.append(f.name) + if options: + args.append("_options") + return args + +def get_options(fields): + return [f for f in fields if f.option] |