Sun, 31 Aug 2014 16:14:36 +0400
8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.runtime.unmarshaller; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import javax.xml.namespace.NamespaceContext; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import org.xml.sax.SAXException; |
aoqi@0 | 31 | |
aoqi@0 | 32 | /** |
aoqi@0 | 33 | * Walks the XML document structure. |
aoqi@0 | 34 | * |
aoqi@0 | 35 | * Implemented by the unmarshaller and called by the API-specific connectors. |
aoqi@0 | 36 | * |
aoqi@0 | 37 | * <h2>Event Call Sequence</h2> |
aoqi@0 | 38 | * |
aoqi@0 | 39 | * The {@link XmlVisitor} expects the event callbacks in the following order: |
aoqi@0 | 40 | * <pre> |
aoqi@0 | 41 | * CALL SEQUENCE := startDocument ELEMENT endDocument |
aoqi@0 | 42 | * ELEMENT := startPrefixMapping ELEMENT endPrefixMapping |
aoqi@0 | 43 | * | startElement BODY endElement |
aoqi@0 | 44 | * BODY := text? (ELEMENT text?)* |
aoqi@0 | 45 | * </pre> |
aoqi@0 | 46 | * Note in particular that text events may not be called in a row; |
aoqi@0 | 47 | * consecutive characters (even those separated by PIs and comments) |
aoqi@0 | 48 | * must be reported as one event, unlike SAX. |
aoqi@0 | 49 | * |
aoqi@0 | 50 | * <p> |
aoqi@0 | 51 | * All namespace URIs, local names, and prefixes of element and attribute |
aoqi@0 | 52 | * names must be interned. qnames need not be interned. |
aoqi@0 | 53 | * |
aoqi@0 | 54 | * |
aoqi@0 | 55 | * <h2>Typed PCDATA</h2> |
aoqi@0 | 56 | * For efficiency, JAXB RI defines a few {@link CharSequence} implementations |
aoqi@0 | 57 | * that can be used as a parameter to the {@link #text(CharSequence)} method. |
aoqi@0 | 58 | * For example, see {@link Base64Data}. |
aoqi@0 | 59 | * |
aoqi@0 | 60 | * <h2>Error Handling</h2> |
aoqi@0 | 61 | * The visitor may throw {@link SAXException} to abort the unmarshalling process |
aoqi@0 | 62 | * in the middle. |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * @author Kohsuke Kawaguchi |
aoqi@0 | 65 | */ |
aoqi@0 | 66 | public interface XmlVisitor { |
aoqi@0 | 67 | /** |
aoqi@0 | 68 | * Notifies a start of the document. |
aoqi@0 | 69 | * |
aoqi@0 | 70 | * @param locator |
aoqi@0 | 71 | * This live object returns the location information as the parsing progresses. |
aoqi@0 | 72 | * must not be null. |
aoqi@0 | 73 | * @param nsContext |
aoqi@0 | 74 | * Some broken XML APIs can't iterate all the in-scope namespace bindings, |
aoqi@0 | 75 | * which makes it impossible to emulate {@link #startPrefixMapping(String, String)} correctly |
aoqi@0 | 76 | * when unmarshalling a subtree. Connectors that use such an API can |
aoqi@0 | 77 | * pass in additional {@link NamespaceContext} object that knows about the |
aoqi@0 | 78 | * in-scope namespace bindings. Otherwise (and normally) it is null. |
aoqi@0 | 79 | * |
aoqi@0 | 80 | * <p> |
aoqi@0 | 81 | * Ideally this object should be immutable and only represent the namespace URI bindings |
aoqi@0 | 82 | * in the context (those done above the element that JAXB started unmarshalling), |
aoqi@0 | 83 | * but it can also work even if it changes as the parsing progress (to include |
aoqi@0 | 84 | * namespaces declared on the current element being parsed.) |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException; |
aoqi@0 | 87 | void endDocument() throws SAXException; |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Notifies a start tag of a new element. |
aoqi@0 | 91 | * |
aoqi@0 | 92 | * namespace URIs and local names must be interned. |
aoqi@0 | 93 | */ |
aoqi@0 | 94 | void startElement(TagName tagName) throws SAXException; |
aoqi@0 | 95 | void endElement(TagName tagName) throws SAXException; |
aoqi@0 | 96 | |
aoqi@0 | 97 | /** |
aoqi@0 | 98 | * Called before {@link #startElement} event to notify a new namespace binding. |
aoqi@0 | 99 | */ |
aoqi@0 | 100 | void startPrefixMapping( String prefix, String nsUri ) throws SAXException; |
aoqi@0 | 101 | /** |
aoqi@0 | 102 | * Called after {@link #endElement} event to notify the end of a binding. |
aoqi@0 | 103 | */ |
aoqi@0 | 104 | void endPrefixMapping( String prefix ) throws SAXException; |
aoqi@0 | 105 | |
aoqi@0 | 106 | /** |
aoqi@0 | 107 | * Text events. |
aoqi@0 | 108 | * |
aoqi@0 | 109 | * <p> |
aoqi@0 | 110 | * The caller should consult {@link TextPredictor} to see |
aoqi@0 | 111 | * if the unmarshaller is expecting any PCDATA. If the above is returning |
aoqi@0 | 112 | * false, the caller is OK to skip any text in XML. The net effect is |
aoqi@0 | 113 | * that we can ignore whitespaces quickly. |
aoqi@0 | 114 | * |
aoqi@0 | 115 | * @param pcdata |
aoqi@0 | 116 | * represents character data. This object can be mutable |
aoqi@0 | 117 | * (such as {@link StringBuilder}); it only needs to be fixed |
aoqi@0 | 118 | * while this method is executing. |
aoqi@0 | 119 | */ |
aoqi@0 | 120 | void text( CharSequence pcdata ) throws SAXException; |
aoqi@0 | 121 | |
aoqi@0 | 122 | /** |
aoqi@0 | 123 | * Returns the {@link UnmarshallingContext} at the end of the chain. |
aoqi@0 | 124 | * |
aoqi@0 | 125 | * @return |
aoqi@0 | 126 | * always return the same object, so caching the result is recommended. |
aoqi@0 | 127 | */ |
aoqi@0 | 128 | UnmarshallingContext getContext(); |
aoqi@0 | 129 | |
aoqi@0 | 130 | /** |
aoqi@0 | 131 | * Gets the predictor that can be used for the caller to avoid |
aoqi@0 | 132 | * calling {@link #text(CharSequence)} unnecessarily. |
aoqi@0 | 133 | */ |
aoqi@0 | 134 | TextPredictor getPredictor(); |
aoqi@0 | 135 | |
aoqi@0 | 136 | interface TextPredictor { |
aoqi@0 | 137 | /** |
aoqi@0 | 138 | * Returns true if the visitor is expecting a text event as the next event. |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * <p> |
aoqi@0 | 141 | * This is primarily intended to be used for optimization to avoid buffering |
aoqi@0 | 142 | * characters unnecessarily. If this method returns false and the connector |
aoqi@0 | 143 | * sees whitespace it can safely skip it. |
aoqi@0 | 144 | * |
aoqi@0 | 145 | * <p> |
aoqi@0 | 146 | * If this method returns true, all the whitespaces are considered significant |
aoqi@0 | 147 | * and thus need to be reported as a {@link XmlVisitor#text} event. Furthermore, |
aoqi@0 | 148 | * if the element has no children (like <foo/>), then it has to be reported |
aoqi@0 | 149 | * an empty {@link XmlVisitor#text} event. |
aoqi@0 | 150 | */ |
aoqi@0 | 151 | boolean expectText(); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | } |