aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.api; aoqi@0: aoqi@0: import javax.xml.bind.JAXBContext; aoqi@0: import javax.xml.bind.Unmarshaller; aoqi@0: import javax.xml.bind.ValidationEventHandler; aoqi@0: import javax.xml.bind.annotation.XmlAnyElement; aoqi@0: aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: aoqi@0: /** aoqi@0: * Dynamically locates classes to represent elements discovered during the unmarshalling. aoqi@0: * aoqi@0: *

aoqi@0: * THIS INTERFACE IS SUBJECT TO CHANGE WITHOUT NOTICE. aoqi@0: * aoqi@0: *

Background

aoqi@0: *

aoqi@0: * {@link JAXBContext#newInstance(Class...)} requires that application informs JAXB aoqi@0: * about all the classes that it may see in the instance document. While this allows aoqi@0: * JAXB to take time to optimize the unmarshalling, it is sometimes inconvenient aoqi@0: * for applications. aoqi@0: * aoqi@0: *

aoqi@0: * This is where {@link ClassResolver} comes to resucue. aoqi@0: * aoqi@0: *

aoqi@0: * A {@link ClassResolver} instance can be specified on {@link Unmarshaller} via aoqi@0: * {@link Unmarshaller#setProperty(String, Object)} as follows: aoqi@0: * aoqi@0: *

aoqi@0:  * unmarshaller.setProperty( ClassResolver.class.getName(), new MyClassResolverImpl() );
aoqi@0:  * 
aoqi@0: * aoqi@0: *

aoqi@0: * When an {@link Unmarshaller} encounters (i) an unknown root element or (ii) unknown aoqi@0: * elements where unmarshaller is trying to unmarshal into {@link XmlAnyElement} with aoqi@0: * lax=true, unmarshaller calls {@link #resolveElementName(String, String)} aoqi@0: * method to see if the application may be able to supply a class that corresponds aoqi@0: * to that class. aoqi@0: * aoqi@0: *

aoqi@0: * When a {@link Class} is returned, a new {@link JAXBContext} is created with aoqi@0: * all the classes known to it so far, plus a new class returned. This operation aoqi@0: * may fail (for example because of some conflicting annotations.) This failure aoqi@0: * is handled just like {@link Exception}s thrown from aoqi@0: * {@link ClassResolver#resolveElementName(String, String)}. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: * @since 2.1 aoqi@0: */ aoqi@0: public abstract class ClassResolver { aoqi@0: /** aoqi@0: * JAXB calls this method when it sees an unknown element. aoqi@0: * aoqi@0: *

aoqi@0: * See the class javadoc for details. aoqi@0: * aoqi@0: * @param nsUri aoqi@0: * Namespace URI of the unknown element. Can be empty but never null. aoqi@0: * @param localName aoqi@0: * Local name of the unknown element. Never be empty nor null. aoqi@0: * aoqi@0: * @return aoqi@0: * If a non-null class is returned, it will be used to unmarshal this element. aoqi@0: * If null is returned, the resolution is assumed to be failed, and aoqi@0: * the unmarshaller will behave as if there was no {@link ClassResolver} aoqi@0: * to begin with (that is, to report it to {@link ValidationEventHandler}, aoqi@0: * then move on.) aoqi@0: * aoqi@0: * @throws Exception aoqi@0: * Throwing any {@link RuntimeException} causes the unmarshaller to stop aoqi@0: * immediately. The exception will be propagated up the call stack. aoqi@0: * Throwing any other checked {@link Exception} results in the error aoqi@0: * reproted to {@link ValidationEventHandler} (just like any other error aoqi@0: * during the unmarshalling.) aoqi@0: */ aoqi@0: public abstract @Nullable Class resolveElementName(@NotNull String nsUri, @NotNull String localName) throws Exception; aoqi@0: }