01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.xml.internal.bind.v2.runtime;
27:
28: import java.awt.*;
29: import java.io.IOException;
30:
31: import javax.activation.MimeType;
32: import javax.xml.stream.XMLStreamException;
33:
34: import com.sun.xml.internal.bind.api.AccessorException;
35:
36: import org.xml.sax.SAXException;
37:
38: /**
39: * {@link Transducer} decorator that wraps another {@link Transducer}
40: * and sets the expected MIME type to the context.
41: *
42: * <p>
43: * Combined with {@link Transducer} implementations (such as one for {@link Image}),
44: * this is used to control the marshalling of the BLOB types.
45: *
46: * @author Kohsuke Kawaguchi
47: */
48: public final class MimeTypedTransducer<V> extends FilterTransducer<V> {
49: private final MimeType expectedMimeType;
50:
51: public MimeTypedTransducer(Transducer<V> core,
52: MimeType expectedMimeType) {
53: super (core);
54: this .expectedMimeType = expectedMimeType;
55: }
56:
57: @Override
58: public CharSequence print(V o) throws AccessorException {
59: XMLSerializer w = XMLSerializer.getInstance();
60: MimeType old = w.setExpectedMimeType(expectedMimeType);
61: try {
62: return core.print(o);
63: } finally {
64: w.setExpectedMimeType(old);
65: }
66: }
67:
68: @Override
69: public void writeText(XMLSerializer w, V o, String fieldName)
70: throws IOException, SAXException, XMLStreamException,
71: AccessorException {
72: MimeType old = w.setExpectedMimeType(expectedMimeType);
73: try {
74: core.writeText(w, o, fieldName);
75: } finally {
76: w.setExpectedMimeType(old);
77: }
78: }
79:
80: @Override
81: public void writeLeafElement(XMLSerializer w, Name tagName, V o,
82: String fieldName) throws IOException, SAXException,
83: XMLStreamException, AccessorException {
84: MimeType old = w.setExpectedMimeType(expectedMimeType);
85: try {
86: core.writeLeafElement(w, tagName, o, fieldName);
87: } finally {
88: w.setExpectedMimeType(old);
89: }
90: }
91: }
|