KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > parser > impl > ChildSetterImpl


1 /*
2  * Copyright 2003, 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.ws.jaxme.xs.parser.impl;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
23
24 import org.apache.ws.jaxme.xs.XSParser;
25 import org.apache.ws.jaxme.xs.parser.*;
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29
30 /** <p>Default implementation of a {@link org.apache.ws.jaxme.xs.parser.ChildSetter}.</p>
31  *
32  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
33  */

34 public class ChildSetterImpl implements ChildSetter {
35   private static final Class JavaDoc[] GETCHILDHANDLER_CLASSES = new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class, String JavaDoc.class};
36   private static final Class JavaDoc[] ZERO_CLASSES = new Class JavaDoc[]{};
37   private static final Object JavaDoc[] ZERO_OBJECTS = new Object JavaDoc[]{};
38
39   private XSContext getData() {
40     return XSParser.getRunningInstance().getContext();
41   }
42
43   /** <p>This method invokes the beans <code>pBean</code> method <code>pMethod</code> with
44    * the argument array pArgs, returning a child handler for the element <code>pQName</code>.</p>
45    */

46   protected Object JavaDoc invokeMethod(Object JavaDoc pBean, Method JavaDoc pMethod, String JavaDoc pName, Object JavaDoc[] pArgs) throws SAXException JavaDoc {
47     try {
48       return pMethod.invoke(pBean, pArgs);
49     } catch (InvocationTargetException JavaDoc e) {
50       Throwable JavaDoc t = e.getTargetException();
51       if (t instanceof RuntimeException JavaDoc) {
52         throw (RuntimeException JavaDoc) t;
53       } else if (t instanceof SAXException JavaDoc) {
54         throw (SAXException JavaDoc) t;
55       } else {
56         throw new UndeclaredThrowableException JavaDoc(t);
57       }
58     } catch (IllegalAccessException JavaDoc e) {
59       throw new IllegalStateException JavaDoc("Failed to invoke method " + pMethod.getName() +
60                                        " of class " + pMethod.getDeclaringClass() +
61                                        " with argument " + pBean + ": IllegalAccessException, " +
62                                        e.getMessage());
63     }
64   }
65
66
67   public ContentHandler JavaDoc getChildHandler(String JavaDoc pQName,
68                                          String JavaDoc pNamespaceURI, String JavaDoc pLocalName) throws SAXException JavaDoc {
69     final XsSAXParser xsSAXParser = (XsSAXParser) getData().getCurrentContentHandler();
70     if (xsSAXParser == null) {
71       throw new NullPointerException JavaDoc("No XsSAXParser registered.");
72     }
73     final Object JavaDoc bean = xsSAXParser.getBean();
74     final Class JavaDoc beanClass = bean.getClass();
75     try {
76       Method JavaDoc m = beanClass.getMethod("getChildHandler", GETCHILDHANDLER_CLASSES);
77       if (Modifier.isPublic(m.getModifiers()) &&
78           ContentHandler JavaDoc.class.isAssignableFrom(m.getReturnType())) {
79         Object JavaDoc result = (ContentHandler JavaDoc) invokeMethod(bean, m, pQName, new Object JavaDoc[]{pQName, pNamespaceURI, pLocalName});
80         if (result != null) {
81           return (ContentHandler JavaDoc) result;
82         }
83       }
84     } catch (NoSuchMethodException JavaDoc e) {
85     }
86
87     final ContentHandler JavaDoc result = getChildHandler(xsSAXParser, pQName, pLocalName);
88     if (result == null) {
89       throw new LocSAXException("Unknown child element '" + pQName + "' for bean " + bean.getClass().getName(),
90                                  getData().getLocator());
91     }
92     return result;
93   }
94
95   /** <p>Creates a new instance of {@link XsSAXParser}, inheriting most properties from
96    * its parent parser.</p>
97    */

98   protected ContentHandler JavaDoc newXsSAXParser(XsSAXParser pParent, Object JavaDoc pBean) {
99     return getData().getXsObjectFactory().newXsSAXParser(pBean);
100   }
101
102   protected ContentHandler JavaDoc getChildHandler(ContentHandler JavaDoc pParent, String JavaDoc pQName, String JavaDoc pLocalName)
103       throws SAXException JavaDoc {
104     final XsSAXParser xsSAXParser = (XsSAXParser) pParent;
105     final Object JavaDoc bean = xsSAXParser.getBean();
106     final Class JavaDoc beanClass = bean.getClass();
107     final String JavaDoc s = Character.toUpperCase(pLocalName.charAt(0)) + pLocalName.substring(1);
108     try {
109       Method JavaDoc m = beanClass.getMethod("create" + s, ZERO_CLASSES);
110       if (Modifier.isPublic(m.getModifiers()) &&
111          !void.class.equals(m.getReturnType())) {
112         Object JavaDoc result = invokeMethod(bean, m, pQName, ZERO_OBJECTS);
113         return newXsSAXParser(xsSAXParser, result);
114       }
115     } catch (NoSuchMethodException JavaDoc e) {
116     } catch (SAXException JavaDoc e) {
117       throw e;
118     }
119
120     return null;
121   }
122 }
123
Popular Tags