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.
35 import template_expander
37 from in_file
import InFile
41 # FIXME: Remove this special case for the ugly x-webkit-foo attributes.
42 if entry
['name'].startswith('x-webkit-'):
43 return entry
['name'].replace('-', '')[1:]
44 return entry
['name'].replace('-', '_')
47 class MakeQualifiedNamesWriter(in_generator
.Writer
):
50 default_parameters
= {
51 'attrsNullNamespace': None,
54 'namespacePrefix': '',
59 'enable_conditional': name_utilities
.enable_conditional_if_endif
,
61 'to_macro_style': name_utilities
.to_macro_style
,
64 def __init__(self
, in_file_paths
):
65 super(MakeQualifiedNamesWriter
, self
).__init
__(None)
66 assert len(in_file_paths
) <= 2, 'MakeQualifiedNamesWriter requires at most 2 in files, got %d.' % len(in_file_paths
)
68 if len(in_file_paths
) == 2:
69 self
.tags_in_file
= InFile
.load_from_files([in_file_paths
.pop(0)], self
.defaults
, self
.valid_values
, self
.default_parameters
)
71 self
.tags_in_file
= None
73 self
.attrs_in_file
= InFile
.load_from_files([in_file_paths
.pop()], self
.defaults
, self
.valid_values
, self
.default_parameters
)
75 self
.namespace
= self
._parameter
('namespace')
77 namespace_prefix
= self
._parameter
('namespacePrefix') or self
.namespace
.lower()
78 namespace_uri
= self
._parameter
('namespaceURI')
80 use_namespace_for_attrs
= self
.attrs_in_file
.parameters
['attrsNullNamespace'] is None
83 (self
.namespace
+ "Names.h"): self
.generate_header
,
84 (self
.namespace
+ "Names.cpp"): self
.generate_implementation
,
86 self
._template
_context
= {
87 'attrs': self
.attrs_in_file
.name_dictionaries
,
88 'export': self
._parameter
('export'),
89 'namespace': self
.namespace
,
90 'namespace_prefix': namespace_prefix
,
91 'namespace_uri': namespace_uri
,
92 'tags': self
.tags_in_file
.name_dictionaries
if self
.tags_in_file
else [],
93 'use_namespace_for_attrs': use_namespace_for_attrs
,
96 def _parameter(self
, name
):
97 parameter
= self
.attrs_in_file
.parameters
[name
].strip('"')
99 assert parameter
== self
.tags_in_file
.parameters
[name
].strip('"'), 'Both in files must have the same %s.' % name
102 @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters
=filters
)
103 def generate_header(self
):
104 return self
._template
_context
106 @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters
=filters
)
107 def generate_implementation(self
):
108 return self
._template
_context
111 if __name__
== "__main__":
112 in_generator
.Maker(MakeQualifiedNamesWriter
).main(sys
.argv
)