KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > dynamic > StringArrayXPathBasedConfigItem


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.dynamic;
5
6 import org.apache.xmlbeans.XmlObject;
7 import org.apache.xmlbeans.impl.common.XPath;
8
9 import com.tc.config.schema.context.ConfigContext;
10 import com.tc.util.Assert;
11
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.lang.reflect.Modifier JavaDoc;
15
16 /**
17  * An {@link XPathBasedConfigItem} that returns a {@link String} array. The {@link XPath} should point to the top-level
18  * element (i.e., the one that has child elements that contain nothing but {@link String}s).
19  */

20 public class StringArrayXPathBasedConfigItem extends XPathBasedConfigItem implements StringArrayConfigItem {
21
22   public StringArrayXPathBasedConfigItem(ConfigContext context, String JavaDoc xpath) {
23     super(context, xpath);
24   }
25
26   protected Object JavaDoc fetchDataFromXmlObject(XmlObject xmlObject) {
27     if (xmlObject == null) return null;
28     
29     // This is a little tricky; the name of the method that returns String[] can be variable. However, there seems to be
30
// only one such method declared in the class itself, so we can find it by reflection. If this assumption ever
31
// proves to be false, we'll need to re-visit this logic.
32
Method JavaDoc targetMethod = null;
33     Method JavaDoc[] allMethods = xmlObject.getClass().getMethods();
34
35     for (int i = 0; i < allMethods.length; ++i) {
36       Method JavaDoc candidate = allMethods[i];
37       if (candidate.getReturnType().equals(String JavaDoc[].class) && candidate.getParameterTypes().length == 0
38           && Modifier.isPublic(candidate.getModifiers()) && candidate.getName().startsWith("get")
39           && candidate.getName().endsWith("Array")) {
40         if (targetMethod != null) {
41           // formatting
42
throw Assert
43               .failure("Whoa! There are multiple public methods that start with 'get', end with 'Array', take no parameters, "
44                        + "and return String[] on class "
45                        + xmlObject.getClass().getName()
46                        + ". One is "
47                        + targetMethod
48                        + ", and another is " + candidate + ". We should fix " + "the program to account for this.");
49         }
50
51         targetMethod = candidate;
52       }
53     }
54
55     if (targetMethod == null) {
56       // formatting
57
throw Assert.failure("Class " + xmlObject.getClass().getName() + " has no public methods that start with 'get', "
58                            + "end with 'Array', take no parameters, and return String[].");
59     }
60
61     try {
62       return targetMethod.invoke(xmlObject, new Object JavaDoc[0]);
63     } catch (IllegalArgumentException JavaDoc iae) {
64       throw Assert.failure("Couldn't invoke method " + targetMethod + " on object " + xmlObject + ": ", iae);
65     } catch (IllegalAccessException JavaDoc iae) {
66       throw Assert.failure("Couldn't invoke method " + targetMethod + " on object " + xmlObject + ": ", iae);
67     } catch (InvocationTargetException JavaDoc ite) {
68       throw Assert.failure("Couldn't invoke method " + targetMethod + " on object " + xmlObject + ": ", ite);
69     }
70   }
71
72   public String JavaDoc[] getStringArray() {
73     return (String JavaDoc[]) getObject();
74   }
75
76 }
77
Popular Tags