KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > schema > rules > SmartTranslator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.schema.rules;
16
17 import java.beans.PropertyEditor JavaDoc;
18 import java.beans.PropertyEditorManager JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.internal.Module;
24 import org.apache.hivemind.schema.Translator;
25
26 /**
27  * A "smart" translator that attempts to automatically convert from string types to object or
28  * wrapper types, using {@link java.beans.PropertyEditor}s.
29  *
30  * @author Howard Lewis Ship
31  */

32 public class SmartTranslator implements Translator
33 {
34     private String JavaDoc _default;
35
36     public SmartTranslator()
37     {
38     }
39
40     /**
41      * Initializers:
42      * <ul>
43      * <li>default: default value for empty input
44      * </ul>
45      */

46     public SmartTranslator(String JavaDoc initializer)
47     {
48         Map JavaDoc m = RuleUtils.convertInitializer(initializer);
49
50         _default = (String JavaDoc) m.get("default");
51     }
52
53     public Object JavaDoc translate(Module contributingModule, Class JavaDoc propertyType, String JavaDoc inputValue,
54             Location location)
55     {
56         // HIVEMIND-10: Inside JavaWebStart you (strangely) can't rely on
57
// a PropertyEditor for String (even though it is trivial).
58

59         if (inputValue == null)
60         {
61             if (_default == null)
62                 return null;
63
64             inputValue = _default;
65         }
66
67         if (propertyType.equals(String JavaDoc.class) || propertyType.equals(Object JavaDoc.class))
68             return inputValue;
69
70         // TODO: This duplicates logic inside PropertyAdaptor.
71

72         try
73         {
74             PropertyEditor JavaDoc e = PropertyEditorManager.findEditor(propertyType);
75
76             if (e == null)
77                 throw new ApplicationRuntimeException(RulesMessages.noPropertyEditor(propertyType),
78                         location, null);
79
80             e.setAsText(inputValue);
81
82             return e.getValue();
83         }
84         catch (Exception JavaDoc ex)
85         {
86             throw new ApplicationRuntimeException(RulesMessages.smartTranslatorError(
87                     inputValue,
88                     propertyType,
89                     ex), location, ex);
90
91         }
92     }
93
94 }
Popular Tags