1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.component.writer; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.Writer; |
20 | |
import java.lang.reflect.Field; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import org.seasar.framework.util.FieldUtil; |
24 | |
import org.seasar.framework.util.StringUtil; |
25 | |
import org.seasar.uruma.annotation.ComponentAttribute; |
26 | |
import org.seasar.uruma.annotation.ComponentElement; |
27 | |
import org.seasar.uruma.component.Template; |
28 | |
import org.seasar.uruma.component.UIComponentContainer; |
29 | |
import org.seasar.uruma.component.UIElement; |
30 | |
import org.seasar.uruma.component.UIElementContainer; |
31 | |
import org.seasar.uruma.component.UIElementVisitor; |
32 | |
import org.seasar.uruma.core.UrumaConstants; |
33 | |
import org.seasar.uruma.util.AnnotationUtil; |
34 | |
import org.seasar.uruma.util.AssertionUtil; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class UIElementWriter implements UIElementVisitor, UrumaConstants { |
42 | |
protected Writer writer; |
43 | |
|
44 | |
private int indentLevel; |
45 | |
|
46 | |
private static final String INDENT = " "; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | public UIElementWriter(final Writer writer) { |
55 | 0 | AssertionUtil.assertNotNull("writer", writer); |
56 | 0 | this.writer = writer; |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void visit(final UIElement element) { |
63 | 0 | AssertionUtil.assertNotNull("element", element); |
64 | |
|
65 | |
try { |
66 | 0 | writeStartTag(element, true); |
67 | 0 | } catch (IOException ex) { |
68 | |
|
69 | 0 | ex.printStackTrace(); |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void visit(final UIElementContainer container) { |
77 | 0 | AssertionUtil.assertNotNull("container", container); |
78 | |
|
79 | |
try { |
80 | 0 | writeStartTag(container, false); |
81 | |
|
82 | 0 | indent(); |
83 | 0 | for (UIElement element : container.getChildren()) { |
84 | 0 | element.accept(this); |
85 | |
} |
86 | 0 | deindent(); |
87 | |
|
88 | 0 | writeEndTag(container); |
89 | 0 | } catch (IOException ex) { |
90 | |
|
91 | 0 | ex.printStackTrace(); |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void visit(final Template template) { |
99 | 0 | AssertionUtil.assertNotNull("template", template); |
100 | |
try { |
101 | 0 | writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
102 | 0 | writer |
103 | |
.write("<template xmlns=\"http://uruma.sandbox.seasar.org\">\n"); |
104 | 0 | indent(); |
105 | |
|
106 | 0 | UIComponentContainer root = template.getRootComponent(); |
107 | 0 | if (root != null) { |
108 | 0 | root.accept(this); |
109 | |
} |
110 | |
|
111 | 0 | deindent(); |
112 | 0 | writer.write("</template>\n"); |
113 | 0 | writer.flush(); |
114 | 0 | } catch (IOException ex) { |
115 | |
|
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | |
protected void writeStartTag(final UIElement element, |
120 | |
final boolean startTagOnly) throws IOException { |
121 | 0 | String elementName = getElementName(element.getClass()); |
122 | 0 | if (elementName == null) { |
123 | |
|
124 | 0 | return; |
125 | |
} |
126 | |
|
127 | 0 | writeIndent(); |
128 | 0 | writer.write('<'); |
129 | 0 | writer.write(elementName); |
130 | |
|
131 | 0 | List<Field> fields = AnnotationUtil.getAnnotatedFields(element |
132 | |
.getClass(), ComponentAttribute.class); |
133 | |
|
134 | 0 | for (Field field : fields) { |
135 | 0 | String attrName = getAttributeName(field); |
136 | 0 | String value = FieldUtil.getString(field, element); |
137 | 0 | if (value == null) { |
138 | 0 | ComponentAttribute attr = field |
139 | |
.getAnnotation(ComponentAttribute.class); |
140 | 0 | if (attr.required()) { |
141 | 0 | value = NULL_STRING; |
142 | |
} |
143 | |
} |
144 | |
|
145 | 0 | if (value != null) { |
146 | 0 | writer.write(' '); |
147 | 0 | writer.write(attrName); |
148 | 0 | writer.write("=\""); |
149 | 0 | writer.write(value); |
150 | 0 | writer.write('"'); |
151 | |
} |
152 | 0 | } |
153 | |
|
154 | 0 | if (startTagOnly) { |
155 | 0 | writer.write(" />\n"); |
156 | |
} else { |
157 | 0 | writer.write(" >\n"); |
158 | |
} |
159 | 0 | writer.flush(); |
160 | 0 | } |
161 | |
|
162 | |
protected void writeEndTag(final UIElement element) throws IOException { |
163 | 0 | String elementName = getElementName(element.getClass()); |
164 | 0 | if (elementName == null) { |
165 | |
|
166 | 0 | return; |
167 | |
} |
168 | |
|
169 | 0 | writeIndent(); |
170 | 0 | writer.write("</"); |
171 | 0 | writer.write(elementName); |
172 | 0 | writer.write(">\n"); |
173 | 0 | writer.flush(); |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
protected String getElementName(final Class<?> clazz) { |
188 | 0 | ComponentElement anno = clazz.getAnnotation(ComponentElement.class); |
189 | 0 | if (anno == null) { |
190 | 0 | return null; |
191 | |
} |
192 | |
|
193 | 0 | String elementName = anno.value(); |
194 | 0 | if (NULL_STRING.equals(elementName)) { |
195 | 0 | String className = clazz.getSimpleName(); |
196 | 0 | if (className.endsWith("Component")) { |
197 | 0 | elementName = className.substring(0, className.length() |
198 | |
- "Component".length()); |
199 | 0 | elementName = StringUtil.decapitalize(elementName); |
200 | |
} else { |
201 | 0 | return null; |
202 | |
} |
203 | |
} |
204 | |
|
205 | 0 | return elementName; |
206 | |
} |
207 | |
|
208 | |
protected String getAttributeName(final Field field) { |
209 | 0 | ComponentAttribute attr = field.getAnnotation(ComponentAttribute.class); |
210 | 0 | if (attr == null) { |
211 | 0 | return null; |
212 | |
} |
213 | |
|
214 | 0 | String attrName = attr.name(); |
215 | 0 | if (NULL_STRING.equals(attrName)) { |
216 | 0 | attrName = field.getName(); |
217 | |
} |
218 | 0 | return attrName; |
219 | |
} |
220 | |
|
221 | |
protected void writeIndent() throws IOException { |
222 | 0 | StringBuffer buf = new StringBuffer(INDENT.length() * indentLevel); |
223 | 0 | for (int i = 0; i < indentLevel; i++) { |
224 | 0 | buf.append(INDENT); |
225 | |
} |
226 | 0 | writer.write(buf.toString()); |
227 | 0 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public void resetIndent() { |
233 | 0 | indentLevel = 0; |
234 | 0 | } |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public void indent() { |
240 | 0 | indentLevel++; |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public void deindent() { |
247 | 0 | indentLevel--; |
248 | 0 | } |
249 | |
} |