Sun, 31 Aug 2014 16:14:36 +0400
8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind.v2.runtime; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.util.HashMap; |
ohair@286 | 29 | |
ohair@286 | 30 | import javax.xml.bind.ValidationEvent; |
ohair@286 | 31 | import javax.xml.bind.ValidationEventHandler; |
ohair@286 | 32 | import javax.xml.bind.ValidationEventLocator; |
ohair@286 | 33 | import javax.xml.bind.annotation.adapters.XmlAdapter; |
ohair@286 | 34 | import javax.xml.bind.helpers.ValidationEventImpl; |
ohair@286 | 35 | |
ohair@286 | 36 | import com.sun.xml.internal.bind.v2.ClassFactory; |
ohair@286 | 37 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
ohair@286 | 38 | |
ohair@286 | 39 | import org.xml.sax.ErrorHandler; |
ohair@286 | 40 | import org.xml.sax.SAXException; |
ohair@286 | 41 | import org.xml.sax.SAXParseException; |
ohair@286 | 42 | |
ohair@286 | 43 | /** |
ohair@286 | 44 | * Object that coordinates the marshalling/unmarshalling. |
ohair@286 | 45 | * |
ohair@286 | 46 | * <p> |
ohair@286 | 47 | * This class takes care of the logic that allows code to obtain |
ohair@286 | 48 | * {@link UnmarshallingContext} and {@link XMLSerializer} instances |
ohair@286 | 49 | * during the unmarshalling/marshalling. |
ohair@286 | 50 | * |
ohair@286 | 51 | * <p> |
ohair@286 | 52 | * This is done by using a {@link ThreadLocal}. Therefore one unmarshalling/marshalling |
ohair@286 | 53 | * episode has to be done from the beginning till end by the same thread. |
ohair@286 | 54 | * (Note that the same {@link Coordinator} can be then used by a different thread |
ohair@286 | 55 | * for an entirely different episode.) |
ohair@286 | 56 | * |
ohair@286 | 57 | * This class also maintains the user-configured instances of {@link XmlAdapter}s. |
ohair@286 | 58 | * |
ohair@286 | 59 | * <p> |
ohair@286 | 60 | * This class implements {@link ErrorHandler} and propages erros to this object |
ohair@286 | 61 | * as the {@link ValidationEventHandler}, which will be implemented in a derived class. |
ohair@286 | 62 | * |
ohair@286 | 63 | * @author Kohsuke Kawaguchi |
ohair@286 | 64 | */ |
ohair@286 | 65 | public abstract class Coordinator implements ErrorHandler, ValidationEventHandler { |
ohair@286 | 66 | |
ohair@286 | 67 | private final HashMap<Class<? extends XmlAdapter>,XmlAdapter> adapters = |
ohair@286 | 68 | new HashMap<Class<? extends XmlAdapter>,XmlAdapter>(); |
ohair@286 | 69 | |
ohair@286 | 70 | |
ohair@286 | 71 | public final XmlAdapter putAdapter(Class<? extends XmlAdapter> c, XmlAdapter a) { |
ohair@286 | 72 | if(a==null) |
ohair@286 | 73 | return adapters.remove(c); |
ohair@286 | 74 | else |
ohair@286 | 75 | return adapters.put(c,a); |
ohair@286 | 76 | } |
ohair@286 | 77 | |
ohair@286 | 78 | /** |
ohair@286 | 79 | * Gets the instance of the adapter. |
ohair@286 | 80 | * |
ohair@286 | 81 | * @return |
ohair@286 | 82 | * always non-null. |
ohair@286 | 83 | */ |
ohair@286 | 84 | public final <T extends XmlAdapter> T getAdapter(Class<T> key) { |
ohair@286 | 85 | T v = key.cast(adapters.get(key)); |
ohair@286 | 86 | if(v==null) { |
ohair@286 | 87 | v = ClassFactory.create(key); |
ohair@286 | 88 | putAdapter(key,v); |
ohair@286 | 89 | } |
ohair@286 | 90 | return v; |
ohair@286 | 91 | } |
ohair@286 | 92 | |
ohair@286 | 93 | public <T extends XmlAdapter> boolean containsAdapter(Class<T> type) { |
ohair@286 | 94 | return adapters.containsKey(type); |
ohair@286 | 95 | } |
ohair@286 | 96 | |
mkos@397 | 97 | // this much is necessary to avoid calling get and set twice when we push. |
mkos@397 | 98 | private static final ThreadLocal<Coordinator> activeTable = new ThreadLocal<Coordinator>(); |
mkos@397 | 99 | |
ohair@286 | 100 | /** |
ohair@286 | 101 | * The {@link Coordinator} in charge before this {@link Coordinator}. |
ohair@286 | 102 | */ |
mkos@397 | 103 | private Coordinator old; |
ohair@286 | 104 | |
ohair@286 | 105 | /** |
ohair@286 | 106 | * Called whenever an execution flow enters the realm of this {@link Coordinator}. |
ohair@286 | 107 | */ |
ohair@286 | 108 | protected final void pushCoordinator() { |
mkos@397 | 109 | old = activeTable.get(); |
mkos@397 | 110 | activeTable.set(this); |
ohair@286 | 111 | } |
ohair@286 | 112 | |
ohair@286 | 113 | /** |
ohair@286 | 114 | * Called whenever an execution flow exits the realm of this {@link Coordinator}. |
ohair@286 | 115 | */ |
ohair@286 | 116 | protected final void popCoordinator() { |
mkos@397 | 117 | if (old != null) |
mkos@397 | 118 | activeTable.set(old); |
mkos@397 | 119 | else |
mkos@397 | 120 | activeTable.remove(); |
ohair@286 | 121 | old = null; // avoid memory leak |
ohair@286 | 122 | } |
ohair@286 | 123 | |
ohair@286 | 124 | public static Coordinator _getInstance() { |
mkos@397 | 125 | return activeTable.get(); |
ohair@286 | 126 | } |
ohair@286 | 127 | |
ohair@286 | 128 | // |
ohair@286 | 129 | // |
ohair@286 | 130 | // ErrorHandler implementation |
ohair@286 | 131 | // |
ohair@286 | 132 | // |
ohair@286 | 133 | /** |
ohair@286 | 134 | * Gets the current location. Used for reporting the error source location. |
ohair@286 | 135 | */ |
ohair@286 | 136 | protected abstract ValidationEventLocator getLocation(); |
ohair@286 | 137 | |
ohair@286 | 138 | public final void error(SAXParseException exception) throws SAXException { |
ohair@286 | 139 | propagateEvent( ValidationEvent.ERROR, exception ); |
ohair@286 | 140 | } |
ohair@286 | 141 | |
ohair@286 | 142 | public final void warning(SAXParseException exception) throws SAXException { |
ohair@286 | 143 | propagateEvent( ValidationEvent.WARNING, exception ); |
ohair@286 | 144 | } |
ohair@286 | 145 | |
ohair@286 | 146 | public final void fatalError(SAXParseException exception) throws SAXException { |
ohair@286 | 147 | propagateEvent( ValidationEvent.FATAL_ERROR, exception ); |
ohair@286 | 148 | } |
ohair@286 | 149 | |
ohair@286 | 150 | private void propagateEvent( int severity, SAXParseException saxException ) |
ohair@286 | 151 | throws SAXException { |
ohair@286 | 152 | |
ohair@286 | 153 | ValidationEventImpl ve = |
ohair@286 | 154 | new ValidationEventImpl( severity, saxException.getMessage(), getLocation() ); |
ohair@286 | 155 | |
ohair@286 | 156 | Exception e = saxException.getException(); |
ohair@286 | 157 | if( e != null ) { |
ohair@286 | 158 | ve.setLinkedException( e ); |
ohair@286 | 159 | } else { |
ohair@286 | 160 | ve.setLinkedException( saxException ); |
ohair@286 | 161 | } |
ohair@286 | 162 | |
ohair@286 | 163 | // call the client's event handler. If it returns false, then bail-out |
ohair@286 | 164 | // and terminate the unmarshal operation. |
ohair@286 | 165 | boolean result = handleEvent( ve ); |
ohair@286 | 166 | if( ! result ) { |
ohair@286 | 167 | // bail-out of the parse with a SAX exception, but convert it into |
ohair@286 | 168 | // an UnmarshalException back in in the AbstractUnmarshaller |
ohair@286 | 169 | throw saxException; |
ohair@286 | 170 | } |
ohair@286 | 171 | } |
ohair@286 | 172 | } |