KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > cfg > DefaultPropertyProvider


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.JsStatements;
25
26 import java.io.IOException JavaDoc;
27 import java.io.StringReader JavaDoc;
28
29 /**
30  * A property provider that reports property values specified literally in a
31  * host HTML page.
32  */

33 public class DefaultPropertyProvider extends PropertyProvider {
34
35   /*
36    * TODO: this references 'parent' literally, which could be a problem if you
37    * were to include the selector script in the host page itself rather than in
38    * an iframe.
39    */

40   public DefaultPropertyProvider(ModuleDef module, Property property) {
41     super(module, property);
42     String JavaDoc src = "function () {";
43     src += "return _gwt_getMetaProperty(\"";
44     src += property.getName();
45     src += "\"); }";
46     setBody(parseFunction(src));
47   }
48
49   private JsBlock parseFunction(String JavaDoc jsniSrc) {
50     Throwable JavaDoc caught = null;
51     try {
52       JsProgram jsPgm = new JsProgram();
53       JsParser jsParser = new JsParser();
54       StringReader JavaDoc r = new StringReader JavaDoc(jsniSrc);
55       JsStatements stmts = jsParser.parse(jsPgm.getScope(), r, 1);
56       JsFunction fn = (JsFunction) ((JsExprStmt) stmts.get(0)).getExpression();
57       return fn.getBody();
58     } catch (IOException JavaDoc e) {
59       caught = e;
60     } catch (JsParserException e) {
61       caught = e;
62     }
63     throw new RuntimeException JavaDoc(
64         "Internal error parsing source for default property provider", caught);
65   }
66 }
67
Popular Tags