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;
27
28import javax.xml.bind.Marshaller;
29
30/**
31 * Optional interface that can be implemented by JAXB-bound objects
32 * to handle cycles in the object graph.
33 *
34 * <p>
35 * As discussed in <a href="https://jaxb.dev.java.net/guide/Mapping_cyclic_references_to_XML.html">
36 * the users' guide</a>, normally a cycle in the object graph causes the marshaller to report an error,
37 * and when an error is found, the JAXB RI recovers by cutting the cycle arbitrarily.
38 * This is not always a desired behavior.
39 *
40 * <p>
41 * Implementing this interface allows user application to change this behavior.
42 * Also see <a href="http://forums.java.net/jive/thread.jspa?threadID=13670">this related discussion</a>.
43 *
44 * @since JAXB 2.1 EA2
45 * @author Kohsuke Kawaguchi
46 */
47public interface CycleRecoverable {
48    /**
49     * Called when a cycle is detected by the JAXB RI marshaller
50     * to nominate a new object to be marshalled instead.
51     *
52     * @param context
53     *      This object is provided by the JAXB RI to inform
54     *      the object about the marshalling process that's going on.
55     *
56     *
57     * @return
58     *      the object to be marshalled instead of {@code this} object.
59     *      Or return null to indicate that the JAXB RI should behave
60     *      just like when your object does not implement {@link CycleRecoverable}
61     *      (IOW, cut the cycle arbitrarily and try to go on.)
62     */
63    Object onCycleDetected(Context context);
64
65    /**
66     * This interface is implemented by the JAXB RI to provide
67     * information about the on-going marshalling process.
68     *
69     * <p>
70     * We may add more methods in the future, so please do not
71     * implement this interface in your application.
72     */
73    public interface Context {
74        /**
75         * Returns the marshaller object that's doing the marshalling.
76         *
77         * @return
78         *      always non-null.
79         */
80        Marshaller getMarshaller();
81    }
82}
83