KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > xslt > DOMAdapter


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.xslt;
6
7 import org.w3c.dom.Node JavaDoc;
8
9 import java.lang.reflect.Constructor JavaDoc;
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.util.Collection JavaDoc;
12
13
14 /**
15  * @author <a HREF="mailto:meier@meisterbohne.de">Philipp Meier</a>
16  * Date: 10.10.2003
17  * Time: 19:39:13
18  */

19 public class DOMAdapter {
20     //~ Methods ////////////////////////////////////////////////////////////////
21

22     public short getNodeType() {
23         return Node.PROCESSING_INSTRUCTION_NODE; // What to return, is DOMAdapter a Node at last?
24
}
25
26     public String JavaDoc getPropertyName() {
27         return "[Root]";
28     }
29
30     public AdapterNode adapt(DOMAdapter rootAdapter, Node JavaDoc parent, String JavaDoc propertyName, Object JavaDoc value) {
31         Class JavaDoc klass = value.getClass();
32         Class JavaDoc adapterClass = findAdapterForClass(klass);
33
34         try {
35             if (adapterClass == null) {
36                 if (klass.isArray()) {
37                     adapterClass = ArrayAdapter.class;
38                 } else if (String JavaDoc.class.isAssignableFrom(klass) || klass.isPrimitive() || Number JavaDoc.class.isAssignableFrom(klass)) {
39                     adapterClass = ToStringAdapter.class;
40                 } else if (Collection JavaDoc.class.isAssignableFrom(klass)) {
41                     adapterClass = CollectionAdapter.class;
42                 } else {
43                     adapterClass = BeanAdapter.class;
44                 }
45             }
46
47             Constructor JavaDoc c = adapterClass.getConstructor(new Class JavaDoc[]{
48                 DOMAdapter.class, AdapterNode.class, String JavaDoc.class,
49                 Object JavaDoc.class
50             });
51             AdapterNode adapter = ((AdapterNode) c.newInstance(new Object JavaDoc[]{
52                 this, parent, propertyName, value
53             }));
54
55             return adapter;
56         } catch (IllegalAccessException JavaDoc e) {
57             e.printStackTrace();
58             throw new RuntimeException JavaDoc("Cannot adapt " + value + " (" + propertyName + ") :" + e.getMessage());
59         } catch (InstantiationException JavaDoc e) {
60             e.printStackTrace();
61             throw new RuntimeException JavaDoc("Cannot adapt " + value + " (" + propertyName + ") :" + e.getMessage());
62         } catch (NoSuchMethodException JavaDoc e) {
63             e.printStackTrace();
64             throw new RuntimeException JavaDoc("Adapter Class " + adapterClass.getName() + " must define the right constructor. :" + e.getMessage());
65         } catch (InvocationTargetException JavaDoc e) {
66             e.printStackTrace();
67             throw new RuntimeException JavaDoc("Cannot adapt " + value + " (" + propertyName + ") :" + e.getMessage());
68         }
69     }
70
71     public AdapterNode adapt(Object JavaDoc value) throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
72         return new DocumentAdapter(this, null, "result", value);
73     }
74
75     public AdapterNode adaptNullValue(DOMAdapter rootAdapter, BeanAdapter parent, String JavaDoc propertyName) {
76         return new ToStringAdapter(rootAdapter, parent, propertyName, "null");
77     }
78
79     //TODO: implement Configuration option to provide additional adapter classes
80
private Class JavaDoc findAdapterForClass(Class JavaDoc klass) {
81         return null;
82     }
83 }
84
Popular Tags