KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > rebind > SimpleValueMethodCreator


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.i18n.rebind;
17
18 import com.google.gwt.core.ext.TreeLogger;
19 import com.google.gwt.core.ext.UnableToCompleteException;
20 import com.google.gwt.core.ext.typeinfo.JMethod;
21 import com.google.gwt.user.rebind.AbstractGeneratorClassCreator;
22
23 /**
24  * Method creator to handle <code>Number</code> creation.
25  */

26 class SimpleValueMethodCreator extends AbstractLocalizableMethodCreator {
27
28   /**
29    * Helper class to delegate to the correct Number parser for this method.
30    */

31   public abstract static class AbstractValueCreator {
32     abstract String JavaDoc getValue(String JavaDoc stringVal);
33   }
34
35   private static class BadBooleanPropertyValue extends RuntimeException JavaDoc {
36   }
37
38   static final AbstractValueCreator DOUBLE = new AbstractValueCreator() {
39     String JavaDoc getValue(String JavaDoc stringVal) {
40       return Double.parseDouble(stringVal) + "";
41     }
42   };
43   static final AbstractValueCreator FLOAT = new AbstractValueCreator() {
44     String JavaDoc getValue(String JavaDoc stringVal) {
45       return Float.parseFloat(stringVal) + "f";
46     }
47   };
48   static final AbstractValueCreator INT = new AbstractValueCreator() {
49     String JavaDoc getValue(String JavaDoc stringVal) {
50       return Integer.parseInt(stringVal) + "";
51     }
52   };
53   static final AbstractValueCreator STRING = new AbstractValueCreator() {
54     String JavaDoc getValue(String JavaDoc stringVal) {
55       return wrap(stringVal);
56     }
57   };
58   static final AbstractValueCreator BOOLEAN = new AbstractValueCreator() {
59     String JavaDoc getValue(String JavaDoc stringVal) {
60       if ("true".equals(stringVal)) {
61         return "true";
62       } else if ("false".equals(stringVal)) {
63         return "false";
64       } else {
65         throw new BadBooleanPropertyValue();
66       }
67     }
68   };
69
70   private AbstractValueCreator valueCreator;
71
72   /**
73    * Constructor for <code>SimpleValueMethodCreator</code>.
74    *
75    * @param currentCreator
76    * @param valueCreator
77    */

78   SimpleValueMethodCreator(AbstractGeneratorClassCreator currentCreator,
79       AbstractValueCreator valueCreator) {
80     super(currentCreator);
81     this.valueCreator = valueCreator;
82   }
83
84   public void createMethodFor(TreeLogger logger, JMethod targetMethod,
85       String JavaDoc value) throws UnableToCompleteException {
86     try {
87       String JavaDoc translatedValue = valueCreator.getValue(value);
88       println("return " + translatedValue + ";");
89     } catch (NumberFormatException JavaDoc e) {
90       throw error(logger, value + " could not be parsed as a number.");
91     } catch (BadBooleanPropertyValue e) {
92       throw error(
93           logger,
94           "'"
95               + value
96               + "' is not a valid boolean property value; must be 'true' or 'false'");
97     }
98   }
99 }
100
Popular Tags