#!/usr/bin/env python from glob import glob import os, re, sys classre = re.compile(r'(?P(/\*\*([^/]|/[^*])*?\*/\s*)?)(?Ppublic\s+((abstract)\s+)*)(?P(class|interface)\s+)?(?P\w+)\s*(?P(extends|implements|{))', re.DOTALL) fieldre = re.compile(r'(?P(/\*\*([^/]|/[^*])*?\*/\s*)?)(?Ppublic\s+((abstract|final|static)\s+)*)(?P[\w\[\]]+\s+)?(?P\w+)(?P\s*(\(.*?\)|=|;))', re.DOTALL) paramre = re.compile(r'\s*(?:final\s*)?(?P[\w\[\]]+)\s+(?P\w+)\s*(?P,?)') def fieldsig(m): return '%s%s' % (m.group('field_name'), paramre.sub('\g\g', m.group('params') or '')) def parse(input): javadoc = {} for m in classre.finditer(input): javadoc[m.group('class_name')] = m.group('javadoc') for m in fieldre.finditer(input): javadoc[fieldsig(m)] = m.group('javadoc') return javadoc def mkclass(m): #if debug: #print "Looking up", m.group('class_name') return '%s%s%s%s %s' % (javadoc.get(m.group('class_name'), '/** TODO */\n'), m.group('mods'), (m.group('class_type') or ''), m.group('class_name'), m.group('end')) def mkfield(m): #if debug: #print "Looking up", fieldsig(m) return '%s%s%s%s%s' % (javadoc.get(fieldsig(m), '/** TODO */\n '), m.group('mods'), (m.group('field_type') or ''), m.group('field_name'), m.group('params')) if len(sys.argv) < 3 or not os.path.isfile(sys.argv[1]) or not os.path.isfile(sys.argv[2]): print >>sys.stderr, "Usage %s " % sys.argv[0] sys.exit(-1) docfile, srcfile = sys.argv[1:3] # Parse the javadoc from the doc file #debug = True or (os.path.basename(docfile) == 'Database.java') javadoc = parse(open(docfile).read()) #if debug: #print repr(javadoc) # Merge the javadoc with the source file newsrc = classre.sub(mkclass, open(srcfile).read()) newsrc = fieldre.sub(mkfield, newsrc) sys.stdout.write(newsrc)