KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.parsers.ParserConfigurationException JavaDoc;
13 import javax.xml.namespace.QName JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
33  * @version <tt>$Revision: 1.7.2.7 $</tt>
34  */

35 public abstract class AbstractMarshaller
36    implements Marshaller
37 {
38    private static final Logger log = Logger.getLogger(AbstractMarshaller.class);
39
40    protected String JavaDoc version = VERSION;
41    protected String JavaDoc encoding = ENCODING;
42    protected List JavaDoc rootQNames = new ArrayList JavaDoc();
43
44    private Map JavaDoc classMappings = Collections.EMPTY_MAP;
45
46    private Properties JavaDoc props;
47
48    // Marshaller implementation
49

50    public void marshal(String JavaDoc schemaUri, ObjectModelProvider provider, Object JavaDoc root, Writer JavaDoc writer) throws IOException JavaDoc,
51       ParserConfigurationException JavaDoc,
52       SAXException JavaDoc
53    {
54       URL JavaDoc url = null;
55       try
56       {
57          url = new URL JavaDoc(schemaUri);
58       }
59       catch(MalformedURLException JavaDoc e)
60       {
61          throw new IllegalArgumentException JavaDoc("Malformed schema URI " + schemaUri + ": " + e.getMessage());
62       }
63
64       InputStream JavaDoc is;
65       try
66       {
67          is = url.openStream();
68       }
69       catch(IOException JavaDoc e)
70       {
71          throw new IllegalStateException JavaDoc("Failed to open input stream for schema " + schemaUri + ": " + e.getMessage());
72       }
73
74       try
75       {
76          InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(is);
77          marshal(reader, provider, root, writer);
78       }
79       finally
80       {
81          is.close();
82       }
83    }
84
85    public void mapClassToNamespace(Class JavaDoc cls,
86                                    String JavaDoc root,
87                                    String JavaDoc namespaceUri,
88                                    Reader JavaDoc schemaReader,
89                                    ObjectModelProvider provider)
90    {
91       if(classMappings == Collections.EMPTY_MAP)
92       {
93          classMappings = new HashMap JavaDoc();
94       }
95
96       ClassMapping mapping = new ClassMapping(
97          cls,
98          root,
99          namespaceUri,
100          schemaReader,
101          null,
102          provider instanceof GenericObjectModelProvider ?
103          (GenericObjectModelProvider)provider :
104          new DelegatingObjectModelProvider(provider)
105       );
106       classMappings.put(mapping.cls, mapping);
107    }
108
109    public void mapClassToNamespace(Class JavaDoc cls,
110                                    String JavaDoc root,
111                                    String JavaDoc namespaceUri,
112                                    String JavaDoc schemaUrl,
113                                    ObjectModelProvider provider)
114    {
115       if(classMappings == Collections.EMPTY_MAP)
116       {
117          classMappings = new HashMap JavaDoc();
118       }
119
120       ClassMapping mapping = new ClassMapping(
121          cls,
122          root,
123          namespaceUri,
124          null,
125          schemaUrl,
126          provider instanceof GenericObjectModelProvider ?
127          (GenericObjectModelProvider)provider :
128          new DelegatingObjectModelProvider(provider)
129       );
130       classMappings.put(mapping.cls, mapping);
131    }
132
133    public void setVersion(String JavaDoc version)
134    {
135       this.version = version;
136    }
137
138    public void setEncoding(String JavaDoc encoding)
139    {
140       this.encoding = encoding;
141    }
142
143    public void mapPublicIdToSystemId(String JavaDoc publicId, String JavaDoc systemId)
144    {
145       throw new UnsupportedOperationException JavaDoc();
146    }
147
148    public void addRootElement(String JavaDoc namespaceUri, String JavaDoc prefix, String JavaDoc name)
149    {
150       QName JavaDoc qName = new QName JavaDoc(namespaceUri, name, prefix);
151       rootQNames.add(qName);
152    }
153
154    public void setProperty(String JavaDoc name, String JavaDoc value)
155    {
156       if(props == null)
157       {
158          props = new Properties JavaDoc();
159       }
160       props.setProperty(name, value);
161    }
162
163    public String JavaDoc getProperty(String JavaDoc name)
164    {
165       return props == null ? null : props.getProperty(name);
166    }
167
168    // Protected
169

170    protected boolean propertyIsTrueOrNotSet(String JavaDoc name)
171    {
172       String JavaDoc value = getProperty(name);
173       return value == null || "true".equalsIgnoreCase(value);
174    }
175
176    protected void writeXmlVersion(Writer JavaDoc writer) throws IOException JavaDoc
177    {
178       String JavaDoc xmlVersion = getProperty(Marshaller.PROP_OUTPUT_XML_VERSION);
179       if(xmlVersion == null || "true".equalsIgnoreCase(xmlVersion))
180       {
181          writer.write("<?xml version=\"");
182          writer.write(version);
183          writer.write("\" encoding=\"");
184          writer.write(encoding);
185          writer.write("\"?>\n");
186       }
187    }
188    protected ClassMapping getClassMapping(Class JavaDoc cls)
189    {
190       return (ClassMapping)classMappings.get(cls);
191    }
192
193    static final Object JavaDoc provideChildren(ObjectModelProvider provider,
194                                        Object JavaDoc parent,
195                                        String JavaDoc namespaceUri,
196                                        String JavaDoc name)
197    {
198       Class JavaDoc providerClass = provider.getClass();
199       Class JavaDoc parentClass = parent.getClass();
200       String JavaDoc methodName = "getChildren";
201
202       Object JavaDoc container = null;
203       Method JavaDoc method = getProviderMethod(providerClass,
204          methodName,
205          new Class JavaDoc[]{parentClass, String JavaDoc.class, String JavaDoc.class}
206       );
207       if(method != null)
208       {
209          try
210          {
211             container = method.invoke(provider, new Object JavaDoc[]{parent, namespaceUri, name});
212          }
213          catch(Exception JavaDoc e)
214          {
215             log.error("Failed to invoke method " + methodName, e);
216             throw new IllegalStateException JavaDoc("Failed to invoke method " + methodName);
217          }
218       }
219       else if(log.isTraceEnabled())
220       {
221          log.trace("No " + methodName + " for " + name);
222       }
223       return container;
224    }
225
226    static final Object JavaDoc provideValue(ObjectModelProvider provider,
227                                     Object JavaDoc parent,
228                                     String JavaDoc namespaceUri,
229                                     String JavaDoc name)
230    {
231       Class JavaDoc providerClass = provider.getClass();
232       Class JavaDoc parentClass = parent.getClass();
233       String JavaDoc methodName = "getElementValue";
234
235       Object JavaDoc value = null;
236       Method JavaDoc method = getProviderMethod(providerClass,
237          methodName,
238          new Class JavaDoc[]{parentClass, String JavaDoc.class, String JavaDoc.class}
239       );
240       if(method != null)
241       {
242          try
243          {
244             value = method.invoke(provider, new Object JavaDoc[]{parent, namespaceUri, name});
245          }
246          catch(Exception JavaDoc e)
247          {
248             throw new IllegalStateException JavaDoc("Failed to invoke method " + methodName);
249          }
250       }
251       else if(log.isTraceEnabled())
252       {
253          log.trace("No " + methodName + " for " + name);
254       }
255       return value;
256    }
257
258    static final Object JavaDoc provideAttributeValue(ObjectModelProvider provider,
259                                              Object JavaDoc object,
260                                              String JavaDoc namespaceUri,
261                                              String JavaDoc name)
262    {
263       Class JavaDoc providerClass = provider.getClass();
264       Class JavaDoc objectClass = object.getClass();
265       String JavaDoc methodName = "getAttributeValue";
266
267       Object JavaDoc value = null;
268       Method JavaDoc method = getProviderMethod(providerClass,
269          methodName,
270          new Class JavaDoc[]{objectClass, String JavaDoc.class, String JavaDoc.class}
271       );
272       if(method != null)
273       {
274          try
275          {
276             value = method.invoke(provider, new Object JavaDoc[]{object, namespaceUri, name});
277          }
278          catch(Exception JavaDoc e)
279          {
280             throw new IllegalStateException JavaDoc("Failed to invoke method " + methodName);
281          }
282       }
283       else if(log.isTraceEnabled())
284       {
285          log.trace("No " + methodName + " for " + name);
286       }
287       return value;
288    }
289
290    private static final Method JavaDoc getProviderMethod(Class JavaDoc providerClass, String JavaDoc methodName, Class JavaDoc[] args)
291    {
292       Method JavaDoc method = null;
293       try
294       {
295          method = providerClass.getMethod(methodName, args);
296       }
297       catch(NoSuchMethodException JavaDoc e)
298       {
299          // no method
300
}
301       return method;
302    }
303
304    // Inner
305

306    protected class ClassMapping
307    {
308       public final Class JavaDoc cls;
309       public final String JavaDoc root;
310       public final String JavaDoc namespaceUri;
311       public final Reader JavaDoc schemaReader;
312       public final String JavaDoc schemaUrl;
313       public final GenericObjectModelProvider provider;
314
315       public ClassMapping(Class JavaDoc cls,
316                           String JavaDoc root,
317                           String JavaDoc namespaceUri,
318                           Reader JavaDoc schemaReader,
319                           String JavaDoc schemaUrl,
320                           GenericObjectModelProvider provider)
321       {
322          this.cls = cls;
323          this.root = root;
324          this.namespaceUri = namespaceUri;
325          this.schemaReader = schemaReader;
326          this.schemaUrl = schemaUrl;
327          this.provider = provider;
328       }
329
330       public boolean equals(Object JavaDoc o)
331       {
332          if(this == o)
333          {
334             return true;
335          }
336          if(!(o instanceof ClassMapping))
337          {
338             return false;
339          }
340
341          final ClassMapping classMapping = (ClassMapping)o;
342
343          if(cls != null ? !cls.equals(classMapping.cls) : classMapping.cls != null)
344          {
345             return false;
346          }
347          if(namespaceUri != null ? !namespaceUri.equals(classMapping.namespaceUri) : classMapping.namespaceUri != null)
348          {
349             return false;
350          }
351
352          return true;
353       }
354
355       public int hashCode()
356       {
357          int result;
358          result = (cls != null ? cls.hashCode() : 0);
359          result = 29 * result + (namespaceUri != null ? namespaceUri.hashCode() : 0);
360          return result;
361       }
362    }
363
364    protected static interface Stack
365    {
366       void clear();
367
368       void push(Object JavaDoc o);
369
370       Object JavaDoc pop();
371
372       Object JavaDoc peek();
373
374       boolean isEmpty();
375    }
376
377    protected static class StackImpl
378       implements Stack
379    {
380       private LinkedList JavaDoc list = new LinkedList JavaDoc();
381
382       public void clear()
383       {
384          list.clear();
385       }
386
387       public void push(Object JavaDoc o)
388       {
389          list.addLast(o);
390       }
391
392       public Object JavaDoc pop()
393       {
394          return list.removeLast();
395       }
396
397       public Object JavaDoc peek()
398       {
399          return list.getLast();
400       }
401
402       public boolean isEmpty()
403       {
404          return list.isEmpty();
405       }
406    }
407 }
408
Popular Tags