KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > DelegatingObjectModelProvider


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24 import java.lang.reflect.Method JavaDoc;
25 import org.jboss.logging.Logger;
26
27 /**
28  * todo come up with a nicer class name
29  *
30  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
31  * @version <tt>$Revision: 1958 $</tt>
32  */

33 public class DelegatingObjectModelProvider
34    implements GenericObjectModelProvider
35 {
36    private static final Logger log = Logger.getLogger(DelegatingObjectModelProvider.class);
37
38    private final ObjectModelProvider provider;
39    private final boolean trace = log.isTraceEnabled();
40
41    public DelegatingObjectModelProvider(ObjectModelProvider provider)
42    {
43       this.provider = provider;
44    }
45
46    public Object JavaDoc getChildren(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
47    {
48       return provideChildren(provider, o, namespaceURI, localName);
49    }
50
51    public Object JavaDoc getElementValue(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
52    {
53       return provideValue(provider, o, namespaceURI, localName);
54    }
55
56    public Object JavaDoc getAttributeValue(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
57    {
58       return provideAttributeValue(provider, o, namespaceURI, localName);
59    }
60
61    public Object JavaDoc getRoot(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
62    {
63       return provider.getRoot(o, null, namespaceURI, localName);
64    }
65
66    // private
67

68    Object JavaDoc provideChildren(ObjectModelProvider provider,
69                           Object JavaDoc parent,
70                           String JavaDoc namespaceUri,
71                           String JavaDoc name)
72    {
73       Class JavaDoc providerClass = provider.getClass();
74       Class JavaDoc parentClass = parent.getClass();
75       String JavaDoc methodName = "getChildren";
76
77       Object JavaDoc container = null;
78       Method JavaDoc method = getProviderMethod(providerClass,
79          methodName,
80          new Class JavaDoc[]{parentClass, String JavaDoc.class, String JavaDoc.class}
81       );
82       if(method != null)
83       {
84          try
85          {
86             container = method.invoke(provider, new Object JavaDoc[]{parent, namespaceUri, name});
87          }
88          catch(Exception JavaDoc e)
89          {
90             log.error("Failed to invoke method " + methodName, e);
91             throw new IllegalStateException JavaDoc("Failed to invoke method " + methodName);
92          }
93       }
94       else if(trace)
95       {
96          log.trace("No " + methodName + " for " + name);
97       }
98       return container;
99    }
100
101    Object JavaDoc provideValue(ObjectModelProvider provider,
102                        Object JavaDoc parent,
103                        String JavaDoc namespaceUri,
104                        String JavaDoc name)
105    {
106       Class JavaDoc providerClass = provider.getClass();
107       Class JavaDoc parentClass = parent.getClass();
108       String JavaDoc methodName = "getElementValue";
109
110       Object JavaDoc value = null;
111       Method JavaDoc method = getProviderMethod(providerClass,
112          methodName,
113          new Class JavaDoc[]{parentClass, String JavaDoc.class, String JavaDoc.class}
114       );
115       if(method != null)
116       {
117          try
118          {
119             value = method.invoke(provider, new Object JavaDoc[]{parent, namespaceUri, name});
120          }
121          catch(Exception JavaDoc e)
122          {
123             throw new IllegalStateException JavaDoc("Failed to invoke method " + methodName);
124          }
125       }
126       else if(trace)
127       {
128          log.trace("No " + methodName + " for " + name);
129       }
130       return value;
131    }
132
133    Object JavaDoc provideAttributeValue(ObjectModelProvider provider,
134                                 Object JavaDoc object,
135                                 String JavaDoc namespaceUri,
136                                 String JavaDoc name)
137    {
138       Class JavaDoc providerClass = provider.getClass();
139       Class JavaDoc objectClass = object.getClass();
140       String JavaDoc methodName = "getAttributeValue";
141
142       Object JavaDoc value = null;
143       Method JavaDoc method = getProviderMethod(providerClass,
144          methodName,
145          new Class JavaDoc[]{objectClass, String JavaDoc.class, String JavaDoc.class}
146       );
147       if(method != null)
148       {
149          try
150          {
151             value = method.invoke(provider, new Object JavaDoc[]{object, namespaceUri, name});
152          }
153          catch(Exception JavaDoc e)
154          {
155             throw new IllegalStateException JavaDoc("Failed to invoke method " + methodName);
156          }
157       }
158       else if(trace)
159       {
160          log.trace("No " + methodName + " for " + name);
161       }
162       return value;
163    }
164
165    private static Method JavaDoc getProviderMethod(Class JavaDoc providerClass, String JavaDoc methodName, Class JavaDoc[] args)
166    {
167       Method JavaDoc method = null;
168       try
169       {
170          method = providerClass.getMethod(methodName, args);
171       }
172       catch(NoSuchMethodException JavaDoc e)
173       {
174          // no method
175
}
176       return method;
177    }
178 }
179
Popular Tags