-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-for-java.py
129 lines (106 loc) · 4.13 KB
/
main-for-java.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import getopt, sys, os
from common import schemaParser, sampleGenerator
from java import misc, loaderGenerator
def _printUsage():
print('usage:')
print('python3 -m json-schema/java <schema name> <sample directory> <package root directory> ' +
'<class package> [ -i|--interface-package <interface package> ] <schema file> ...')
def _checkArgs(schemaName, sampleDir, pkgRootDir, clasPkg, intfPkg, schemaFiles):
print('')
print(' schema name: ' + schemaName)
print(' sample dir: ' + sampleDir)
print(' pkg root dir: ' + pkgRootDir)
print(' class pkg: ' + clasPkg)
print('interface pkg: ' + (intfPkg if intfPkg else '<none>'))
print(' schema files: ' + str(schemaFiles))
print('')
# sample dir must exist
if not os.path.isdir(sampleDir):
print('error: ' + sampleDir + ' is not an existing directory');
sys.exit(1)
# package root dir must exist
if not os.path.isdir(pkgRootDir):
print('error: ' + pkgRootDir + ' is not an existing directory');
sys.exit(1)
# class dir must exist
clasDir = pkgRootDir + '/' + clasPkg.replace('.', '/')
if not os.path.isdir(clasDir):
print('creating a directory ' + clasDir + ' to put the generated implementation class file in');
os.makedirs(clasDir)
# interface dir must exist
if intfPkg:
intfDir = pkgRootDir + '/' + intfPkg.replace('.', '/')
if not os.path.isdir(intfDir):
print('creating a directory ' + intfDir + ' to put the generated interface file in');
os.makedirs(intfDir)
else:
intfDir = None
return (clasDir, intfDir)
def main():
if len(sys.argv) < 6:
_printUsage()
sys.exit(1)
schemaName = sys.argv[1]
sampleDir = sys.argv[2]
pkgRootDir = sys.argv[3]
clasPkg = sys.argv[4]
try:
opts, schemaFiles = getopt.getopt(sys.argv[5:], "i:", ['interface-package='])
except getopt.GetoptError as err:
print(err)
_printUsage()
sys.exit(1)
intfPkg = None # default is not to generate an interface
for o, a in opts:
if o == '-i' or o == '--interface-package':
intfPkg = a
else:
assert False, ('illegal option ' + o)
(clasDir, intfDir) = _checkArgs(schemaName, sampleDir, pkgRootDir, clasPkg, intfPkg, schemaFiles)
(enumDef, structDef, arrElemTypes) = schemaParser.parse(schemaName, schemaFiles)
samplePath = sampleDir + '/' + schemaName + '.sample.json'
fldComments = sampleGenerator.generate(samplePath, enumDef, structDef, schemaName)
assert fldComments != None
genIntf = (intfPkg != None)
if genIntf:
# create a java file defining the interface
intfName = misc.getIntfName(schemaName)
print("creating the interface file " + intfDir + '/' + intfName + '.java')
intfFile = misc.createJavaFile(intfName, intfPkg, intfDir, [
"io.github.hyunikn.jsonschemalib.UINT64"
])
try:
loaderGenerator.writeIntf(intfFile, enumDef, structDef, schemaName)
except:
intfFile.close()
os.remove(intfFile.name)
raise
intfFile.close()
# create a java file defining the implementation class
imports = [
"io.github.hyunikn.jsonschemalib.*",
"io.github.hyunikn.jsonden.*",
"io.github.hyunikn.jsonden.exception.*",
'io.github.getify.minify.Minify',
'java.io.File',
'java.io.FileOutputStream',
'java.util.TreeSet',
'java.util.Set',
]
if genIntf:
imports.append(intfPkg + '.' + intfName)
clasName = misc.getClasName(schemaName)
print("creating the implementation class file " + clasDir + '/' + clasName + '.java')
clasFile = misc.createJavaFile(clasName, clasPkg, clasDir, imports)
try:
clasDef = loaderGenerator.getClassDef(schemaName, enumDef, structDef, arrElemTypes, genIntf, fldComments)
clasFile.write(clasDef)
except:
clasFile.close()
os.remove(clasFile.name)
raise
clasFile.close()
if __name__ == '__main__':
main()
else:
raise Exception('can only be run')