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.XmlValue;
31import javax.xml.datatype.XMLGregorianCalendar;
32import javax.xml.namespace.QName;
33import javax.xml.stream.XMLStreamException;
34
35import com.sun.istack.internal.NotNull;
36import com.sun.xml.internal.bind.api.AccessorException;
37import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
38import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedTransducedAccessorFactory;
39
40import org.xml.sax.SAXException;
41
42
43/**
44 * Responsible for converting a Java object to a lexical representation
45 * and vice versa.
46 *
47 * <p>
48 * An implementation of this interface hides how this conversion happens.
49 *
50 * <p>
51 * {@link Transducer}s are immutable.
52 *
53 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
54 */
55public interface Transducer<ValueT> {
56
57    /**
58     * If this {@link Transducer} is the default transducer for the <code>ValueT</code>,
59     * this method returns true.
60     *
61     * Used exclusively by {@link OptimizedTransducedAccessorFactory#get(RuntimePropertyInfo)}
62     */
63    boolean isDefault();
64
65    /**
66     * If true, this {@link Transducer} doesn't declare any namespace,
67     * and therefore {@link #declareNamespace(Object, XMLSerializer)} is no-op.
68     *
69     * It also means that the {@link #parse(CharSequence)} method
70     * won't use the context parameter.
71     */
72    boolean useNamespace();
73
74    /**
75     * Declares the namespace URIs used in the given value to {@code w}.
76     *
77     * @param o
78     *      never be null.
79     * @param w
80     *      may be null if {@code !{@link #useNamespace()}}.
81     */
82    void declareNamespace( ValueT o, XMLSerializer w ) throws AccessorException;
83
84    /**
85     * Converts the given value to its lexical representation.
86     *
87     * @param o
88     *      never be null.
89     * @return
90     *      always non-null valid lexical representation.
91     */
92    @NotNull CharSequence print(@NotNull ValueT o) throws AccessorException;
93
94    /**
95     * Converts the lexical representation to a value object.
96     *
97     * @param lexical
98     *      never be null.
99     * @throws AccessorException
100     *      if the transducer is used to parse an user bean that uses {@link XmlValue},
101     *      then this exception may occur when it tries to set the leaf value to the bean.
102     * @throws SAXException
103     *      if the lexical form is incorrect, the error should be reported
104     *      and SAXException may thrown (or it can return null to recover.)
105     */
106    ValueT parse(CharSequence lexical) throws AccessorException, SAXException;
107
108    /**
109     * Sends the result of the {@link #print(Object)} operation
110     * to one of the {@link XMLSerializer#text(String, String)} method,
111     * but with the best representation of the value, not necessarily String.
112     */
113    void writeText(XMLSerializer w, ValueT o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException;
114
115    /**
116     * Sends the result of the {@link #print(Object)} operation
117     * to one of the {@link XMLSerializer#leafElement(Name, String, String)} method.
118     * but with the best representation of the value, not necessarily String.
119     */
120    void writeLeafElement(XMLSerializer w, Name tagName, @NotNull ValueT o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException;
121
122    /**
123     * Transducers implicitly work against a single XML type,
124     * but sometimes (most notably {@link XMLGregorianCalendar},
125     * an instance may choose different XML types.
126     *
127     * @return
128     *      return non-null from this method allows transducers
129     *      to specify the type it wants to marshal to.
130     *      Most of the time this method returns null, in which case
131     *      the implicitly associated type will be used.
132     */
133    QName getTypeName(@NotNull ValueT instance);
134}
135