OLD | NEW |
1 // Copyright (C) 2008 Google Inc. | 1 // Copyright (C) 2008 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 values.add(new IntegerLiteral(unk, valueMaker.apply(e))); | 121 values.add(new IntegerLiteral(unk, valueMaker.apply(e))); |
122 } | 122 } |
123 return new ExpressionStmt(unk, | 123 return new ExpressionStmt(unk, |
124 (Expression) QuasiBuilder.substV( | 124 (Expression) QuasiBuilder.substV( |
125 "html4.@i = { @k*: @v* };", | 125 "html4.@i = { @k*: @v* };", |
126 "i", new Reference(new Identifier(unk, key)), | 126 "i", new Reference(new Identifier(unk, key)), |
127 "k", new ParseTreeNodeContainer(keys), | 127 "k", new ParseTreeNodeContainer(keys), |
128 "v", new ParseTreeNodeContainer(values))); | 128 "v", new ParseTreeNodeContainer(values))); |
129 } | 129 } |
130 | 130 |
| 131 /** |
| 132 * Generate a JS object-building statement from the given Map, whose keys |
| 133 * should stringify to the JS keys and whose values are mapped by the function |
| 134 * {@code codegen}. |
| 135 */ |
| 136 private static <T> ExpressionStmt mapFromMap( |
| 137 Map<?, T> entries, String key, Function<T, Expression> codegen) { |
| 138 final FilePosition unk = FilePosition.UNKNOWN; |
| 139 List<StringLiteral> keys = new ArrayList<StringLiteral>(); |
| 140 List<Expression> values = new ArrayList<Expression>(); |
| 141 for (Map.Entry<?, T> e : entries.entrySet()) { |
| 142 keys.add(StringLiteral.valueOf(unk, e.getKey().toString())); |
| 143 values.add(codegen.apply(e.getValue())); |
| 144 } |
| 145 ExpressionStmt def = new ExpressionStmt(unk, (Expression) |
| 146 QuasiBuilder.substV( |
| 147 "html4.@i = { @k*: @v* };", |
| 148 "i", new Reference(new Identifier(unk, key)), |
| 149 "k", new ParseTreeNodeContainer(keys), |
| 150 "v", new ParseTreeNodeContainer(values))); |
| 151 return def; |
| 152 } |
| 153 |
131 public static Block generateJavascriptDefinitions(HtmlSchema schema) { | 154 public static Block generateJavascriptDefinitions(HtmlSchema schema) { |
132 final FilePosition unk = FilePosition.UNKNOWN; | 155 final FilePosition unk = FilePosition.UNKNOWN; |
133 Map<AttribKey, HTML.Attribute.Type> atypes = attributeTypes(schema); | 156 Map<AttribKey, HTML.Attribute.Type> atypes = attributeTypes(schema); |
134 Map<ElKey, EnumSet<EFlag>> eflags = elementFlags(schema); | 157 Map<ElKey, EnumSet<EFlag>> eflags = elementFlags(schema); |
| 158 Map<ElKey, String> einterfaces = elementDOMInterfaces(schema); |
135 Map<AttribKey, UriEffect> uriEffects = uriEffects(schema); | 159 Map<AttribKey, UriEffect> uriEffects = uriEffects(schema); |
136 Map<AttribKey, LoaderType> ltypes = loaderTypes(schema); | 160 Map<AttribKey, LoaderType> ltypes = loaderTypes(schema); |
137 | 161 |
138 Block definitions = new Block(); | 162 Block definitions = new Block(); |
139 definitions.appendChild(QuasiBuilder.substV("var html4 = {};")); | 163 definitions.appendChild(QuasiBuilder.substV("var html4 = {};")); |
140 | 164 |
141 definitions.appendChild(mapFromEnum( | 165 definitions.appendChild(mapFromEnum( |
142 EnumSet.allOf(HTML.Attribute.Type.class), | 166 EnumSet.allOf(HTML.Attribute.Type.class), |
143 "atype", | 167 "atype", |
144 new Function<HTML.Attribute.Type, String>() { | 168 new Function<HTML.Attribute.Type, String>() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 return f.name(); | 205 return f.name(); |
182 } | 206 } |
183 }, | 207 }, |
184 new Function<EFlag, Integer>() { | 208 new Function<EFlag, Integer>() { |
185 public Integer apply(EFlag f) { | 209 public Integer apply(EFlag f) { |
186 return f.bitMask; | 210 return f.bitMask; |
187 } | 211 } |
188 }) | 212 }) |
189 ); | 213 ); |
190 | 214 |
191 { | 215 definitions.appendChild(mapFromMap( |
192 List<StringLiteral> keys = new ArrayList<StringLiteral>(); | 216 eflags, |
193 List<IntegerLiteral> values = new ArrayList<IntegerLiteral>(); | 217 "ELEMENTS", |
194 for (Map.Entry<ElKey, EnumSet<EFlag>> e : eflags.entrySet()) { | 218 new Function<EnumSet<EFlag>, Expression>() { |
195 ElKey key = e.getKey(); | 219 public Expression apply(EnumSet<EFlag> flags) { |
196 int value = 0; | 220 int value = 0; |
197 for (EFlag f : e.getValue()) { value |= f.bitMask; } | 221 for (EFlag f : flags) { value |= f.bitMask; } |
198 keys.add(StringLiteral.valueOf(unk, key.toString())); | 222 return new IntegerLiteral(unk, value); |
199 values.add(new IntegerLiteral(unk, value)); | 223 } |
200 } | 224 }) |
201 definitions.appendChild(new ExpressionStmt(unk, (Expression) | 225 ); |
202 QuasiBuilder.substV( | 226 |
203 "html4.ELEMENTS = { @k*: @v* };", | 227 definitions.appendChild(mapFromMap( |
204 "k", new ParseTreeNodeContainer(keys), | 228 einterfaces, |
205 "v", new ParseTreeNodeContainer(values)))); | 229 "ELEMENT_DOM_INTERFACES", |
206 } | 230 new Function<String, Expression>() { |
| 231 public Expression apply(String domInterface) { |
| 232 return new StringLiteral(unk, domInterface); |
| 233 } |
| 234 }) |
| 235 ); |
207 | 236 |
208 definitions.appendChild(mapFromEnum( | 237 definitions.appendChild(mapFromEnum( |
209 EnumSet.allOf(UriEffect.class), | 238 EnumSet.allOf(UriEffect.class), |
210 "ueffects", | 239 "ueffects", |
211 new Function<UriEffect, String>() { | 240 new Function<UriEffect, String>() { |
212 public String apply(UriEffect f) { | 241 public String apply(UriEffect f) { |
213 return f.name(); | 242 return f.name(); |
214 } | 243 } |
215 }, | 244 }, |
216 new Function<UriEffect, Integer>() { | 245 new Function<UriEffect, Integer>() { |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 flags.add(EFlag.FOLDABLE); | 454 flags.add(EFlag.FOLDABLE); |
426 } | 455 } |
427 if (schema.isElementVirtualized(elementName)) { | 456 if (schema.isElementVirtualized(elementName)) { |
428 flags.add(EFlag.VIRTUALIZED); | 457 flags.add(EFlag.VIRTUALIZED); |
429 } | 458 } |
430 elementFlags.put(elementName, flags); | 459 elementFlags.put(elementName, flags); |
431 } | 460 } |
432 return elementFlags; | 461 return elementFlags; |
433 } | 462 } |
434 | 463 |
| 464 private static Map<ElKey, String> elementDOMInterfaces(HtmlSchema schema) { |
| 465 Map<ElKey, String> data = Maps.newTreeMap(); |
| 466 for (ElKey elementName : schema.getElementNames()) { |
| 467 HTML.Element el = schema.lookupElement(elementName); |
| 468 data.put(elementName, el.getDOMInterface()); |
| 469 } |
| 470 return data; |
| 471 } |
| 472 |
435 public static class Builder implements BuildCommand { | 473 public static class Builder implements BuildCommand { |
436 public boolean build(List<File> inputs, List<File> deps, Map<String, Object>
options, | 474 public boolean build(List<File> inputs, List<File> deps, Map<String, Object>
options, |
437 File output) throws IOException { | 475 File output) throws IOException { |
438 File elementsFile = null; | 476 File elementsFile = null; |
439 File attrsFile = null; | 477 File attrsFile = null; |
440 for (File input : inputs) { | 478 for (File input : inputs) { |
441 if (input.getName().endsWith(".json")) { | 479 if (input.getName().endsWith(".json")) { |
442 if (elementsFile == null) { | 480 if (elementsFile == null) { |
443 elementsFile = input; | 481 elementsFile = input; |
444 } else if (attrsFile == null) { | 482 } else if (attrsFile == null) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 PropertyNameQuotingMode.PRESERVE_QUOTES); | 541 PropertyNameQuotingMode.PRESERVE_QUOTES); |
504 for (Statement s : node.children()) { | 542 for (Statement s : node.children()) { |
505 s.render(rc); | 543 s.render(rc); |
506 if (!s.isTerminal()) { rc.getOut().consume(";"); } | 544 if (!s.isTerminal()) { rc.getOut().consume(";"); } |
507 } | 545 } |
508 rc.getOut().noMoreTokens(); | 546 rc.getOut().noMoreTokens(); |
509 out.write("\n"); | 547 out.write("\n"); |
510 out.write("// exports for Closure Compiler\n"); | 548 out.write("// exports for Closure Compiler\n"); |
511 out.write("html4['ATTRIBS'] = html4.ATTRIBS;\n"); | 549 out.write("html4['ATTRIBS'] = html4.ATTRIBS;\n"); |
512 out.write("html4['ELEMENTS'] = html4.ELEMENTS;\n"); | 550 out.write("html4['ELEMENTS'] = html4.ELEMENTS;\n"); |
| 551 out.write("html4['ELEMENT_DOM_INTERFACES'] = " + |
| 552 "html4.ELEMENT_DOM_INTERFACES;\n"); |
513 out.write("html4['URIEFFECTS'] = html4.URIEFFECTS;\n"); | 553 out.write("html4['URIEFFECTS'] = html4.URIEFFECTS;\n"); |
514 out.write("html4['LOADERTYPES'] = html4.LOADERTYPES;\n"); | 554 out.write("html4['LOADERTYPES'] = html4.LOADERTYPES;\n"); |
515 out.write("html4['atype'] = html4.atype;\n"); | 555 out.write("html4['atype'] = html4.atype;\n"); |
516 out.write("html4['eflags'] = html4.eflags;\n"); | 556 out.write("html4['eflags'] = html4.eflags;\n"); |
517 out.write("html4['ltypes'] = html4.ltypes;\n"); | 557 out.write("html4['ltypes'] = html4.ltypes;\n"); |
518 out.write("html4['ueffects'] = html4.ueffects;\n"); | 558 out.write("html4['ueffects'] = html4.ueffects;\n"); |
519 out.write("if (typeof window !== 'undefined') {\n"); | 559 out.write("if (typeof window !== 'undefined') {\n"); |
520 out.write(" window['html4'] = html4;\n"); | 560 out.write(" window['html4'] = html4;\n"); |
521 out.write("}\n"); | 561 out.write("}\n"); |
522 } finally { | 562 } finally { |
523 out.close(); | 563 out.close(); |
524 } | 564 } |
525 return true; | 565 return true; |
526 } | 566 } |
527 } | 567 } |
528 | 568 |
529 public static void main(String[] args) { | 569 public static void main(String[] args) { |
530 HtmlSchema schema = HtmlSchema.getDefault(new SimpleMessageQueue()); | 570 HtmlSchema schema = HtmlSchema.getDefault(new SimpleMessageQueue()); |
531 Block node = generateJavascriptDefinitions(schema); | 571 Block node = generateJavascriptDefinitions(schema); |
532 RenderContext rc = new RenderContext(node.makeRenderer(System.out, null)); | 572 RenderContext rc = new RenderContext(node.makeRenderer(System.out, null)); |
533 for (Statement s : node.children()) { | 573 for (Statement s : node.children()) { |
534 s.render(rc); | 574 s.render(rc); |
535 if (!s.isTerminal()) { rc.getOut().consume(";"); } | 575 if (!s.isTerminal()) { rc.getOut().consume(";"); } |
536 } | 576 } |
537 rc.getOut().noMoreTokens(); | 577 rc.getOut().noMoreTokens(); |
538 } | 578 } |
539 } | 579 } |
OLD | NEW |