01: /*
02: * Copyright 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.cfg;
17:
18: import com.google.gwt.dev.js.JsParser;
19: import com.google.gwt.dev.js.JsParserException;
20: import com.google.gwt.dev.js.ast.JsBlock;
21: import com.google.gwt.dev.js.ast.JsExprStmt;
22: import com.google.gwt.dev.js.ast.JsFunction;
23: import com.google.gwt.dev.js.ast.JsProgram;
24: import com.google.gwt.dev.js.ast.JsStatement;
25:
26: import java.io.IOException;
27: import java.io.StringReader;
28: import java.util.List;
29:
30: /**
31: * A property provider that reports property values specified literally in a
32: * host HTML page.
33: */
34: public class DefaultPropertyProvider extends PropertyProvider {
35:
36: /*
37: * TODO: this references 'parent' literally, which could be a problem if you
38: * were to include the selector script in the host page itself rather than in
39: * an iframe.
40: */
41: public DefaultPropertyProvider(ModuleDef module, Property property) {
42: super (module, property);
43: String src = "function () {";
44: src += "return _gwt_getMetaProperty(\"";
45: src += property.getName();
46: src += "\"); }";
47: setBody(parseFunction(src));
48: }
49:
50: private JsBlock parseFunction(String jsniSrc) {
51: Throwable caught = null;
52: try {
53: JsProgram jsPgm = new JsProgram();
54: JsParser jsParser = new JsParser();
55: StringReader r = new StringReader(jsniSrc);
56: List<JsStatement> stmts = jsParser.parse(jsPgm.getScope(),
57: r, 1);
58: JsFunction fn = (JsFunction) ((JsExprStmt) stmts.get(0))
59: .getExpression();
60: return fn.getBody();
61: } catch (IOException e) {
62: caught = e;
63: } catch (JsParserException e) {
64: caught = e;
65: }
66: throw new RuntimeException(
67: "Internal error parsing source for default property provider",
68: caught);
69: }
70: }
|