KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > util > CustomImplementationHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.books.publisher.impl.util;
17
18 import org.apache.xmlbeans.XmlObject;
19 import org.apache.xmlbeans.XmlCursor;
20
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25
26 public class CustomImplementationHelper {
27     /**
28      * Instantiates a class trying different constructors: first one taking an xmlObject,
29      * then one taking a Map, and finally the default constructor
30      */

31     public static Object JavaDoc instantiateComponent(Class JavaDoc clazz, XmlObject xmlObject) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, InstantiationException JavaDoc {
32         // First try constructor taking an XmlObject as argument
33
Constructor JavaDoc constructor = null;
34         try {
35             constructor = clazz.getConstructor(new Class JavaDoc[] { XmlObject.class });
36         } catch (NoSuchMethodException JavaDoc e) {
37             // ignore
38
}
39         if (constructor != null) {
40             return constructor.newInstance(new Object JavaDoc[] { xmlObject });
41         }
42
43         // Then try a constructor which takes a map as argument
44
try {
45             constructor = clazz.getConstructor(new Class JavaDoc[] { Map JavaDoc.class });
46         } catch (NoSuchMethodException JavaDoc e) {
47             // ignore
48
}
49         if (constructor != null) {
50             Map JavaDoc attributes = getAttributes(xmlObject);
51             return constructor.newInstance(new Object JavaDoc[] { attributes });
52         }
53
54         // Finally use the default constructor
55
return clazz.newInstance();
56
57     }
58
59     private static Map JavaDoc getAttributes(XmlObject xmlObject) {
60         Map JavaDoc attributes = new HashMap JavaDoc();
61         XmlCursor cursor = xmlObject.newCursor();
62         cursor.toFirstAttribute();
63         do {
64             attributes.put(cursor.getName().getLocalPart(), cursor.getTextValue());
65         } while (cursor.toNextAttribute());
66         cursor.dispose();
67         return attributes;
68     }
69 }
70
Popular Tags