OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generates java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
6 | 6 |
7 import argparse | 7 import argparse |
8 import os | 8 import os |
9 import re | 9 import re |
10 | 10 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 isinstance(element, mojom.Field)): | 71 isinstance(element, mojom.Field)): |
72 return CamelCase(element.name) | 72 return CamelCase(element.name) |
73 if isinstance(element, mojom.EnumValue): | 73 if isinstance(element, mojom.EnumValue): |
74 return (UpperCamelCase(element.enum_name) + '.' + | 74 return (UpperCamelCase(element.enum_name) + '.' + |
75 ConstantStyle(element.name)) | 75 ConstantStyle(element.name)) |
76 if (isinstance(element, mojom.NamedValue) or | 76 if (isinstance(element, mojom.NamedValue) or |
77 isinstance(element, mojom.Constant)): | 77 isinstance(element, mojom.Constant)): |
78 return ConstantStyle(element.name) | 78 return ConstantStyle(element.name) |
79 raise Exception("Unexpected element: " % element) | 79 raise Exception("Unexpected element: " % element) |
80 | 80 |
| 81 def ParseStringAttribute(attribute): |
| 82 if isinstance(attribute, basestring): |
| 83 return attribute |
| 84 assert attribute[0] == 'EXPRESSION' |
| 85 assert len(attribute[1]) == 1 |
| 86 return attribute[1][0][1:-1].encode('string_escape') |
| 87 |
81 def GetPackage(module): | 88 def GetPackage(module): |
82 if 'JavaPackage' in module.attributes: | 89 if 'JavaPackage' in module.attributes: |
83 package = module.attributes['JavaPackage'] | 90 return ParseStringAttribute(module.attributes['JavaPackage']) |
84 if isinstance(package, basestring): | |
85 return package | |
86 assert package[0] == 'EXPRESSION' | |
87 assert len(package[1]) == 1 | |
88 return package[1][0][1:-1].encode('string_escape') | |
89 # Default package. | 91 # Default package. |
90 return "org.chromium.mojom." + module.namespace | 92 return "org.chromium.mojom." + module.namespace |
91 | 93 |
92 def GetNameForKind(kind): | 94 def GetNameForKind(kind): |
93 def _GetNameHierachy(kind): | 95 def _GetNameHierachy(kind): |
94 hierachy = [] | 96 hierachy = [] |
95 if kind.parent_kind: | 97 if kind.parent_kind: |
96 hierachy = _GetNameHierachy(kind.parent_kind) | 98 hierachy = _GetNameHierachy(kind.parent_kind) |
97 hierachy.append(kind.name) | 99 hierachy.append(kind.name) |
98 return hierachy | 100 return hierachy |
(...skipping 29 matching lines...) Expand all Loading... |
128 return token + 'L' | 130 return token + 'L' |
129 return token | 131 return token |
130 | 132 |
131 def ExpressionToText(value, module): | 133 def ExpressionToText(value, module): |
132 if value[0] != "EXPRESSION": | 134 if value[0] != "EXPRESSION": |
133 raise Exception("Expected EXPRESSION, got" + value) | 135 raise Exception("Expected EXPRESSION, got" + value) |
134 return "".join(generator.ExpressionMapper(value, | 136 return "".join(generator.ExpressionMapper(value, |
135 lambda token: TranslateConstants(token, module))) | 137 lambda token: TranslateConstants(token, module))) |
136 | 138 |
137 def GetConstantsMainEntityName(module): | 139 def GetConstantsMainEntityName(module): |
| 140 if 'JavaConstantsClassName' in module.attributes: |
| 141 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
138 # This constructs the name of the embedding classes for module level constants | 142 # This constructs the name of the embedding classes for module level constants |
139 # by extracting the mojom's filename and prepending it to Constants. | 143 # by extracting the mojom's filename and prepending it to Constants. |
140 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 144 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
141 'Constants') | 145 'Constants') |
142 | 146 |
143 class Generator(generator.Generator): | 147 class Generator(generator.Generator): |
144 | 148 |
145 java_filters = { | 149 java_filters = { |
146 "expression_to_text": ExpressionToText, | 150 "expression_to_text": ExpressionToText, |
147 "java_type": GetJavaType, | 151 "java_type": GetJavaType, |
(...skipping 21 matching lines...) Expand all Loading... |
169 args = parser.parse_args(unparsed_args) | 173 args = parser.parse_args(unparsed_args) |
170 if self.output_dir and args.java_output_directory: | 174 if self.output_dir and args.java_output_directory: |
171 self.output_dir = os.path.join(args.java_output_directory, | 175 self.output_dir = os.path.join(args.java_output_directory, |
172 GetPackage(self.module).replace('.', '/')) | 176 GetPackage(self.module).replace('.', '/')) |
173 if not os.path.exists(self.output_dir): | 177 if not os.path.exists(self.output_dir): |
174 os.makedirs(self.output_dir) | 178 os.makedirs(self.output_dir) |
175 | 179 |
176 if self.module.constants: | 180 if self.module.constants: |
177 self.Write(self.GenerateConstantsSource(self.module), | 181 self.Write(self.GenerateConstantsSource(self.module), |
178 "%s.java" % GetConstantsMainEntityName(self.module)) | 182 "%s.java" % GetConstantsMainEntityName(self.module)) |
OLD | NEW |