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.v2.runtime; aoqi@0: aoqi@0: import java.util.HashMap; aoqi@0: aoqi@0: import javax.xml.bind.ValidationEvent; aoqi@0: import javax.xml.bind.ValidationEventHandler; aoqi@0: import javax.xml.bind.ValidationEventLocator; aoqi@0: import javax.xml.bind.annotation.adapters.XmlAdapter; aoqi@0: import javax.xml.bind.helpers.ValidationEventImpl; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.ClassFactory; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; aoqi@0: aoqi@0: import org.xml.sax.ErrorHandler; aoqi@0: import org.xml.sax.SAXException; aoqi@0: import org.xml.sax.SAXParseException; aoqi@0: aoqi@0: /** aoqi@0: * Object that coordinates the marshalling/unmarshalling. aoqi@0: * aoqi@0: *

aoqi@0: * This class takes care of the logic that allows code to obtain aoqi@0: * {@link UnmarshallingContext} and {@link XMLSerializer} instances aoqi@0: * during the unmarshalling/marshalling. aoqi@0: * aoqi@0: *

aoqi@0: * This is done by using a {@link ThreadLocal}. Therefore one unmarshalling/marshalling aoqi@0: * episode has to be done from the beginning till end by the same thread. aoqi@0: * (Note that the same {@link Coordinator} can be then used by a different thread aoqi@0: * for an entirely different episode.) aoqi@0: * aoqi@0: * This class also maintains the user-configured instances of {@link XmlAdapter}s. aoqi@0: * aoqi@0: *

aoqi@0: * This class implements {@link ErrorHandler} and propages erros to this object aoqi@0: * as the {@link ValidationEventHandler}, which will be implemented in a derived class. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public abstract class Coordinator implements ErrorHandler, ValidationEventHandler { aoqi@0: aoqi@0: private final HashMap,XmlAdapter> adapters = aoqi@0: new HashMap,XmlAdapter>(); aoqi@0: aoqi@0: aoqi@0: public final XmlAdapter putAdapter(Class c, XmlAdapter a) { aoqi@0: if(a==null) aoqi@0: return adapters.remove(c); aoqi@0: else aoqi@0: return adapters.put(c,a); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the instance of the adapter. aoqi@0: * aoqi@0: * @return aoqi@0: * always non-null. aoqi@0: */ aoqi@0: public final T getAdapter(Class key) { aoqi@0: T v = key.cast(adapters.get(key)); aoqi@0: if(v==null) { aoqi@0: v = ClassFactory.create(key); aoqi@0: putAdapter(key,v); aoqi@0: } aoqi@0: return v; aoqi@0: } aoqi@0: aoqi@0: public boolean containsAdapter(Class type) { aoqi@0: return adapters.containsKey(type); aoqi@0: } aoqi@0: aoqi@0: // this much is necessary to avoid calling get and set twice when we push. aoqi@0: private static final ThreadLocal activeTable = new ThreadLocal(); aoqi@0: aoqi@0: /** aoqi@0: * The {@link Coordinator} in charge before this {@link Coordinator}. aoqi@0: */ aoqi@0: private Coordinator old; aoqi@0: aoqi@0: /** aoqi@0: * Called whenever an execution flow enters the realm of this {@link Coordinator}. aoqi@0: */ aoqi@0: protected final void pushCoordinator() { aoqi@0: old = activeTable.get(); aoqi@0: activeTable.set(this); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Called whenever an execution flow exits the realm of this {@link Coordinator}. aoqi@0: */ aoqi@0: protected final void popCoordinator() { aoqi@0: if (old != null) aoqi@0: activeTable.set(old); aoqi@0: else aoqi@0: activeTable.remove(); aoqi@0: old = null; // avoid memory leak aoqi@0: } aoqi@0: aoqi@0: public static Coordinator _getInstance() { aoqi@0: return activeTable.get(); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // ErrorHandler implementation aoqi@0: // aoqi@0: // aoqi@0: /** aoqi@0: * Gets the current location. Used for reporting the error source location. aoqi@0: */ aoqi@0: protected abstract ValidationEventLocator getLocation(); aoqi@0: aoqi@0: public final void error(SAXParseException exception) throws SAXException { aoqi@0: propagateEvent( ValidationEvent.ERROR, exception ); aoqi@0: } aoqi@0: aoqi@0: public final void warning(SAXParseException exception) throws SAXException { aoqi@0: propagateEvent( ValidationEvent.WARNING, exception ); aoqi@0: } aoqi@0: aoqi@0: public final void fatalError(SAXParseException exception) throws SAXException { aoqi@0: propagateEvent( ValidationEvent.FATAL_ERROR, exception ); aoqi@0: } aoqi@0: aoqi@0: private void propagateEvent( int severity, SAXParseException saxException ) aoqi@0: throws SAXException { aoqi@0: aoqi@0: ValidationEventImpl ve = aoqi@0: new ValidationEventImpl( severity, saxException.getMessage(), getLocation() ); aoqi@0: aoqi@0: Exception e = saxException.getException(); aoqi@0: if( e != null ) { aoqi@0: ve.setLinkedException( e ); aoqi@0: } else { aoqi@0: ve.setLinkedException( saxException ); aoqi@0: } aoqi@0: aoqi@0: // call the client's event handler. If it returns false, then bail-out aoqi@0: // and terminate the unmarshal operation. aoqi@0: boolean result = handleEvent( ve ); aoqi@0: if( ! result ) { aoqi@0: // bail-out of the parse with a SAX exception, but convert it into aoqi@0: // an UnmarshalException back in in the AbstractUnmarshaller aoqi@0: throw saxException; aoqi@0: } aoqi@0: } aoqi@0: }