com.sun.xml.bind.v2.runtime.MimeTypedTransducer Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.bind.v2.runtime;
import java.awt.*;
import java.io.IOException;
import javax.activation.MimeType;
import javax.xml.stream.XMLStreamException;
import com.sun.xml.bind.api.AccessorException;
import org.xml.sax.SAXException;
/**
* {@link Transducer} decorator that wraps another {@link Transducer}
* and sets the expected MIME type to the context.
*
*
* Combined with {@link Transducer} implementations (such as one for {@link Image}),
* this is used to control the marshalling of the BLOB types.
*
* @author Kohsuke Kawaguchi
*/
public final class MimeTypedTransducer extends FilterTransducer {
private final MimeType expectedMimeType;
public MimeTypedTransducer(Transducer core,MimeType expectedMimeType) {
super(core);
this.expectedMimeType = expectedMimeType;
}
@Override
public CharSequence print(V o) throws AccessorException {
XMLSerializer w = XMLSerializer.getInstance();
MimeType old = w.setExpectedMimeType(expectedMimeType);
try {
return core.print(o);
} finally {
w.setExpectedMimeType(old);
}
}
@Override
public void writeText(XMLSerializer w, V o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
MimeType old = w.setExpectedMimeType(expectedMimeType);
try {
core.writeText(w, o, fieldName);
} finally {
w.setExpectedMimeType(old);
}
}
@Override
public void writeLeafElement(XMLSerializer w, Name tagName, V o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
MimeType old = w.setExpectedMimeType(expectedMimeType);
try {
core.writeLeafElement(w, tagName, o, fieldName);
} finally {
w.setExpectedMimeType(old);
}
}
}