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 26 package com.sun.xml.internal.bind.api; 27 28 import javax.xml.bind.JAXBContext; 29 import javax.xml.bind.Unmarshaller; 30 import javax.xml.bind.ValidationEventHandler; 31 import javax.xml.bind.annotation.XmlAnyElement; 32 33 import com.sun.istack.internal.NotNull; 34 import com.sun.istack.internal.Nullable; 35 36 /** 37 * Dynamically locates classes to represent elements discovered during the unmarshalling. 38 * 39 * <p> 40 * <b>THIS INTERFACE IS SUBJECT TO CHANGE WITHOUT NOTICE.</b> 41 * 42 * <h2>Background</h2> 43 * <p> 44 * {@link JAXBContext#newInstance(Class...)} requires that application informs JAXB 45 * about all the classes that it may see in the instance document. While this allows 46 * JAXB to take time to optimize the unmarshalling, it is sometimes inconvenient 47 * for applications. 48 * 49 * <p> 50 * This is where {@link ClassResolver} comes to resucue. 51 * 52 * <p> 53 * A {@link ClassResolver} instance can be specified on {@link Unmarshaller} via 54 * {@link Unmarshaller#setProperty(String, Object)} as follows: 55 * 56 * <pre> 57 * unmarshaller.setProperty( ClassResolver.class.getName(), new MyClassResolverImpl() ); 58 * </pre> 59 * 60 * <p> 61 * When an {@link Unmarshaller} encounters (i) an unknown root element or (ii) unknown 62 * elements where unmarshaller is trying to unmarshal into {@link XmlAnyElement} with 63 * {@code lax=true}, unmarshaller calls {@link #resolveElementName(String, String)} 64 * method to see if the application may be able to supply a class that corresponds 65 * to that class. 66 * 67 * <p> 68 * When a {@link Class} is returned, a new {@link JAXBContext} is created with 69 * all the classes known to it so far, plus a new class returned. This operation 70 * may fail (for example because of some conflicting annotations.) This failure 71 * is handled just like {@link Exception}s thrown from 72 * {@link ClassResolver#resolveElementName(String, String)}. 73 * 74 * @author Kohsuke Kawaguchi 75 * @since 2.1 76 */ 77 public abstract class ClassResolver { 78 /** 79 * JAXB calls this method when it sees an unknown element. 80 * 81 * <p> 82 * See the class javadoc for details. 83 * 84 * @param nsUri 85 * Namespace URI of the unknown element. Can be empty but never null. 86 * @param localName 87 * Local name of the unknown element. Never be empty nor null. 88 * 89 * @return 90 * If a non-null class is returned, it will be used to unmarshal this element. 91 * If null is returned, the resolution is assumed to be failed, and 92 * the unmarshaller will behave as if there was no {@link ClassResolver} 93 * to begin with (that is, to report it to {@link ValidationEventHandler}, 94 * then move on.) 95 * 96 * @throws Exception 97 * Throwing any {@link RuntimeException} causes the unmarshaller to stop 98 * immediately. The exception will be propagated up the call stack. 99 * Throwing any other checked {@link Exception} results in the error 100 * reproted to {@link ValidationEventHandler} (just like any other error 101 * during the unmarshalling.) 102 */ 103 public abstract @Nullable Class<?> resolveElementName(@NotNull String nsUri, @NotNull String localName) throws Exception; 104 }