KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > StoreAppender


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.storeconfig;
18
19 import java.beans.IndexedPropertyDescriptor JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.catalina.deploy.ResourceBase;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.tomcat.util.IntrospectionUtils;
29
30 /**
31  * StoreAppends generate really the xml tag elements
32  *
33  * @author Peter Rossbach
34  *
35  */

36 public class StoreAppender {
37     private static Log log = LogFactory.getLog(StoreAppender.class);
38
39     /**
40      * The set of classes that represent persistable properties.
41      */

42     private static Class JavaDoc persistables[] = { String JavaDoc.class, Integer JavaDoc.class,
43             Integer.TYPE, Boolean JavaDoc.class, Boolean.TYPE, Byte JavaDoc.class, Byte.TYPE,
44             Character JavaDoc.class, Character.TYPE, Double JavaDoc.class, Double.TYPE,
45             Float JavaDoc.class, Float.TYPE, Long JavaDoc.class, Long.TYPE, Short JavaDoc.class,
46             Short.TYPE, };
47
48     /**
49      * print the closing tag
50      *
51      * @param aWriter
52      * @param aDesc
53      * @throws Exception
54      */

55     public void printCloseTag(PrintWriter JavaDoc aWriter, StoreDescription aDesc)
56             throws Exception JavaDoc {
57         aWriter.print("</");
58         aWriter.print(aDesc.getTag());
59         aWriter.println(">");
60     }
61
62     /**
63      * print only the open tag with all attributes
64      *
65      * @param aWriter
66      * @param indent
67      * @param bean
68      * @param aDesc
69      * @throws Exception
70      */

71     public void printOpenTag(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc bean,
72             StoreDescription aDesc) throws Exception JavaDoc {
73         aWriter.print("<");
74         aWriter.print(aDesc.getTag());
75         if (aDesc.isAttributes() && bean != null)
76             printAttributes(aWriter, indent, bean, aDesc);
77         aWriter.println(">");
78     }
79
80     /**
81      * Print tag with all attributes
82      *
83      * @param aWriter
84      * @param indent
85      * @param bean
86      * @param aDesc
87      * @throws Exception
88      */

89     public void printTag(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc bean,
90             StoreDescription aDesc) throws Exception JavaDoc {
91         aWriter.print("<");
92         aWriter.print(aDesc.getTag());
93         if (aDesc.isAttributes() && bean != null)
94             printAttributes(aWriter, indent, bean, aDesc);
95         aWriter.println("/>");
96     }
97
98     /**
99      * print the value from tag as content
100      *
101      * @param aWriter
102      * @param tag
103      * @param content
104      * @throws Exception
105      */

106     public void printTagContent(PrintWriter JavaDoc aWriter, String JavaDoc tag, String JavaDoc content)
107             throws Exception JavaDoc {
108         aWriter.print("<");
109         aWriter.print(tag);
110         aWriter.print(">");
111         aWriter.print(convertStr(content));
112         aWriter.print("</");
113         aWriter.print(tag);
114         aWriter.println(">");
115     }
116
117     /**
118      * print an array of values
119      *
120      * @param aWriter
121      * @param tag
122      * @param indent
123      * @param elements
124      */

125     public void printTagValueArray(PrintWriter JavaDoc aWriter, String JavaDoc tag, int indent,
126             String JavaDoc[] elements) {
127         if (elements != null && elements.length > 0) {
128             printIndent(aWriter, indent + 2);
129             aWriter.print("<");
130             aWriter.print(tag);
131             aWriter.print(">");
132             for (int i = 0; i < elements.length; i++) {
133                 printIndent(aWriter, indent + 4);
134                 aWriter.print(elements[i]);
135                 if (i + 1 < elements.length)
136                     aWriter.println(",");
137             }
138             printIndent(aWriter, indent + 2);
139             aWriter.print("</");
140             aWriter.print(tag);
141             aWriter.println(">");
142         }
143     }
144
145     /**
146      * print a array of elements
147      *
148      * @param aWriter
149      * @param tag
150      * @param indent
151      * @param elements
152      */

153     public void printTagArray(PrintWriter JavaDoc aWriter, String JavaDoc tag, int indent,
154             String JavaDoc[] elements) throws Exception JavaDoc {
155         if (elements != null) {
156             for (int i = 0; i < elements.length; i++) {
157                 printIndent(aWriter, indent);
158                 printTagContent(aWriter, tag, elements[i]);
159             }
160         }
161     }
162
163     /**
164      * Print some spaces
165      *
166      * @param aWriter
167      * @param indent
168      * number of spaces
169      */

170     public void printIndent(PrintWriter JavaDoc aWriter, int indent) {
171         for (int i = 0; i < indent; i++) {
172             aWriter.print(' ');
173         }
174     }
175
176     /**
177      * Store the relevant attributes of the specified JavaBean, plus a
178      * <code>className</code> attribute defining the fully qualified Java
179      * class name of the bean.
180      *
181      * @param writer
182      * PrintWriter to which we are storing
183      * @param bean
184      * Bean whose properties are to be rendered as attributes,
185      *
186      * @exception Exception
187      * if an exception occurs while storing
188      */

189     public void printAttributes(PrintWriter JavaDoc writer, int indent, Object JavaDoc bean,
190             StoreDescription desc) throws Exception JavaDoc {
191
192         printAttributes(writer, indent, true, bean, desc);
193
194     }
195
196     /**
197      * Store the relevant attributes of the specified JavaBean.
198      *
199      * @param writer
200      * PrintWriter to which we are storing
201      * @param include
202      * Should we include a <code>className</code> attribute?
203      * @param bean
204      * Bean whose properties are to be rendered as attributes,
205      * @param desc
206      * RegistryDescrpitor from this bean
207      *
208      * @exception Exception
209      * if an exception occurs while storing
210      */

211     public void printAttributes(PrintWriter JavaDoc writer, int indent,
212             boolean include, Object JavaDoc bean, StoreDescription desc)
213             throws Exception JavaDoc {
214
215         // Render the relevant properties of this bean
216
String JavaDoc className = bean.getClass().getName();
217
218         // Render a className attribute if requested
219
if (include && desc != null && !desc.isStandard()) {
220             writer.print(" className=\"");
221             writer.print(bean.getClass().getName());
222             writer.print("\"");
223         }
224
225         // Acquire the list of properties for this bean
226
PropertyDescriptor JavaDoc descriptors[] = Introspector.getBeanInfo(
227                 bean.getClass()).getPropertyDescriptors();
228         if (descriptors == null) {
229             descriptors = new PropertyDescriptor JavaDoc[0];
230         }
231
232         // Create blank instance
233
Object JavaDoc bean2 = defaultInstance(bean);
234         for (int i = 0; i < descriptors.length; i++) {
235             if (descriptors[i] instanceof IndexedPropertyDescriptor JavaDoc) {
236                 continue; // Indexed properties are not persisted
237
}
238             if (!isPersistable(descriptors[i].getPropertyType())
239                     || (descriptors[i].getReadMethod() == null)
240                     || (descriptors[i].getWriteMethod() == null)) {
241                 continue; // Must be a read-write primitive or String
242
}
243             if (desc.isTransientAttribute(descriptors[i].getName())) {
244                 continue; // Skip the specified exceptions
245
}
246             Object JavaDoc value = IntrospectionUtils.getProperty(bean, descriptors[i]
247                     .getName());
248             if (value == null) {
249                 continue; // Null values are not persisted
250
}
251             Object JavaDoc value2 = IntrospectionUtils.getProperty(bean2,
252                     descriptors[i].getName());
253             if (value.equals(value2)) {
254                 // The property has its default value
255
continue;
256             }
257             if (isPrintValue(bean, bean2, descriptors[i].getName(), desc))
258                 printValue(writer, indent, descriptors[i].getName(), value);
259         }
260
261         if (bean instanceof ResourceBase) {
262             ResourceBase resource = (ResourceBase) bean;
263             for (Iterator JavaDoc iter = resource.listProperties(); iter.hasNext();) {
264                 String JavaDoc name = (String JavaDoc) iter.next();
265                 Object JavaDoc value = resource.getProperty(name);
266                 if (!isPersistable(value.getClass())) {
267                     continue;
268                 }
269                 if (desc.isTransientAttribute(name)) {
270                     continue; // Skip the specified exceptions
271
}
272                 printValue(writer, indent, name, value);
273
274             }
275         }
276     }
277
278     /**
279      * print this Attribute value or not
280      *
281      * @param bean
282      * orginal bean
283      * @param bean2
284      * default bean
285      * @param attrName
286      * attribute name
287      * @param desc
288      * StoreDescription from bean
289      * @return
290      */

291     public boolean isPrintValue(Object JavaDoc bean, Object JavaDoc bean2, String JavaDoc attrName,
292             StoreDescription desc) {
293         boolean printValue = false;
294
295         Object JavaDoc value = IntrospectionUtils.getProperty(bean, attrName);
296         if (value != null) {
297             Object JavaDoc value2 = IntrospectionUtils.getProperty(bean2, attrName);
298             printValue = !value.equals(value2);
299
300         }
301         return printValue;
302     }
303
304     /**
305      * generate default Instance
306      *
307      * @param bean
308      * @return an object from same class as bean parameter
309      * @throws InstantiationException
310      * @throws IllegalAccessException
311      */

312     public Object JavaDoc defaultInstance(Object JavaDoc bean) throws InstantiationException JavaDoc,
313             IllegalAccessException JavaDoc {
314         return bean.getClass().newInstance();
315     }
316
317     /**
318      * print an attribute value
319      *
320      * @param writer
321      * @param name
322      * @param value
323      */

324     public void printValue(PrintWriter JavaDoc writer, int indent, String JavaDoc name,
325             Object JavaDoc value) {
326         if (!(value instanceof String JavaDoc)) {
327             value = value.toString();
328         }
329         writer.println();
330         printIndent(writer, indent + 4);
331         writer.print(name);
332         writer.print("=\"");
333         String JavaDoc strValue = convertStr((String JavaDoc) value);
334         writer.print(strValue);
335         writer.print("\"");
336     }
337
338     /**
339      * Given a string, this method replaces all occurrences of ' <', '>', '&',
340      * and '"'.
341      */

342
343     public String JavaDoc convertStr(String JavaDoc input) {
344
345         StringBuffer JavaDoc filtered = new StringBuffer JavaDoc(input.length());
346         char c;
347         for (int i = 0; i < input.length(); i++) {
348             c = input.charAt(i);
349             if (c == '<') {
350                 filtered.append("&lt;");
351             } else if (c == '>') {
352                 filtered.append("&gt;");
353             } else if (c == '\'') {
354                 filtered.append("&apos;");
355             } else if (c == '"') {
356                 filtered.append("&quot;");
357             } else if (c == '&') {
358                 filtered.append("&amp;");
359             } else {
360                 filtered.append(c);
361             }
362         }
363         return (filtered.toString());
364     }
365
366     /**
367      * Is the specified property type one for which we should generate a
368      * persistence attribute?
369      *
370      * @param clazz
371      * Java class to be tested
372      */

373     protected boolean isPersistable(Class JavaDoc clazz) {
374
375         for (int i = 0; i < persistables.length; i++) {
376             if (persistables[i] == clazz) {
377                 return (true);
378             }
379         }
380         return (false);
381
382     }
383 }
Popular Tags