KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xml > binding > SchemalessMarshaller


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xml.binding;
8
9 import org.jboss.logging.Logger;
10 import org.xml.sax.SAXException JavaDoc;
11
12 import java.io.StringWriter JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Properties JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Array JavaDoc;
22
23 /**
24  * Sandbox. Very testcase specific impl.
25  *
26  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
27  * @version <tt>$Revision: 1.1.2.6 $</tt>
28  */

29 public class SchemalessMarshaller
30 {
31    private static final Logger log = Logger.getLogger(SchemalessMarshaller.class);
32
33    public static final String JavaDoc PROPERTY_JAXB_SCHEMA_LOCATION = "jaxb.schemaLocation";
34
35    private final Properties JavaDoc props = new Properties JavaDoc();
36
37    private final Map JavaDoc gettersPerClass = new HashMap JavaDoc();
38
39    private final Content content = new Content();
40
41    public void setProperty(String JavaDoc name, String JavaDoc value)
42    {
43       props.setProperty(name, value);
44    }
45
46    public void marshal(Object JavaDoc root, StringWriter JavaDoc writer)
47    {
48       log.debug("marshal: root=" + root);
49
50       content.startDocument();
51
52       marshalObject(root, root.getClass().getName(), writer);
53
54       content.endDocument();
55
56       writer.write("<?xml version=\"");
57       writer.write("1.0");
58       writer.write("\" encoding=\"");
59       writer.write("UTF-8");
60       writer.write("\"?>\n");
61
62       ContentWriter contentWriter = new ContentWriter(writer, true);
63       try
64       {
65          content.handleContent(contentWriter);
66       }
67       catch(SAXException JavaDoc e)
68       {
69          log.error("Failed to write content.", e);
70          throw new IllegalStateException JavaDoc("Failed to write content: " + e.getMessage());
71       }
72    }
73
74    private void marshalObject(Object JavaDoc root, String JavaDoc localName, StringWriter JavaDoc writer)
75    {
76       List JavaDoc getters = getGetterList(root.getClass());
77       AttributesImpl attrs = null; //new AttributesImpl(5);
78
content.startElement(null, localName, localName, attrs);
79
80       for(int i = 0; i < getters.size(); ++i)
81       {
82          Method JavaDoc getter = (Method JavaDoc)getters.get(i);
83          Object JavaDoc child;
84          try
85          {
86             child = getter.invoke(root, null);
87          }
88          catch(Exception JavaDoc e)
89          {
90             log.error("Failed to invoke getter " + getter.getName() + " on " + root, e);
91             throw new IllegalStateException JavaDoc(
92                "Failed to invoke getter " + getter.getName() + " on " + root + ": " + e.getMessage()
93             );
94          }
95
96          if(child != null)
97          {
98             String JavaDoc childName = getter.getName().substring(3);
99             if(isAttributeType(child.getClass()))
100             {
101                marshalAttributeType(childName, child);
102
103                /*
104                attrs.add(null,
105                   getter.getName().substring(3),
106                   getter.getName().substring(3),
107                   getter.getClass().getName(),
108                   child.toString()
109                );
110                */

111             }
112             else if(child.getClass().isArray())
113             {
114                content.startElement(null, childName, childName, null);
115                for(int arrInd = 0; arrInd < Array.getLength(child); ++arrInd)
116                {
117                   Object JavaDoc o = Array.get(child, arrInd);
118                   marshalCollectionItem(o, o.getClass().getName(), o.getClass().getName(), writer);
119                }
120                content.endElement(null, childName, childName);
121             }
122             else if(Collection JavaDoc.class.isAssignableFrom(child.getClass()))
123             {
124                content.startElement(null, childName, childName, null);
125                Collection JavaDoc col = (Collection JavaDoc)child;
126                for(Iterator JavaDoc iter = col.iterator(); iter.hasNext();)
127                {
128                   Object JavaDoc o = iter.next();
129                   marshalCollectionItem(o, o.getClass().getName(), o.getClass().getName(), writer);
130                }
131                content.endElement(null, childName, childName);
132             }
133             else
134             {
135                marshalObject(child, childName, writer);
136             }
137          }
138       }
139
140       content.endElement(null, localName, localName);
141    }
142
143    private void marshalCollectionItem(Object JavaDoc o, String JavaDoc childName, String JavaDoc qName, StringWriter JavaDoc writer)
144    {
145       if(o != null)
146       {
147          if(isAttributeType(o.getClass()))
148          {
149             marshalAttributeType(childName, o);
150          }
151          else
152          {
153             marshalObject(o, qName, writer);
154          }
155       }
156    }
157
158    private void marshalAttributeType(String JavaDoc qName, Object JavaDoc child)
159    {
160       content.startElement(null, qName, qName, null);
161       String JavaDoc value = child.toString();
162       content.characters(value.toCharArray(), 0, value.length());
163       content.endElement(null, qName, qName);
164    }
165
166    private List JavaDoc getGetterList(Class JavaDoc aClass)
167    {
168       List JavaDoc getters = (List JavaDoc)gettersPerClass.get(aClass);
169       if(getters == null)
170       {
171          getters = new ArrayList JavaDoc();
172          Method JavaDoc[] methods = aClass.getMethods();
173          for(int i = 0; i < methods.length; ++i)
174          {
175             Method JavaDoc method = methods[i];
176             if(method.getDeclaringClass() != Object JavaDoc.class)
177             {
178                if((method.getName().startsWith("get") || method.getName().startsWith("is")) &&
179                   (method.getParameterTypes() == null || method.getParameterTypes().length == 0))
180                {
181                   getters.add(method);
182                }
183             }
184          }
185          gettersPerClass.put(aClass, getters);
186       }
187       return getters;
188    }
189
190    static boolean isAttributeType(Class JavaDoc cls)
191    {
192       if(cls.isPrimitive() ||
193          cls == Byte JavaDoc.class ||
194          cls == Short JavaDoc.class ||
195          cls == Integer JavaDoc.class ||
196          cls == Long JavaDoc.class ||
197          cls == Float JavaDoc.class ||
198          cls == Double JavaDoc.class ||
199          cls == Character JavaDoc.class ||
200          cls == Boolean JavaDoc.class ||
201          cls == String JavaDoc.class ||
202          cls == java.util.Date JavaDoc.class)
203       {
204          return true;
205       }
206       else
207       {
208          return false;
209       }
210    }
211 }
212
Popular Tags