2 # Copyright (C) 2013 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 from collections import defaultdict
34 import template_expander
37 from make_qualified_names import MakeQualifiedNamesWriter
40 class MakeElementFactoryWriter(MakeQualifiedNamesWriter):
41 defaults = dict(MakeQualifiedNamesWriter.default_parameters, **{
42 'JSInterfaceName': None,
44 'constructorNeedsCreatedByParser': None,
45 'constructorNeedsFormElement': None,
46 'contextConditional': None,
47 'interfaceName': None,
48 'noConstructor': None,
49 'noTypeHelpers': None,
50 'runtimeEnabled': None,
52 default_parameters = dict(MakeQualifiedNamesWriter.default_parameters, **{
53 'fallbackInterfaceName': '',
54 'fallbackJSInterfaceName': '',
56 filters = MakeQualifiedNamesWriter.filters
58 def __init__(self, in_file_paths):
59 super(MakeElementFactoryWriter, self).__init__(in_file_paths)
61 # FIXME: When we start using these element factories, we'll want to
62 # remove the "new" prefix and also have our base class generate
63 # *Names.h and *Names.cpp.
64 self._outputs.update({
65 (self.namespace + 'ElementFactory.h'): self.generate_factory_header,
66 (self.namespace + 'ElementFactory.cpp'): self.generate_factory_implementation,
67 ('V8' + self.namespace + 'ElementWrapperFactory.h'): self.generate_wrapper_factory_header,
68 ('V8' + self.namespace + 'ElementWrapperFactory.cpp'): self.generate_wrapper_factory_implementation,
71 fallback_interface = self.tags_in_file.parameters['fallbackInterfaceName'].strip('"')
72 fallback_js_interface = self.tags_in_file.parameters['fallbackJSInterfaceName'].strip('"') or fallback_interface
74 interface_counts = defaultdict(int)
75 tags = self._template_context['tags']
77 tag['has_js_interface'] = self._has_js_interface(tag)
78 tag['js_interface'] = self._js_interface(tag)
79 tag['interface'] = self._interface(tag)
80 interface_counts[tag['interface']] += 1
83 tag['multipleTagNames'] = (interface_counts[tag['interface']] > 1 or tag['interface'] == fallback_interface)
85 self._template_context.update({
86 'fallback_interface': fallback_interface,
87 'fallback_js_interface': fallback_js_interface,
90 @template_expander.use_jinja('ElementFactory.h.tmpl', filters=filters)
91 def generate_factory_header(self):
92 return self._template_context
94 @template_expander.use_jinja('ElementFactory.cpp.tmpl', filters=filters)
95 def generate_factory_implementation(self):
96 return self._template_context
98 @template_expander.use_jinja('ElementWrapperFactory.h.tmpl', filters=filters)
99 def generate_wrapper_factory_header(self):
100 return self._template_context
102 @template_expander.use_jinja('ElementWrapperFactory.cpp.tmpl', filters=filters)
103 def generate_wrapper_factory_implementation(self):
104 return self._template_context
106 def _interface(self, tag):
107 if tag['interfaceName']:
108 return tag['interfaceName']
109 name = name_utilities.upper_first(tag['name'])
110 # FIXME: We shouldn't hard-code HTML here.
113 dash = name.find('-')
115 name = name[:dash] + name[dash + 1].upper() + name[dash + 2:]
116 dash = name.find('-')
117 return '%s%sElement' % (self.namespace, name)
119 def _js_interface(self, tag):
120 if tag['JSInterfaceName']:
121 return tag['JSInterfaceName']
122 return self._interface(tag)
124 def _has_js_interface(self, tag):
125 return not tag['noConstructor'] and self._js_interface(tag) != ('%sElement' % self.namespace)
128 if __name__ == "__main__":
129 in_generator.Maker(MakeElementFactoryWriter).main(sys.argv)