def __repr__(self, interface): """ Used to print out the constructor. :param interface: Boolean telling whether the constructor is in an interface or not :return: string that can be printed to a file """ # TODO consider checking what is in the parent class's constructor and call super on those parameters, # then do this. on the rest, adding the field as necessary if self.parameters: for parameter in self.parameters: self.body += "\t\tthis." + parameter[0] + " = " + parameter[ 0] + ";\n" else: self.body = "" if self.comments and self.parameters: header = str(self.comments) + ReverseDoc.parameter_print( self.parameters) + "\n\t */\n" elif self.comments: header = str(self.comments) + "\n\t */\n" else: header = "" if interface: return header + "\t" + self.sig + ";" return header + "\t" + self.sig + " {" \ + "\n" + "\t\t//TODO Check for accuracy\n" + self.body + "\n\t} \n\n"
def find_constructor(soup, fields): """ Finds the constructor and other necessary items for the constructor method :param soup: HTML of the class :return: constructor instance """ constructor = soup.find(text=re.compile("CONSTRUCTOR\sDETAIL")) if constructor: constructor = constructor.findNext("ul") new_constructor = Constructor() new_constructor.sig = " ".join( str(constructor.find("pre").text).replace("\n", "").split()) if str(new_constructor.sig).find("(") - str(new_constructor.sig).find(")") != -1 and \ constructor.find("div", {"class": "block"}): new_constructor.comments = ReverseDoc.create_comment( str(constructor.find("div", { "class": "block" }).text), True) constructor_parameters = constructor.find("span", {"class": "paramLabel"}, recursive=True) if constructor_parameters: parameters_list = list() constructor_parameters = constructor_parameters.parent.parent # Move up two levels so dd flag can be seen for parameter in constructor_parameters.find_all("dd"): if parameter.find("code") is not None: parameters_list.append([ parameter.text.split("-", 1)[0].strip(), parameter.text.split("-", 1)[1].strip() ]) new_constructor.parameters = parameters_list else: return None check_fields(new_constructor, fields) return new_constructor
def find_methods_details(methods_list, soup): """ method find_methods_details Adds comments from the method details area of the javadocs to the methods Arguments: methods_list - list of all of the methods """ for method in methods_list: method_details = soup.find("a", {"name": re.compile(method.name.split("(")[0])}) method_details = method_details.findNext("ul") comment = method_details.find("div", {"class": "block"}) if comment: method.comments = ReverseDoc.create_comment(str(comment.text), True) method_parameters = method_details.find("span", {"class": "paramLabel"}) if method_parameters: parameter = method_parameters.parent.next_sibling.next_sibling parameters_list = list() while str(parameter).find("Returns:") == -1 and parameter: parameters_list.append([parameter.text.split("-", 1)[0].strip(), parameter.text.split("-", 1)[1].strip()]) parameter = parameter.next_sibling.next_sibling method.parameters = parameters_list method_returns = method_details.find("span", {"class": "returnLabel"}) if method_returns and method.return_type.find("void") == -1: method.returns = method_returns.findNext("dd").text override = method_details.find("span", {"class": "overrideSpecifyLabel"}) if override and str(override.text) == "Overrides:": method.overrides = True
def __repr__(self, interface): if self.parameters: for parameter in self.parameters: self.body.append("\t\tthis." + parameter[0] + " = " + parameter[0] + ";\n") else: self.body = "" if self.comments and self.parameters: header = str(self.comments) + ReverseDoc.parameter_print(self.parameters) + "\n\t */\n" elif self.comments: header = str(self.comments) + "\n\t */\n" else: header = "" if interface: return header + "\t" + self.sig + ";" return header + "\t" + self.sig + " {" \ + "\n" + "\t\t//TODO Check for accuracy\n" + ReverseDoc.str_list(self.body, interface) + "\n\t} \n\n"
def find_class_name(soup): """ method find_class_name finds a returns the name of the class on the page """ my_class = ClassName() my_class.title = str(soup.find("pre").text).replace("\n", " ") if soup.find("div", {"class": "description"}).find("div", {"class": "block"}): my_class.comments = ReverseDoc.create_comment(str(soup.find("div", {"class": "block"}).text), False) return my_class
def find_fields_details(fields_list, soup): for field in fields_list: field_details = soup.find("a", {"name": field.name}) field.name = str(field_details.findNext("pre").text).replace( u'\u00a0', " ") print(field.name) if field_details.findNext("div", {"class": "block"}): field.comments = ReverseDoc.create_comment( str(field_details.findNext("div", { "class": "block" }).text), True)
def find_class_name(soup): """ method find_class_name finds a returns the name of the class on the page :param soup: """ my_class = ClassName() my_class.title = str(soup.find("pre").text).replace("\n", " ") class_comments = soup.find("div", {"class": "description"}).find("div", {"class": "block"}) if class_comments: my_class.comments = ReverseDoc.create_comment(str(class_comments.text), False) return my_class
def __repr__(self, interface): """ method __repr__(self) Returns the method text in this format: /** * self.comments */ public self.return_type self.name { //Body } """ header = "" if self.comments: header += str(self.comments) if self.parameters: header += str(ReverseDoc.parameter_print(self.parameters)) if self.returns: self.returns = " ".join( str(self.returns).replace("\n", "").split()) self.returns = "\n\t * @return " + str(self.returns) header += self.returns if self.comments or self.parameters or self.returns: header += "\n\t */\n" if self.return_type.find("private") == -1 and self.return_type.find( "protected") == -1: self.return_type = "public " + self.return_type if self.overrides: header += "\t@Override\n" if self.return_type.find("byte") != -1 or self.return_type.find("short") != -1 or \ self.return_type.find("int") != -1 or self.return_type.find("long") != -1 or \ self.return_type.find("float") != -1 or self.return_type.find("double") != -1: self.method_body = "\n\t\treturn 0;" # if self.return_type.find("double") != -1: # self.method_body = "\n\t\treturn 0.0d;" elif self.return_type.find("boolean") != -1: self.method_body = "\n\t\treturn false;" elif self.return_type.find("char") != -1: self.method_body = "\n\t\treturn '\u0000';" elif self.return_type.find("void") == -1: self.method_body = '\n\t\treturn null;' if interface or self.return_type.find("abstract") != -1: return header + "\t" + self.return_type + " " + self.name + ";\n\n" return header + "\t" + self.return_type + " " + self.name + " {" + "\n\t\t" + \ "//TODO Add method body for " + self.name + self.method_body + "\n\t" + "}\n\n"
def find_fields_details(fields_list, soup): """ :param fields_list: :param soup: """ for field in fields_list: field_details = soup.find("a", {"name": field.name}) if (field_details == None): continue field.name = str(field_details.find_next("pre").text).replace( u'\u00a0', " ") if field_details.findNext("div", {"class": "block"}): field.comments = ReverseDoc.create_comment( str(field_details.findNext("div", { "class": "block" }).text), True)
def check_fields(new_constructor, fields): param_to_make = copy.deepcopy(new_constructor.parameters) for param in new_constructor.parameters: for field in fields: if param[0] == field.name: param_to_make.remove(param) for param in param_to_make: new_field = Fields.StaticField() new_field.comments = str(ReverseDoc.create_comment(param[1], True)) signature = str(new_constructor.sig) start = signature.index("(") end = signature.find(")") params = signature[start + 1:end] param_list = re.findall(r"[\w]+", params) param_loc = param_list.index(str(param[0])) new_field.name = "private " + param_list[param_loc - 1] + " " + param[0] fields.append(new_field)
def find_methods_details(methods_list, soup): """ method find_methods_details Adds comments from the method details area of the javadocs to the methods Arguments: methods_list - list of all of the methods """ for method in methods_list: method_details = soup.find( "a", {"name": re.compile(method.name.split("(")[0])}) if (method_details == None): continue method_details = method_details.find_next("ul") comment = method_details.find("div", {"class": "block"}) if comment: comment = comment.text else: comment = "" method.comments = ReverseDoc.create_comment(str(comment), True) method_parameters = method_details.find("span", {"class": "paramLabel"}) if method_parameters: parameter = method_parameters.parent.next_sibling.next_sibling parameters_list = list() while str(parameter).find("Returns:") == -1 and parameter and str( parameter).find("Throws:") == -1: parameters_list.append([ parameter.text.split("-", 1)[0].strip(), parameter.text.split("-", 1)[1].strip() ]) parameter = parameter.next_sibling.next_sibling method.parameters = parameters_list method_returns = method_details.find("span", {"class": "returnLabel"}) if method_returns and method.return_type.find("void") == -1: method.returns = method_returns.findNext("dd").text override = method_details.find("span", {"class": "overrideSpecifyLabel"}) if override and str(override.text) == "Overrides:": method.overrides = True
def __repr__(self, interface): """ method __repr__(self) Returns the method text in this format: /** * self.comments */ public self.return_type self.name { //Body } """ header = "" if self.comments: header += str(self.comments) if self.parameters: header += str(ReverseDoc.parameter_print(self.parameters)) if self.returns: self.returns = " ".join(str(self.returns).replace("\n", "").split()) self.returns = "\n\t * @return " + str(self.returns) header += self.returns if self.comments or self.parameters or self.returns: header += "\n\t */\n" if self.return_type.find("private") == -1 and self.return_type.find("protected") == -1: self.return_type = "public " + self.return_type if self.overrides: header += "\t@Override\n" if self.return_type.find("int") != -1: self.method_body = "\n\t\treturn 0;" if self.return_type.find("double") != -1: self.method_body = "\n\t\treturn 0.0;" if self.return_type.find("boolean") != -1: self.method_body = "\n\t\treturn False;" if self.return_type.find("String") != -1: self.method_body = '\n\t\treturn "";' if interface or self.return_type.find("abstract") != -1: return header + "\t" + self.return_type + " " + self.name + ";\n\n" return header + "\t" + self.return_type + " " + self.name + " {" + "\n\t\t" + \ "//TODO Add method body for " + self.name + self.method_body + "\n\t" + "}\n\n"
def find_constructor(soup): constructor = soup.find("a", {"name": "constructor.detail"}, recursive="true") if constructor: new_constructor = Constructor() new_constructor.sig = " ".join(str(constructor.findNext("pre").text).replace("\n", "").split()) if str(new_constructor.sig).find("(") - str(new_constructor.sig).find(")") != -1 and constructor.findNext("div", { "class": "block"}): new_constructor.comments = ReverseDoc.create_comment( str(constructor.findNext("div", {"class": "block"}).text), True) constructor_parameters = constructor.find("span", {"class": "paramLabel"}) if constructor_parameters: parameters_list = list() constructor_parameters = constructor_parameters.parent for parameter in constructor_parameters.find_all("dd"): if parameter.find("code") is not None: print(parameter) parameters_list.append([parameter.text.split("-", 1)[0].strip(), parameter.text.split("-", 1)[1].strip()]) new_constructor.parameters = parameters_list else: return None return new_constructor
def find_fields_details(fields_list, soup): for field in fields_list: field_details = soup.find("a", {"name": field.name}) if field_details.findNext("div", {"class": "block"}): field.comments = ReverseDoc.create_comment(str(field_details.findNext("div", {"class": "block"}).text), True)
def main(): htmlfile = input("Enter url to main doc page: ") output = input("Enter complete location to output src files: ") # htmlfile = "http://www.cs.rit.edu/~csci142/Projects/01/doc/" # htmlfile = "http://www.cs.rit.edu/~csci142/Labs/04/Doc/" # output = "/home/andrew/java" # output = "/home/andrew/school/CS142/4BankAccount/Lab4" # output = "/home/andrew/school/CS142/1Perp/Project1" javafile = htmlfile.replace("doc", "src") if htmlfile[-1] != "/": htmlfile += "/" if output[-1] != "/": output += "/" output += "src/" htmltext = urllib.request.urlopen(htmlfile + "overview-tree.html").read() soup = BeautifulSoup(htmltext) class_list = findClasses(soup) interface_list = findInterfaces(soup) for java_class in class_list: # if java_class.location.count(".") != 1: # continue try: new_class = ( urlopen(javafile + java_class.location.replace("html", "java")).read(), "try") except: new_class = (ReverseDoc.ReverseDoc( urllib.request.urlopen(htmlfile + java_class.location).read(), htmlfile), "except") path = os.path.join(output, java_class.location.replace(".html", "") + ".java") dirpath = path.rsplit("/", 1)[0] + "/" if not os.path.exists(dirpath): os.makedirs(dirpath) with open(path, "w") as f: if new_class[1] == "try": f.write(new_class[0].decode("utf-8")) else: f.write(new_class[0].__str__(False)) for interface in interface_list: # if interface.location.count(".") != 1: # continue try: new_interface = ( urlopen(javafile + interface.location.replace("html", "java")).read(), "try") except urllib.error.HTTPError: new_interface = (ReverseDoc.ReverseDoc( urllib.request.urlopen(htmlfile + interface.location).read(), htmlfile), "except") path = os.path.join(output, interface.location.replace(".html", "") + ".java") dirpath = path.rsplit("/", 1)[0] + "/" if not os.path.exists(dirpath): os.makedirs(dirpath) with open(path, "w") as f: if new_interface[1] == "try": f.write(new_interface[0].decode("utf-8")) else: f.write(new_interface[1].__str__(True))
def main(): parser = argparse.ArgumentParser( description='a program that generates stubs form JavaDoc') parser.add_argument('--doc', type=str, default='', help='url to main doc page(index)') parser.add_argument('--src', type=str, default='', help='complete location to output src files: ') args = parser.parse_args() htmlfile = args.doc output = args.src if (args.doc == ''): htmlfile = input("Enter url to main doc page: ") if (args.src == ''): output = input("Enter complete location to output src files: ") # htmlfile = "http://www.cs.rit.edu/~csci142/Projects/01/doc/" # htmlfile = "http://www.cs.rit.edu/~csci142/Labs/09/Doc/" # output = "/home/andrew/Documents/AJ-College/Spring2015/CS142/9Mogwai/Lab9" # htmlfile = "http://www.cs.rit.edu/~csci142/Labs/06/Doc/" # output = "/home/andrew/java" # output = "/home/andrew/Documents/AJ-College/Spring2015/CS142/6Graduation/Lab6" # output = "/home/andrew/school/CS142/4BankAccount/Lab4" # output = "/home/andrew/school/CS142/1Perp/Project1" if htmlfile[-1] != "/": #add slashes as appropriate htmlfile += "/" if output[-1] != "/": output += "/" output += "src/" # put the output in a directory called src htmltext = urllib.request.urlopen(htmlfile + "overview-tree.html").read() soup = BeautifulSoup(htmltext) class_list = findClasses(soup) interface_list = findInterfaces(soup) #TODO make this a function and pass it interface or class as appropriate for java_class in class_list: if (java_class.location.startswith("https://docs.oracle.com/javase")): continue new_class = (ReverseDoc.ReverseDoc( urllib.request.urlopen(htmlfile + java_class.location).read(), htmlfile), "except") path = os.path.join(output, java_class.location.replace(".html", "") + ".java") dirpath = path.rsplit("/", 1)[0] + "/" if not os.path.exists(dirpath): os.makedirs(dirpath) with open(path, "w") as f: #TODO see if the decoding or printing can be done at creation to remove if else if new_class[1] == "try": f.write(new_class[0].decode("utf-8")) else: f.write(new_class[0].__repr__( False)) #telling it to print as a class for interface in interface_list: if (interface.location.startswith("https://docs.oracle.com/javase")): continue new_interface = (ReverseDoc.ReverseDoc( urllib.request.urlopen(htmlfile + interface.location).read(), htmlfile), "except") path = os.path.join(output, interface.location.replace(".html", "") + ".java") dirpath = path.rsplit("/", 1)[0] + "/" if not os.path.exists(dirpath): os.makedirs(dirpath) with open(path, "w") as f: if new_interface[1] == "try": f.write(new_interface[0].decode("utf-8")) else: f.write(new_interface[0].__repr__( True)) #telling it to print as an interface