KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > arooa > HandlerUtils


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.arooa;
5
6 import org.oddjob.arooa.ArooaException;
7 import org.oddjob.arooa.reflect.BeanUtilsHelper;
8 import org.oddjob.designer.model.DesignAttribute;
9 import org.xml.sax.Attributes JavaDoc;
10
11 /**
12  *
13  */

14 public class HandlerUtils {
15
16     public static void setAttributes(String JavaDoc uri, Attributes JavaDoc attrs, Object JavaDoc obj)
17     throws ArooaException {
18         for (int i = 0; i < attrs.getLength(); i++) {
19             String JavaDoc attrUri = attrs.getURI(i);
20             if (attrUri != null
21                 && !attrUri.equals("")
22                 && !attrUri.equals(uri)) {
23                 continue; // Ignore attributes from unknown uris
24
}
25             String JavaDoc name = attrs.getLocalName(i);
26             String JavaDoc value = attrs.getValue(i);
27
28             Class JavaDoc attributeClass = null;
29             attributeClass = BeanUtilsHelper.getPropertyType(obj, name);
30         
31             // if the attribute is a design element we create it too.
32
if (DesignAttribute.class.isAssignableFrom(attributeClass)) {
33                 DesignAttribute de = null;
34                 try {
35                     de = (DesignAttribute) attributeClass.newInstance();
36                 }
37                 catch (Exception JavaDoc e) {
38                     throw new ArooaException("Failed to create [" + attributeClass + "]", e);
39                 }
40                 de.attribute(value);
41                 BeanUtilsHelper.setProperty(obj, name, de);
42             }
43             // if not we hope it's a string.
44
else {
45                 BeanUtilsHelper.setProperty(obj, name, value);
46             }
47         }
48
49     }
50 }
51
Popular Tags