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.api;
038:
039: import javax.xml.bind.JAXBContext;
040: import javax.xml.bind.Unmarshaller;
041: import javax.xml.bind.ValidationEventHandler;
042: import javax.xml.bind.annotation.XmlAnyElement;
043:
044: import com.sun.istack.NotNull;
045: import com.sun.istack.Nullable;
046:
047: /**
048: * Dynamically locates classes to represent elements discovered during the unmarshalling.
049: *
050: * <p>
051: * <b>THIS INTERFACE IS SUBJECT TO CHANGE WITHOUT NOTICE.</b>
052: *
053: * <h2>Background</h2>
054: * <p>
055: * {@link JAXBContext#newInstance(Class...)} requires that application informs JAXB
056: * about all the classes that it may see in the instance document. While this allows
057: * JAXB to take time to optimize the unmarshalling, it is sometimes inconvenient
058: * for applications.
059: *
060: * <p>
061: * This is where {@link ClassResolver} comes to resucue.
062: *
063: * <p>
064: * A {@link ClassResolver} instance can be specified on {@link Unmarshaller} via
065: * {@link Unmarshaller#setProperty(String, Object)} as follows:
066: *
067: * <pre>
068: * unmarshaller.setProperty( ClassResolver.class.getName(), new MyClassResolverImpl() );
069: * </pre>
070: *
071: * <p>
072: * When an {@link Unmarshaller} encounters (i) an unknown root element or (ii) unknown
073: * elements where unmarshaller is trying to unmarshal into {@link XmlAnyElement} with
074: * <tt>lax=true</tt>, unmarshaller calls {@link #resolveElementName(String, String)}
075: * method to see if the application may be able to supply a class that corresponds
076: * to that class.
077: *
078: * <p>
079: * When a {@link Class} is returned, a new {@link JAXBContext} is created with
080: * all the classes known to it so far, plus a new class returned. This operation
081: * may fail (for example because of some conflicting annotations.) This failure
082: * is handled just like {@link Exception}s thrown from
083: * {@link ClassResolver#resolveElementName(String, String)}.
084: *
085: * @author Kohsuke Kawaguchi
086: * @since 2.1
087: */
088: public abstract class ClassResolver {
089: /**
090: * JAXB calls this method when it sees an unknown element.
091: *
092: * <p>
093: * See the class javadoc for details.
094: *
095: * @param nsUri
096: * Namespace URI of the unknown element. Can be empty but never null.
097: * @param localName
098: * Local name of the unknown element. Never be empty nor null.
099: *
100: * @return
101: * If a non-null class is returned, it will be used to unmarshal this element.
102: * If null is returned, the resolution is assumed to be failed, and
103: * the unmarshaller will behave as if there was no {@link ClassResolver}
104: * to begin with (that is, to report it to {@link ValidationEventHandler},
105: * then move on.)
106: *
107: * @throws Exception
108: * Throwing any {@link RuntimeException} causes the unmarshaller to stop
109: * immediately. The exception will be propagated up the call stack.
110: * Throwing any other checked {@link Exception} results in the error
111: * reproted to {@link ValidationEventHandler} (just like any other error
112: * during the unmarshalling.)
113: */
114: public abstract @Nullable
115: Class<?> resolveElementName(@NotNull
116: String nsUri, @NotNull
117: String localName) throws Exception;
118: }
|