KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > values > types > ValueType


1 package org.oddjob.values.types;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.InputStream JavaDoc;
5
6 import org.oddjob.arooa.reflect.IntrospectionHelper;
7
8 /**
9  * @oddjob.description A simple value, the most common type.
10  * <p>
11  * A value can be:
12  * <ul>
13  * <li>Any simple type, either text or a number or boolean.</li>
14  * <li>A URL or file name.</li>
15  * <li>It can also be a reference to any other type somewhere else. i.e.
16  * value can contain a ${someid.anyvalue} reference.</li>
17  * <li>Used as an input to jobs like {@link org.oddjob.io.CopyJob}.</li>
18  * </ul>
19  * Large amounts of text can also be declared inline if required.
20  *
21  *
22  * @oddjob.example
23  *
24  * <pre>
25  * &lt;sequential&gt;
26  * &lt;variables id="vars"&gt;
27  * &lt;value name="fruit" value="apple"/&gt;
28  * &lt;/variables&gt;
29  * &lt;echo text="${vars.fruit}"/&gt;
30  * &lt;/sequential&gt;
31  * </pre>
32  *
33  * @oddjob.example
34  *
35  * <pre>
36  * &lt;sequential&gt;
37  * &lt;variables id="vars2"&gt;
38  * &lt;value name="more" value="${vars.fruit}"/&gt;
39  * &lt;/variables&gt;
40  * &lt;echo text="${vars2.more}"/&gt;
41  * &lt;/sequential&gt;
42  * </pre>
43  *
44  * @oddjob.example
45  *
46  * <pre>
47  * &lt;sequential&gt;
48  * &lt;variables id="vars"&gt;
49  * &lt;value name="essay"&gt;
50  * What a lot of text I'm now going to write...
51  * &lt;/value&gt;
52  * &lt;/variables&gt;
53  * &lt;copy input="${vars.essay}" to="essay.txt"/&gt;
54  * &lt;/sequential&gt;
55  * </pre>
56  *
57  * @author Rob Gordon.
58  */

59 public class ValueType {
60     // private static final Logger logger = Logger.getLogger(ValueType.class);
61

62     /**
63      * @oddjob.property
64      * @oddjob.description Any simple value.
65      * @oddjob.required Yes, if not provided as element text.
66      */

67     private Object JavaDoc value;
68     
69     public void setValue(Object JavaDoc value) {
70         this.value = value;
71     }
72     
73     /**
74      * Resolve the value.
75      *
76      * @param required The required class.
77      *
78      * @return The value. Could be null.
79      */

80     public Object JavaDoc valueFor(Class JavaDoc required) {
81         if (value == null) {
82             return null;
83         }
84         Object JavaDoc valueFor = IntrospectionHelper.valueFor(value, required);
85         if (valueFor != value) {
86             return valueFor;
87         }
88         
89         if (required.isAssignableFrom(Object JavaDoc.class)) {
90             return value;
91         }
92         
93         if (required.isAssignableFrom(InputStream JavaDoc.class)) {
94             return new ByteArrayInputStream JavaDoc(value.toString().getBytes());
95         }
96
97         return value;
98     }
99
100     public void addText(String JavaDoc text) {
101         String JavaDoc t= "";
102         if (this.value != null) {
103             t = (String JavaDoc) valueFor(String JavaDoc.class);
104         }
105         t += text;
106         value = t;
107     }
108     
109     public String JavaDoc toString() {
110         if (value == null) {
111             return null;
112         }
113         return value.toString();
114     }
115 }
116
Popular Tags