KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jaxb > MarshallerImpl


1 /*
2 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3 *
4 * This file is part of Resin(R) Open Source
5 *
6 * Each copy or derived work must preserve the copyright notice and this
7 * notice unmodified.
8 *
9 * Resin Open Source is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * Resin Open Source is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17 * of NON-INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Resin Open Source; if not, write to the
22 *
23 * Free Software Foundation, Inc.
24 * 59 Temple Place, Suite 330
25 * Boston, MA 02111-1307 USA
26 *
27 * @author Adam Megacz
28 */

29
30 package com.caucho.jaxb;
31 import com.caucho.jaxb.adapters.BeanAdapter;
32 import com.caucho.jaxb.skeleton.ClassSkeleton;
33
34 import javax.xml.bind.JAXBException;
35 import javax.xml.bind.Marshaller;
36 import javax.xml.bind.annotation.XmlRootElement;
37 import javax.xml.bind.annotation.XmlType;
38 import javax.xml.bind.annotation.adapters.XmlAdapter;
39 import javax.xml.bind.helpers.AbstractMarshallerImpl;
40 import javax.xml.namespace.QName JavaDoc;
41 import javax.xml.stream.XMLEventWriter;
42 import javax.xml.stream.XMLOutputFactory;
43 import javax.xml.stream.XMLStreamException;
44 import javax.xml.stream.XMLStreamWriter;
45 import javax.xml.transform.Result JavaDoc;
46
47 public class MarshallerImpl extends AbstractMarshallerImpl {
48
49   private JAXBContextImpl _context;
50   private Listener _listener;
51   
52
53   MarshallerImpl(JAXBContextImpl context)
54   {
55     this._context = context;
56   }
57
58   /**
59    * Marshal the content tree rooted at jaxbElement into a .
60    */

61   public void marshal(Object JavaDoc jaxbElement, XMLStreamWriter writer)
62     throws JAXBException
63   {
64     ClassSkeleton skeleton = _context.findSkeletonForObject(jaxbElement);
65     Class JavaDoc c = skeleton.getType();
66
67     /* tck/JAXBMarshall
68     if (! _context.createJAXBIntrospector().isElement(jaxbElement) &&
69         ! c.isAnnotationPresent(XmlRootElement.class))
70       throw new MarshalException("JAXBIntrospector.isElement()==false");
71     */

72
73     String JavaDoc name = null;
74     String JavaDoc namespace = null;
75
76     /// XXX: put into skeleton?
77
XmlType xmlTypeAnnotation = (XmlType) c.getAnnotation(XmlType.class);
78
79     if (name == null) {
80       name =
81         (xmlTypeAnnotation == null ? c.getName() : xmlTypeAnnotation.name());
82     }
83
84     XmlRootElement xre = (XmlRootElement) c.getAnnotation(XmlRootElement.class);
85
86     if (xre != null) {
87       name = xre.name();
88     }
89
90     String JavaDoc encoding = getEncoding();
91     if (encoding == null)
92       encoding = "utf-8";
93
94     try {
95       if (!isFragment())
96         writer.writeStartDocument(encoding, "1.0");
97
98       // XXX this needs to happen after the startElement is written
99
// jaxb/5003
100
/*
101       if (getNoNSSchemaLocation() != null)
102         writer.writeAttribute("xsi",
103                               "http://www.w3.org/2001/XMLSchema-instance",
104                               "noNamespaceSchemaLocation",
105                               getNoNSSchemaLocation());
106       */

107
108
109       skeleton.write(this, writer, jaxbElement, null);
110     }
111     catch (Exception JavaDoc e) {
112       throw new JAXBException(e);
113     }
114   }
115
116   public Listener getListener()
117   {
118     return _listener;
119   }
120
121   public void setListener(Marshaller.Listener listener)
122   {
123     _listener = listener;
124   }
125
126   public void marshal(Object JavaDoc obj, XMLEventWriter writer) throws JAXBException
127   {
128     throw new UnsupportedOperationException JavaDoc();
129   }
130
131   public void marshal(Object JavaDoc obj, Result JavaDoc result) throws JAXBException
132   {
133     try {
134       XMLOutputFactory factory = XMLOutputFactory.newInstance();
135       XMLStreamWriter out = factory.createXMLStreamWriter(result);
136
137       marshal(obj, out);
138     }
139     catch (XMLStreamException e) {
140       throw new JAXBException(e);
141     }
142   }
143
144   public <A extends XmlAdapter> A getAdapter(Class JavaDoc<A> type)
145   {
146     A a = super.getAdapter(type);
147
148     if (a == null)
149       return (A)new BeanAdapter();
150
151     return a;
152   }
153 }
154
155
Popular Tags