001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.i18n.rebind;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.UnableToCompleteException;
020: import com.google.gwt.core.ext.typeinfo.JMethod;
021: import com.google.gwt.user.rebind.AbstractGeneratorClassCreator;
022:
023: /**
024: * Method creator to handle <code>Number</code> creation.
025: */
026: class SimpleValueMethodCreator extends AbstractLocalizableMethodCreator {
027:
028: /**
029: * Helper class to delegate to the correct Number parser for this method.
030: */
031: public abstract static class AbstractValueCreator {
032: abstract String getValue(String stringVal);
033: }
034:
035: private static class BadBooleanPropertyValue extends
036: RuntimeException {
037: }
038:
039: static final AbstractValueCreator DOUBLE = new AbstractValueCreator() {
040: @Override
041: String getValue(String stringVal) {
042: return Double.parseDouble(stringVal) + "";
043: }
044: };
045: static final AbstractValueCreator FLOAT = new AbstractValueCreator() {
046: @Override
047: String getValue(String stringVal) {
048: return Float.parseFloat(stringVal) + "f";
049: }
050: };
051: static final AbstractValueCreator INT = new AbstractValueCreator() {
052: @Override
053: String getValue(String stringVal) {
054: return Integer.parseInt(stringVal) + "";
055: }
056: };
057: static final AbstractValueCreator STRING = new AbstractValueCreator() {
058: @Override
059: String getValue(String stringVal) {
060: return wrap(stringVal);
061: }
062: };
063: static final AbstractValueCreator BOOLEAN = new AbstractValueCreator() {
064: @Override
065: String getValue(String stringVal) {
066: if ("true".equals(stringVal)) {
067: return "true";
068: } else if ("false".equals(stringVal)) {
069: return "false";
070: } else {
071: throw new BadBooleanPropertyValue();
072: }
073: }
074: };
075:
076: private AbstractValueCreator valueCreator;
077:
078: /**
079: * Constructor for <code>SimpleValueMethodCreator</code>.
080: *
081: * @param currentCreator
082: * @param valueCreator
083: */
084: SimpleValueMethodCreator(
085: AbstractGeneratorClassCreator currentCreator,
086: AbstractValueCreator valueCreator) {
087: super (currentCreator);
088: this .valueCreator = valueCreator;
089: }
090:
091: @Override
092: public void createMethodFor(TreeLogger logger,
093: JMethod targetMethod, String value)
094: throws UnableToCompleteException {
095: try {
096: String translatedValue = valueCreator.getValue(value);
097: println("return " + translatedValue + ";");
098: } catch (NumberFormatException e) {
099: throw error(logger, value
100: + " could not be parsed as a number.");
101: } catch (BadBooleanPropertyValue e) {
102: throw error(
103: logger,
104: "'"
105: + value
106: + "' is not a valid boolean property value; must be 'true' or 'false'");
107: }
108: }
109: }
|