001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind;
038:
039: import java.util.concurrent.Callable;
040:
041: import javax.xml.bind.Unmarshaller;
042: import javax.xml.bind.ValidationEventHandler;
043: import javax.xml.bind.annotation.XmlIDREF;
044:
045: import org.xml.sax.SAXException;
046:
047: /**
048: * Pluggable ID/IDREF handling layer.
049: *
050: * <p>
051: * <b>THIS INTERFACE IS SUBJECT TO CHANGE WITHOUT NOTICE.</b>
052: *
053: * <p>
054: * This 'interface' can be implemented by applications and specified to
055: * {@link Unmarshaller#setProperty(String, Object)} to ovierride the ID/IDREF
056: * processing of the JAXB RI like this:
057: *
058: * <pre>
059: * unmarshaller.setProperty(IDResolver.class.getName(),new MyIDResolverImpl());
060: * </pre>
061: *
062: * <h2>Error Handling</h2>
063: * <p>
064: * This component runs inside the JAXB RI unmarshaller. Therefore, it needs
065: * to coordinate with the JAXB RI unmarshaller when it comes to reporting
066: * errors. This makes sure that applications see consistent error handling behaviors.
067: *
068: * <p>
069: * When the {@link #startDocument(ValidationEventHandler)} method is invoked,
070: * the unmarshaller passes in a {@link ValidationEventHandler} that can be used
071: * by this component to report any errors encountered during the ID/IDREF processing.
072: *
073: * <p>
074: * When an error is detected, the error should be first reported to this
075: * {@link ValidationEventHandler}. If the error is fatal or the event handler
076: * decided to abort, the implementation should throw a {@link SAXException}.
077: * This signals the unmarshaller to abort the processing.
078: *
079: * @author Kohsuke Kawaguchi
080: * @since JAXB 2.0 beta
081: */
082: public abstract class IDResolver {
083:
084: /**
085: * Called when the unmarshalling starts.
086: *
087: * <p>
088: * Since one {@link Unmarshaller} may be used multiple times
089: * to unmarshal documents, one {@link IDResolver} may be used multiple times, too.
090: *
091: * @param eventHandler
092: * Any errors found during the unmarshalling should be reported to this object.
093: */
094: public void startDocument(ValidationEventHandler eventHandler)
095: throws SAXException {
096:
097: }
098:
099: /**
100: * Called after the unmarshalling completes.
101: *
102: * <p>
103: * This is a good opporunity to reset any internal state of this object,
104: * so that it doesn't keep references to other objects unnecessarily.
105: */
106: public void endDocument() throws SAXException {
107:
108: }
109:
110: /**
111: * Binds the given object to the specified ID.
112: *
113: * <p>
114: * While a document is being unmarshalled, every time
115: * an ID value is found, this method is invoked to
116: * remember the association between ID and objects.
117: * This association is supposed to be used later to resolve
118: * IDREFs.
119: *
120: * <p>
121: * This method is invoked right away as soon as a new ID value is found.
122: *
123: * @param id
124: * The ID value found in the document being unmarshalled.
125: * Always non-null.
126: * @param obj
127: * The object being unmarshalled which is going to own the ID.
128: * Always non-null.
129: */
130: public abstract void bind(String id, Object obj)
131: throws SAXException;
132:
133: /**
134: * Obtains the object to be pointed by the IDREF value.
135: *
136: * <p>
137: * While a document is being unmarshalled, every time
138: * an IDREF value is found, this method is invoked immediately to
139: * obtain the object that the IDREF is pointing to.
140: *
141: * <p>
142: * This method returns a {@link Callable} to support forward-references.
143: * When this method returns with a non-null return value,
144: * the JAXB RI unmarshaller invokes the {@link Callable#call()} method immediately.
145: * If the implementation can find the target object (in which case
146: * it was a backward reference), then a non-null object shall be returned,
147: * and it is used as the target object.
148: *
149: * <p>
150: * When a forward-reference happens, the <tt>call</tt> method
151: * should return null. In this case the JAXB RI unmarshaller invokes
152: * the <tt>call</tt> method again after all the documents are fully unmarshalled.
153: * If the <tt>call</tt> method still returns null, then the JAXB RI unmarshaller
154: * treats it as an error.
155: *
156: * <p>
157: * A {@link Callable} object returned from this method may not throw
158: * any exception other than a {@link SAXException} (which means a fatal error.)
159: *
160: * @param id
161: * The IDREF value found in the document being unmarshalled.
162: * Always non-null.
163: * @param targetType
164: * The expected type to which ID resolves to. JAXB infers this
165: * information from the signature of the fields that has {@link XmlIDREF}.
166: * When a property is a collection, this parameter will be the type
167: * of the individual item in the collection.
168: * @return
169: * null if the implementation is sure that the parameter combination
170: * will never yield a valid object. Otherwise non-null.
171: */
172: public abstract Callable<?> resolve(String id, Class targetType)
173: throws SAXException;
174: }
|