KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.ext.BadPropertyValueException;
19 import com.google.gwt.core.ext.GeneratorContext;
20 import com.google.gwt.core.ext.PropertyOracle;
21 import com.google.gwt.core.ext.TreeLogger;
22 import com.google.gwt.core.ext.UnableToCompleteException;
23
24 /**
25  * A deferred binding condition to determine whether a named property has a
26  * particular value.
27  */

28 public class ConditionWhenPropertyIs extends Condition {
29
30   private final String JavaDoc propName;
31
32   private final String JavaDoc value;
33
34   public ConditionWhenPropertyIs(String JavaDoc propName, String JavaDoc value) {
35     this.propName = propName;
36     this.value = value;
37   }
38
39   public String JavaDoc toString() {
40     return "<when-property-is name='" + propName + "' value='" + value + "'/>";
41   }
42
43   protected boolean doEval(TreeLogger logger, GeneratorContext context,
44       String JavaDoc testType) throws UnableToCompleteException {
45     String JavaDoc testValue;
46     try {
47       PropertyOracle propertyOracle = context.getPropertyOracle();
48       testValue = propertyOracle.getPropertyValue(logger, propName);
49       logger.log(TreeLogger.DEBUG, "Property value is '" + testValue + "'",
50           null);
51       if (testValue.equals(value)) {
52         return true;
53       } else {
54         return false;
55       }
56     } catch (BadPropertyValueException e) {
57       String JavaDoc msg = "Unable to get value of property '" + propName + "'";
58       logger.log(TreeLogger.ERROR, msg, e);
59       throw new UnableToCompleteException();
60     }
61   }
62
63   protected String JavaDoc getEvalAfterMessage(String JavaDoc testType, boolean result) {
64     if (result) {
65       return "Yes, the property value matched";
66     } else {
67       return "No, the value did not match";
68     }
69   }
70
71   protected String JavaDoc getEvalBeforeMessage(String JavaDoc testType) {
72     return toString();
73   }
74 }
75
Popular Tags