Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / tools / reflection_generator / reflection_generator.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import optparse
8 import os
9 import shutil
10 import sys
11
12 from bridge_generator import BridgeGenerator
13 from interface_generator import InterfaceGenerator
14 from java_class import JavaClassLoader
15 from wrapper_generator import WrapperGenerator
16
17 # Classes list that have to generate bridge and wrap code.
18 CLASSES_TO_BE_PROCESS = [
19   'XWalkExtensionInternal',
20   'XWalkViewInternal',
21   'XWalkUIClientInternal',
22   'XWalkResourceClientInternal',
23   'XWalkPreferencesInternal',
24   'XWalkNavigationItemInternal',
25   'XWalkNavigationHistoryInternal',
26   'XWalkJavascriptResultHandlerInternal',
27   'XWalkJavascriptResultInternal',
28 ]
29
30
31 WRAPPER_PACKAGE = 'org.xwalk.core'
32 BRIDGE_PACKAGE = 'org.xwalk.core.internal'
33
34
35 def FormatPackagePath(folder, package):
36   return os.path.join(folder, os.path.sep.join(package.split('.')))
37
38
39 def PerformSerialize(output_path, generator, package):
40   # Serialize the code.
41   file_name = os.path.join(FormatPackagePath(output_path, package),
42                            generator.GetGeneratedClassFileName())
43   if not os.path.isdir(os.path.dirname(file_name)):
44     os.makedirs(os.path.dirname(file_name))
45   file_handle = open(file_name, 'w')
46   file_handle.write(generator.GetGeneratedCode())
47   file_handle.close()
48   print '%s has been generated!' % (file_name)
49
50
51 def GenerateBindingForJavaClass(
52     java_data, bridge_output, wrap_output, class_loader):
53   if java_data.class_type == 'interface':
54     interface_generator = InterfaceGenerator(java_data, class_loader)
55     interface_generator.RunTask()
56     PerformSerialize(wrap_output, interface_generator, WRAPPER_PACKAGE)
57   else:
58     # Generate Bridge code.
59     bridge_generator = BridgeGenerator(java_data, class_loader)
60     bridge_generator.RunTask()
61     # Serialize.
62     PerformSerialize(bridge_output, bridge_generator, BRIDGE_PACKAGE)
63     # Generate Wrapper code.
64     wrapper_generator = WrapperGenerator(java_data, class_loader)
65     wrapper_generator.RunTask()
66     PerformSerialize(wrap_output, wrapper_generator, WRAPPER_PACKAGE)
67
68
69 def GenerateBindingForJavaDirectory(input_dir, bridge_output, wrap_output):
70   java_class_loader = JavaClassLoader(input_dir, CLASSES_TO_BE_PROCESS)
71   for input_file in os.listdir(input_dir):
72     input_class_name = input_file.replace('.java', '')
73     if java_class_loader.IsInternalClass(input_class_name):
74       # Load all java classes in first.
75       java_data = java_class_loader.LoadJavaClass(input_class_name)
76       print 'Generate bridge and wrapper code for %s' % input_class_name
77       GenerateBindingForJavaClass(
78           java_data, bridge_output, wrap_output, java_class_loader)
79
80
81 def CopyReflectionHelperJava(helper_class, wrap_output):
82   if helper_class is None:
83     return
84   f = open(helper_class, 'r')
85   output = os.path.join(FormatPackagePath(wrap_output, WRAPPER_PACKAGE),
86                         os.path.basename(helper_class))
87   if not os.path.isdir(os.path.dirname(output)):
88     os.makedirs(os.path.dirname(output))
89   fo = open(output, 'w')
90   for line in f.read().split('\n'):
91     if line.startswith('package '):
92       fo.write('package org.xwalk.core;\n')
93     else:
94       if 'Wrapper Only' in line:
95         pass
96       else:
97         fo.write(line + '\n')
98   fo.close()
99   f.close()
100
101
102 def Touch(path):
103   if not os.path.isdir(os.path.dirname(path)):
104     os.makedirs(os.path.dirname(path))
105   with open(path, 'a'):
106     os.utime(path, None)
107
108
109 def main(argv):
110   usage = """Usage: %prog [OPTIONS]
111 This script can generate bridge and wrap source files for given directory. 
112 \'input_dir\' is provided as directory containing source files.
113   """
114   option_parser = optparse.OptionParser(usage=usage)
115   option_parser.add_option('--input_dir',
116                            help= ('Input source file directory which contains'
117                                   'input files'))
118   option_parser.add_option('--bridge_output',
119                            help=('Output directory where the bridge code'
120                                  'is placed.'))
121   option_parser.add_option('--wrap_output',
122                            help=('Output directory where the wrap code'
123                                 'is placed.'))
124   option_parser.add_option('--helper_class',
125                            help=('the path of ReflectionHelper java source, '
126                                 'will copy it to output folder'))
127   option_parser.add_option('--stamp', help='the file to touch on success.')
128   options, _ = option_parser.parse_args(argv)
129   if not options.input_dir:
130     print('Error: Must specify input.')
131     return 1
132   if os.path.isdir(options.bridge_output):
133     shutil.rmtree(options.bridge_output)
134   if os.path.isdir(options.wrap_output):
135     shutil.rmtree(options.wrap_output)
136
137   if options.input_dir:
138     GenerateBindingForJavaDirectory(options.input_dir,
139         options.bridge_output, options.wrap_output)
140     CopyReflectionHelperJava(options.helper_class,
141         options.wrap_output)
142
143   if options.stamp:
144     Touch(options.stamp)
145
146
147 if __name__ == '__main__':
148   sys.exit(main(sys.argv))