KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.logging.Logger;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import java.io.StringWriter JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.lang.reflect.Array JavaDoc;
37
38 /**
39  * Sandbox. Very testcase specific impl.
40  *
41  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
42  * @version <tt>$Revision: 1958 $</tt>
43  */

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

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