1 package org.apache.torque.generator.outlet.java;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.torque.generator.GeneratorException;
25 import org.apache.torque.generator.control.ControllerState;
26 import org.apache.torque.generator.qname.Namespace;
27 import org.apache.torque.generator.qname.QualifiedName;
28 import org.apache.torque.generator.source.SourceElement;
29 import org.apache.torque.generator.source.SourcePath;
30 import org.apache.torque.generator.variable.Variable;
31
32
33
34
35
36 public final class OutletUtils
37 {
38
39
40
41 private OutletUtils()
42 {
43 }
44
45
46 private static Log log = LogFactory.getLog(OutletUtils.class);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public static String getSourceElementAttribute(
66 String elementName,
67 String attributeName,
68 ControllerState controllerState,
69 Class<?> clazz)
70 throws GeneratorException
71 {
72 SourceElement sourceElement = SourcePath.getElement(
73 controllerState.getSourceElement(),
74 elementName,
75 false);
76 Object attribute
77 = sourceElement.getAttribute(attributeName);
78 if (attribute == null)
79 {
80 throw new GeneratorException(
81 "Source element attribute not set in "
82 + clazz.getName()
83 + "\n"
84 + "The attribute "
85 + attributeName
86 + " of the source element "
87 + elementName
88 + " is not set.");
89 }
90 return attribute.toString();
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public static String getOption(
107 String optionName,
108 ControllerState controllerState,
109 Class<?> clazz)
110 throws GeneratorException
111 {
112 Object optionValue = controllerState.getOption(optionName);
113 if (optionValue == null)
114 {
115 throw new GeneratorException("Invalid configuration of "
116 + clazz.getName()
117 + "\n"
118 + "The option "
119 + optionName
120 + " is not set.");
121 }
122 return optionValue.toString();
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public static String getFromDifferentPlaces(
153 String presetValue,
154 String optionName,
155 String variableName,
156 String sourceElementName,
157 String sourceElementAttribute,
158 ControllerState controllerState,
159 Class<?> clazz,
160 String expectedFieldNames)
161 throws GeneratorException
162 {
163 if (optionName != null
164 && sourceElementName == null
165 && presetValue == null
166 && variableName == null)
167 {
168 return OutletUtils.getOption(
169 optionName,
170 controllerState,
171 clazz);
172 }
173 else if (sourceElementName != null
174 && optionName == null
175 && presetValue == null
176 && variableName == null)
177 {
178 return OutletUtils.getSourceElementAttribute(
179 sourceElementName,
180 sourceElementAttribute,
181 controllerState,
182 clazz);
183 }
184 else if (variableName != null
185 && sourceElementName == null
186 && optionName == null
187 && presetValue == null)
188 {
189 Namespace namespace
190 = controllerState.getOutlet().getName().getNamespace();
191 QualifiedName variableQualifiedName = new QualifiedName(
192 variableName,
193 namespace);
194 Variable variable
195 = controllerState.getVariableStore().getInHierarchy(
196 variableQualifiedName);
197 if (variable == null)
198 {
199 log.info("clazz.getName() : Variable " + variableQualifiedName
200 + " is not set, returning the empty String");
201 return "";
202 }
203 if (variable.getValue() == null)
204 {
205 log.info("clazz.getName() : Variable " + variableQualifiedName
206 + " is set to null, returning the empty String");
207 return "";
208 }
209 return variable.getValue().toString();
210 }
211 else if (presetValue != null
212 && sourceElementName == null
213 && optionName == null
214 && variableName == null)
215 {
216 return presetValue;
217 }
218 else
219 {
220 throw new GeneratorException("Invalid configuration of "
221 + clazz.getName()
222 + "\n"
223 + "Make sure that one and only one of "
224 + expectedFieldNames
225 + " are set.");
226 }
227 }
228 }