def get_java_source(self): type = self.get_type() value = self.get_value() if type == 'bool': if value: return 'true' else: return 'false' elif type == 'char': return "'{0}'".format(value) elif type == 'string': return '"{0}"'.format(value) elif ':bitmask:' in type: value = common.make_c_like_bitmask(value) cast = java_common.get_java_type(type.split(':')[0]) if cast in ['byte', 'short']: return '({0})({1})'.format(cast, value) else: return value elif type.endswith(':constant'): return self.get_value_constant().get_java_source() else: cast = java_common.get_java_type(type) if cast in ['byte', 'short']: cast = '({0})'.format(cast) else: cast = '' return cast + str(value)
def helper(value): if type_ == 'bool': if value: return 'true' else: return 'false' elif type_ == 'char': return "'{0}'".format(value) elif type_ == 'string': return '"{0}"'.format(value) elif ':bitmask:' in type_: value = common.make_c_like_bitmask(value) cast = java_common.get_java_type( type_.split(':')[0], 1, legacy=self.get_device().has_java_legacy_types()) if cast in ['byte', 'short']: return '({0})({1})'.format(cast, value) else: return value elif type_.endswith(':constant'): return self.get_value_constant(value).get_java_source() else: cast = java_common.get_java_type( type_, 1, legacy=self.get_device().has_java_legacy_types()) if cast in ['byte', 'short']: cast = '({0})'.format(cast) else: cast = '' return cast + str(value)
def get_java_constants(self): constant = "\tpublic final static {0} {1}_{2} = {3}{4};\n" constants = [] for constant_group in self.get_constant_groups(): typ = java_common.get_java_type(constant_group.get_type()) for constant_item in constant_group.get_items(): if constant_group.get_type() == "char": cast = "" if self.get_generator().is_octave(): value = "new String(new char[]{{'{0}'}})".format(constant_item.get_value()) typ = "String" else: value = "'{0}'".format(constant_item.get_value()) else: if typ == "int": cast = "" # no need to cast int, its the default type for number literals else: cast = "({0})".format(typ) value = str(constant_item.get_value()) if typ == "long": cast = "" value += "L" # mark longs as such, because int is the default type for number literals constants.append( constant.format( typ, constant_group.get_upper_case_name(), constant_item.get_upper_case_name(), cast, value ) ) return "\n" + "".join(constants)
def get_java_constants(self): constant = '\tpublic final static {0} {1}_{2} = {3}{4};\n' constants = [] for constant_group in self.get_constant_groups(): typ = java_common.get_java_type(constant_group.get_type()) for constant_item in constant_group.get_items(): if constant_group.get_type() == 'char': cast = '' if self.get_generator().is_octave(): value = "new String(new char[]{{'{0}'}})".format( constant_item.get_value()) typ = 'String' else: value = "'{0}'".format(constant_item.get_value()) else: if typ == 'int': cast = '' else: cast = '({0})'.format(typ) value = str(constant_item.get_value()) constants.append( constant.format(typ, constant_group.get_upper_case_name(), constant_item.get_upper_case_name(), cast, value)) return '\n' + ''.join(constants)
def get_java_constants(self): constant = '\tpublic final static {0} {1}_{2} = {3}{4};\n' constants = [] for constant_group in self.get_constant_groups(): typ = java_common.get_java_type(constant_group.get_type()) for constant_item in constant_group.get_items(): if constant_group.get_type() == 'char': cast = '' if self.get_generator().is_octave(): value = "new String(new char[]{{'{0}'}})".format(constant_item.get_value()) typ = 'String' else: value = "'{0}'".format(constant_item.get_value()) else: if typ == 'int': cast = '' # no need to cast int, its the default type for number literals else: cast = '({0})'.format(typ) value = str(constant_item.get_value()) if typ == 'long': cast = '' value += 'L' # mark longs as such, because int is the default type for number literals constants.append(constant.format(typ, constant_group.get_upper_case_name(), constant_item.get_upper_case_name(), cast, value)) return '\n' + ''.join(constants)
def make_object_desc(packet): if len(packet.get_elements('out')) < 2: return '' desc = { 'en': """ The returned object has the public member variables {0}. """, 'de': """ Das zurückgegebene Objekt enthält die Public Member Variablen {0}. """ } var = [] for element in packet.get_elements('out'): var.append('``{0} {1}``'.format(java_common.get_java_type(element[1]), common.underscore_to_headless_camel_case(element[0]))) if len(var) == 1: return common.select_lang(desc).format(var[0]) if len(var) == 2: return common.select_lang(desc).format(var[0] + ' and ' + var[1]) return common.select_lang(desc).format(', '.join(var[:-1]) + ' and ' + var[-1])
def get_java_constants(self): template = '\tpublic final static {0} {1}_{2} = {3}{4};\n' constants = [] for constant_group in self.get_constant_groups(): typ = java_common.get_java_type(constant_group.get_type()) for constant in constant_group.get_constants(): if constant_group.get_type() == 'char': cast = '' if self.get_generator().is_octave(): value = "new String(new char[]{{'{0}'}})".format(constant.get_value()) typ = 'String' else: value = "'{0}'".format(constant.get_value()) else: if typ == 'int': cast = '' # no need to cast int, its the default type for number literals else: cast = '({0})'.format(typ) value = str(constant.get_value()) if typ == 'long': cast = '' value += 'L' # mark longs as such, because int is the default type for number literals constants.append(template.format(typ, constant_group.get_upper_case_name(), constant.get_upper_case_name(), cast, value)) return '\n' + ''.join(constants)
def get_java_variable(self): template = '{type} {headless_camel_case_name}' headless_camel_case_name = self.get_headless_camel_case_name() if headless_camel_case_name == self.get_device().get_initial_name(): headless_camel_case_name += '_' return template.format(type=java_common.get_java_type(self.get_type().split(':')[0]), headless_camel_case_name=headless_camel_case_name)
def get_java_variable(self): template = '{type} {headless_camel_case_name}' headless_camel_case_name = self.get_headless_camel_case_name() if headless_camel_case_name == self.get_device().get_initial_name(): headless_camel_case_name += '_' return template.format( type=java_common.get_java_type(self.get_type().split(':')[0]), headless_camel_case_name=headless_camel_case_name)
def make_constants(): str_constants = '\n' str_constant = '\tpublic final static {0} {1}_{2} = {3}{4};\n' constants = device.get_constants() for constant in constants: for definition in constant.definitions: if constant.type == 'char': cast = '' value = "'{0}'".format(definition.value) else: cast = '({0})'.format(java_common.get_java_type(constant.type)) value = str(definition.value) str_constants += str_constant.format(java_common.get_java_type(constant.type), constant.name_uppercase, definition.name_uppercase, cast, value) return str_constants
def get_java_variable(self): template = '{type_} {name}' name = self.get_name().headless if name == self.get_device().get_initial_name(): name += '_' return template.format(type_=java_common.get_java_type( self.get_type().split(':')[0], self.get_cardinality(), legacy=self.get_device().has_java_legacy_types()), name=name)
def get_java_source(self): templateA = '{type_} {name}' templateB = '{type_}[] {name}' if self.get_cardinality() == 1: template = templateA else: template = templateB return template.format(type_=java_common.get_java_type( self.get_type().split(':')[0], 1, legacy=self.get_device().has_java_legacy_types()), name=self.get_name().headless)
def get_java_source(self): type_ = self.get_type() def helper(value): if type_ == 'bool': if value: return 'true' else: return 'false' elif type_ == 'char': return "'{0}'".format(value) elif type_ == 'string': return '"{0}"'.format(value) elif ':bitmask:' in type_: value = common.make_c_like_bitmask(value) cast = java_common.get_java_type( type_.split(':')[0], 1, legacy=self.get_device().has_java_legacy_types()) if cast in ['byte', 'short']: return '({0})({1})'.format(cast, value) else: return value elif type_.endswith(':constant'): return self.get_value_constant(value).get_java_source() else: cast = java_common.get_java_type( type_, 1, legacy=self.get_device().has_java_legacy_types()) if cast in ['byte', 'short']: cast = '({0})'.format(cast) else: cast = '' return cast + str(value) value = self.get_value() if isinstance(value, list): return 'new {0}[]{{{1}}}'.format( java_common.get_java_type( self.get_type().split(':')[0], 1, legacy=self.get_device().has_java_legacy_types()), ', '.join([helper(item) for item in value])) return helper(value)
def make_return_objects(): objs = '' obj = """ \tpublic class {0} {{ {1} \t\tpublic String toString() {{ \t\t\treturn "[" + {2} "]"; \t\t}} \t}} """ param = '\t\tpublic {0}{1} {2}{3};' for packet in device.get_packets('function'): if packet.has_prototype_in_device(): continue if len(packet.get_elements('out')) < 2: continue name = java_common.get_object_name(packet) params = [] tostr = [] for element in packet.get_elements(): typ = java_common.get_java_type(element[1]) ele_name = common.underscore_to_headless_camel_case(element[0]) if element[2] > 1 and element[1] != 'string': arr = '[]' new = ' = new {0}[{1}]'.format(typ, element[2]) to = '"{0} = " + Arrays.toString({0}) +'.format(ele_name) else: arr = '' new = '' to = '"{0} = " + {0} +'.format(ele_name) tostr.append(to) params.append(param.format(typ, arr, ele_name, new)) objs += obj.format(name, '\n'.join(params), ' ", " + '.join(tostr)) return objs
def get_java_source(self): template = '{minimum}, {maximum}' minimum = self.get_formatted_minimum() maximum = self.get_formatted_maximum() cast = java_common.get_java_type(self.get_type()) if cast in ['byte', 'short']: cast = '({0})'.format(cast) if minimum.isdigit(): minimum = cast + minimum else: minimum = '{0}({1})'.format(cast, minimum) if maximum.isdigit(): maximum = cast + maximum else: maximum = '{0}({1})'.format(cast, maximum) return template.format(minimum=minimum, maximum=maximum)
def get_java_source(self): template = '{minimum},<BP>{maximum}' minimum = self.get_formatted_minimum() maximum = self.get_formatted_maximum() cast = java_common.get_java_type(self.get_type()) if cast in ['byte', 'short']: cast = '({0})'.format(cast) if minimum.isdigit(): minimum = cast + minimum else: minimum = '{0}({1})'.format(cast, minimum) if maximum.isdigit(): maximum = cast + maximum else: maximum = '{0}({1})'.format(cast, maximum) return template.format(minimum=minimum, maximum=maximum)
def get_java_source(self): template = '{type} {headless_camel_case_name}' return template.format( type=java_common.get_java_type(self.get_type().split(':')[0]), headless_camel_case_name=self.get_headless_camel_case_name())
def make_bbgets(packet, with_obj = False): bbgets = '' bbget_other = '\t\t{0}{1}{2} = {3}(bb.get{4}(){5}){6};' bbget_string = '\t\t{0}{1}{2} = {3}(bb{4}{5}){6};' new_arr ='{0}[] {1} = new {0}[{2}];' loop = """\t\t{2}for(int i = 0; i < {0}; i++) {{ {1} \t\t}} """ for element in packet.get_elements('out'): typ = '' if not with_obj: typ = java_common.get_java_type(element[1]) + ' ' bbret = common.underscore_to_headless_camel_case(element[0]) obj = '' if with_obj: obj = 'obj.' cast = '' cast_extra = '' boolean = '' if element[1] == 'uint8': cast = 'IPConnection.unsignedByte' elif element[1] == 'uint16': cast = 'IPConnection.unsignedShort' elif element[1] == 'uint32': cast = 'IPConnection.unsignedInt' elif element[1] == 'bool': boolean = ' != 0' elif element[1] == 'char': cast = '(char)' elif element[1] == 'string': cast = 'IPConnection.string' cast_extra = ', {0}'.format(element[2]) format_typ = '' if not element[2] > 1 or (element[1] == 'string' and not with_obj): format_typ = typ if element[1] == 'string': bbget = bbget_string else: bbget = bbget_other bbget_format = bbget.format(format_typ, obj, bbret, cast, get_put_type(element[1]), cast_extra, boolean) if element[2] > 1 and element[1] != 'string': if with_obj: bbget_format = bbget_format.replace(' =', '[i] =') bbget_format = loop.format(element[2], '\t' + bbget_format, '') else: arr = new_arr.format(typ.replace(' ', ''), bbret, element[2]) bbget_format = bbget_format.replace(' =', '[i] =') bbget_format = loop.format(element[2], '\t' + bbget_format, arr + '\n\t\t') bbgets += bbget_format + '\n' return bbgets, bbret
def make_methods(): methods = '' method = """ \t/** \t * {8} \t */ \tpublic {0} {1}({2}) {3} {{ \t\tByteBuffer bb = ipcon.createRequestPacket((byte){4}, FUNCTION_{5}, this); {6} {7} \t}} """ method_response = """\t\tbyte[] response = sendRequest(bb.array()); \t\tbb = ByteBuffer.wrap(response, 8, response.length - 8); \t\tbb.order(ByteOrder.LITTLE_ENDIAN); {1} \t\treturn {2};""" method_noresponse = """\t\tsendRequest(bb.array());""" loop = """\t\tfor(int i = 0; i < {0}; i++) {{ {1} \t\t}} """ string_loop = """\t\ttry {{ \t\t{0} \t\t\t}} catch(Exception e) {{ \t\t\t\tbb.put((byte)0); \t\t\t}}""" cls = device.get_camel_case_name() for packet in device.get_packets('function'): options = 0 ret = java_common.get_return_type(packet) name_lower = packet.get_headless_camel_case_name() parameter = java_common.make_parameter_list(packet) size = str(packet.get_request_length()) name_upper = packet.get_upper_case_name() doc = format_doc(packet) bbputs = '' bbput = '\t\tbb.put{0}({1}{2});' for element in packet.get_elements('in'): name = common.underscore_to_headless_camel_case(element[0]) if element[1] == 'bool': name = '({0} ? 1 : 0)'.format(name) cast = '' put_java_type = get_put_java_type(element[1]) if put_java_type != java_common.get_java_type(element[1]): cast = '({0})'.format(put_java_type) bbput_format = bbput.format(get_put_type(element[1]), cast, name) if element[2] > 1: if element[1] == 'string': bbput_format = bbput_format.replace(');', '.charAt(i));') bbput_format = string_loop.format(bbput_format) else: bbput_format = bbput_format.replace(');', '[i]);') bbput_format = loop.format(element[2], '\t' + bbput_format) bbputs += bbput_format + '\n' throw = 'throws TimeoutException, NotConnectedException' if len(packet.get_elements('out')) == 0: bbgets = '' bbret = '' elif len(packet.get_elements('out')) > 1: bbgets, bbret = make_bbgets(packet, True) obj_name = java_common.get_object_name(packet) obj = '\t\t{0} obj = new {0}();\n'.format(obj_name) bbgets = obj + bbgets bbret = 'obj' else: bbgets, bbret = make_bbgets(packet, False) if len(packet.get_elements('out')) == 0: response = method_noresponse.format(name_upper) else: response = method_response.format(name_upper, bbgets, bbret) methods += method.format(ret, name_lower, parameter, throw, size, name_upper, bbputs, response, doc) return methods
def get_java_source(self): template = '{type} {headless_camel_case_name}' return template.format(type=java_common.get_java_type(self.get_type().split(':')[0]), headless_camel_case_name=self.get_headless_camel_case_name())