ohair@286: /*
ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package com.sun.xml.internal.bind.v2.runtime;
ohair@286:
ohair@286: import java.util.HashMap;
ohair@286:
ohair@286: import javax.xml.bind.ValidationEvent;
ohair@286: import javax.xml.bind.ValidationEventHandler;
ohair@286: import javax.xml.bind.ValidationEventLocator;
ohair@286: import javax.xml.bind.annotation.adapters.XmlAdapter;
ohair@286: import javax.xml.bind.helpers.ValidationEventImpl;
ohair@286:
ohair@286: import com.sun.xml.internal.bind.v2.ClassFactory;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286:
ohair@286: import org.xml.sax.ErrorHandler;
ohair@286: import org.xml.sax.SAXException;
ohair@286: import org.xml.sax.SAXParseException;
ohair@286:
ohair@286: /**
ohair@286: * Object that coordinates the marshalling/unmarshalling.
ohair@286: *
ohair@286: *
ohair@286: * This class takes care of the logic that allows code to obtain
ohair@286: * {@link UnmarshallingContext} and {@link XMLSerializer} instances
ohair@286: * during the unmarshalling/marshalling.
ohair@286: *
ohair@286: *
ohair@286: * This is done by using a {@link ThreadLocal}. Therefore one unmarshalling/marshalling
ohair@286: * episode has to be done from the beginning till end by the same thread.
ohair@286: * (Note that the same {@link Coordinator} can be then used by a different thread
ohair@286: * for an entirely different episode.)
ohair@286: *
ohair@286: * This class also maintains the user-configured instances of {@link XmlAdapter}s.
ohair@286: *
ohair@286: *
ohair@286: * This class implements {@link ErrorHandler} and propages erros to this object
ohair@286: * as the {@link ValidationEventHandler}, which will be implemented in a derived class.
ohair@286: *
ohair@286: * @author Kohsuke Kawaguchi
ohair@286: */
ohair@286: public abstract class Coordinator implements ErrorHandler, ValidationEventHandler {
ohair@286:
ohair@286: private final HashMap,XmlAdapter> adapters =
ohair@286: new HashMap,XmlAdapter>();
ohair@286:
ohair@286:
ohair@286: public final XmlAdapter putAdapter(Class extends XmlAdapter> c, XmlAdapter a) {
ohair@286: if(a==null)
ohair@286: return adapters.remove(c);
ohair@286: else
ohair@286: return adapters.put(c,a);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets the instance of the adapter.
ohair@286: *
ohair@286: * @return
ohair@286: * always non-null.
ohair@286: */
ohair@286: public final T getAdapter(Class key) {
ohair@286: T v = key.cast(adapters.get(key));
ohair@286: if(v==null) {
ohair@286: v = ClassFactory.create(key);
ohair@286: putAdapter(key,v);
ohair@286: }
ohair@286: return v;
ohair@286: }
ohair@286:
ohair@286: public boolean containsAdapter(Class type) {
ohair@286: return adapters.containsKey(type);
ohair@286: }
ohair@286:
mkos@397: // this much is necessary to avoid calling get and set twice when we push.
mkos@397: private static final ThreadLocal activeTable = new ThreadLocal();
mkos@397:
ohair@286: /**
ohair@286: * The {@link Coordinator} in charge before this {@link Coordinator}.
ohair@286: */
mkos@397: private Coordinator old;
ohair@286:
ohair@286: /**
ohair@286: * Called whenever an execution flow enters the realm of this {@link Coordinator}.
ohair@286: */
ohair@286: protected final void pushCoordinator() {
mkos@397: old = activeTable.get();
mkos@397: activeTable.set(this);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Called whenever an execution flow exits the realm of this {@link Coordinator}.
ohair@286: */
ohair@286: protected final void popCoordinator() {
mkos@397: if (old != null)
mkos@397: activeTable.set(old);
mkos@397: else
mkos@397: activeTable.remove();
ohair@286: old = null; // avoid memory leak
ohair@286: }
ohair@286:
ohair@286: public static Coordinator _getInstance() {
mkos@397: return activeTable.get();
ohair@286: }
ohair@286:
ohair@286: //
ohair@286: //
ohair@286: // ErrorHandler implementation
ohair@286: //
ohair@286: //
ohair@286: /**
ohair@286: * Gets the current location. Used for reporting the error source location.
ohair@286: */
ohair@286: protected abstract ValidationEventLocator getLocation();
ohair@286:
ohair@286: public final void error(SAXParseException exception) throws SAXException {
ohair@286: propagateEvent( ValidationEvent.ERROR, exception );
ohair@286: }
ohair@286:
ohair@286: public final void warning(SAXParseException exception) throws SAXException {
ohair@286: propagateEvent( ValidationEvent.WARNING, exception );
ohair@286: }
ohair@286:
ohair@286: public final void fatalError(SAXParseException exception) throws SAXException {
ohair@286: propagateEvent( ValidationEvent.FATAL_ERROR, exception );
ohair@286: }
ohair@286:
ohair@286: private void propagateEvent( int severity, SAXParseException saxException )
ohair@286: throws SAXException {
ohair@286:
ohair@286: ValidationEventImpl ve =
ohair@286: new ValidationEventImpl( severity, saxException.getMessage(), getLocation() );
ohair@286:
ohair@286: Exception e = saxException.getException();
ohair@286: if( e != null ) {
ohair@286: ve.setLinkedException( e );
ohair@286: } else {
ohair@286: ve.setLinkedException( saxException );
ohair@286: }
ohair@286:
ohair@286: // call the client's event handler. If it returns false, then bail-out
ohair@286: // and terminate the unmarshal operation.
ohair@286: boolean result = handleEvent( ve );
ohair@286: if( ! result ) {
ohair@286: // bail-out of the parse with a SAX exception, but convert it into
ohair@286: // an UnmarshalException back in in the AbstractUnmarshaller
ohair@286: throw saxException;
ohair@286: }
ohair@286: }
ohair@286: }