1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.bind.v2.runtime;
27
28import java.io.IOException;
29
30import javax.xml.bind.annotation.XmlSchemaType;
31import javax.xml.namespace.QName;
32import javax.xml.stream.XMLStreamException;
33
34import com.sun.xml.internal.bind.api.AccessorException;
35
36import org.xml.sax.SAXException;
37
38/**
39 * {@link Transducer} that signals the runtime that this datatype
40 * is marshalled to a different XML Schema type.
41 *
42 * <p>
43 * This transducer is used to implement the semantics of {@link XmlSchemaType} annotation.
44 *
45 *
46 * @see XMLSerializer#schemaType
47 * @author Kohsuke Kawaguchi
48 */
49public class SchemaTypeTransducer<V> extends FilterTransducer<V> {
50    private final QName schemaType;
51
52    public SchemaTypeTransducer(Transducer<V> core, QName schemaType) {
53        super(core);
54        this.schemaType = schemaType;
55    }
56
57    @Override
58    public CharSequence print(V o) throws AccessorException {
59        XMLSerializer w = XMLSerializer.getInstance();
60        QName old = w.setSchemaType(schemaType);
61        try {
62            return core.print(o);
63        } finally {
64            w.setSchemaType(old);
65        }
66    }
67
68    @Override
69    public void writeText(XMLSerializer w, V o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
70        QName old = w.setSchemaType(schemaType);
71        try {
72            core.writeText(w,o,fieldName);
73        } finally {
74            w.setSchemaType(old);
75        }
76    }
77
78    @Override
79    public void writeLeafElement(XMLSerializer w, Name tagName, V o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
80        QName old = w.setSchemaType(schemaType);
81        try {
82            core.writeLeafElement(w, tagName, o, fieldName);
83        } finally {
84            w.setSchemaType(old);
85        }
86    }
87}
88