001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.xml.bind;
031:
032: import java.util.*;
033: import java.io.*;
034: import java.net.*;
035: import java.lang.ref.*;
036:
037: import javax.xml.transform.*;
038: import javax.xml.transform.stream.*;
039:
040: public final class JAXB {
041: private static WeakHashMap<Class, SoftReference<JAXBContext>> _contextMap = new WeakHashMap<Class, SoftReference<JAXBContext>>();
042:
043: public static void marshal(Object obj, File xml) {
044: try {
045: getContext(obj).createMarshaller().marshal(obj, xml);
046: } catch (RuntimeException e) {
047: throw e;
048: } catch (Exception e) {
049: throw new DataBindingException(e);
050: }
051: }
052:
053: public static void marshal(Object obj, URL xml) {
054: try {
055: URLConnection conn = xml.openConnection();
056: conn.setDoInput(false);
057: conn.setDoOutput(false);
058: conn.connect();
059:
060: OutputStream os = conn.getOutputStream();
061: try {
062: StreamResult result = new StreamResult(os);
063:
064: marshal(obj, result);
065: } finally {
066: os.close();
067: }
068: } catch (RuntimeException e) {
069: throw e;
070: } catch (Exception e) {
071: throw new DataBindingException(e);
072: }
073: }
074:
075: public static void marshal(Object obj, URI xml) {
076: try {
077: marshal(obj, xml.toURL());
078: } catch (RuntimeException e) {
079: throw e;
080: } catch (Exception e) {
081: throw new DataBindingException(e);
082: }
083: }
084:
085: public static void marshal(Object obj, String xml) {
086: try {
087: StreamResult result = new StreamResult(xml);
088:
089: getContext(obj).createMarshaller().marshal(obj, result);
090: } catch (RuntimeException e) {
091: throw e;
092: } catch (Exception e) {
093: throw new DataBindingException(e);
094: }
095: }
096:
097: public static void marshal(Object obj, OutputStream xml) {
098: try {
099: getContext(obj).createMarshaller().marshal(obj, xml);
100: } catch (RuntimeException e) {
101: throw e;
102: } catch (Exception e) {
103: throw new DataBindingException(e);
104: }
105: }
106:
107: public static void marshal(Object obj, Writer xml) {
108: try {
109: getContext(obj).createMarshaller().marshal(obj, xml);
110: } catch (RuntimeException e) {
111: throw e;
112: } catch (Exception e) {
113: throw new DataBindingException(e);
114: }
115: }
116:
117: public static void marshal(Object obj, Result xml) {
118: try {
119: getContext(obj).createMarshaller().marshal(obj, xml);
120: } catch (RuntimeException e) {
121: throw e;
122: } catch (Exception e) {
123: throw new DataBindingException(e);
124: }
125: }
126:
127: public static <T> T unmarshal(File xml, Class<T> type) {
128: try {
129: return (T) getContext(type).createUnmarshaller().unmarshal(
130: xml);
131: } catch (RuntimeException e) {
132: throw e;
133: } catch (Exception e) {
134: throw new DataBindingException(e);
135: }
136: }
137:
138: public static <T> T unmarshal(URL url, Class<T> type) {
139: try {
140: return (T) getContext(type).createUnmarshaller().unmarshal(
141: url);
142: } catch (RuntimeException e) {
143: throw e;
144: } catch (Exception e) {
145: throw new DataBindingException(e);
146: }
147: }
148:
149: public static <T> T unmarshal(URI uri, Class<T> type) {
150: try {
151: return unmarshal(uri.toURL(), type);
152: } catch (RuntimeException e) {
153: throw e;
154: } catch (Exception e) {
155: throw new DataBindingException(e);
156: }
157: }
158:
159: public static <T> T unmarshal(String xml, Class<T> type) {
160: try {
161: StreamSource source = new StreamSource(xml);
162:
163: return (T) getContext(type).createUnmarshaller().unmarshal(
164: source);
165: } catch (RuntimeException e) {
166: throw e;
167: } catch (Exception e) {
168: throw new DataBindingException(e);
169: }
170: }
171:
172: public static <T> T unmarshal(InputStream xml, Class<T> type) {
173: try {
174: return (T) getContext(type).createUnmarshaller().unmarshal(
175: xml);
176: } catch (RuntimeException e) {
177: throw e;
178: } catch (Exception e) {
179: throw new DataBindingException(e);
180: }
181: }
182:
183: public static <T> T unmarshal(Reader xml, Class<T> type) {
184: try {
185: return (T) getContext(type).createUnmarshaller().unmarshal(
186: xml);
187: } catch (RuntimeException e) {
188: throw e;
189: } catch (Exception e) {
190: throw new DataBindingException(e);
191: }
192: }
193:
194: public static <T> T unmarshal(Source xml, Class<T> type) {
195: try {
196: return (T) getContext(type).createUnmarshaller().unmarshal(
197: xml);
198: } catch (RuntimeException e) {
199: throw e;
200: } catch (Exception e) {
201: throw new DataBindingException(e);
202: }
203: }
204:
205: private static JAXBContext getContext(Object obj)
206: throws JAXBException {
207: if (obj instanceof JAXBElement) {
208: JAXBElement elt = (JAXBElement) obj;
209:
210: return getContext(elt.getDeclaredType());
211: } else
212: return getContext(obj.getClass());
213: }
214:
215: private static JAXBContext getContext(Class cl)
216: throws JAXBException {
217: SoftReference<JAXBContext> ref = _contextMap.get(cl);
218:
219: JAXBContext context = null;
220:
221: if (ref != null)
222: context = ref.get();
223:
224: if (context == null) {
225: context = JAXBContext.newInstance(cl);
226:
227: _contextMap
228: .put(cl, new SoftReference<JAXBContext>(context));
229: }
230:
231: return context;
232: }
233: }
|