src/share/jaxws_classes/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,256 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.marshaller;
    1.30 +
    1.31 +import java.io.OutputStream;
    1.32 +
    1.33 +import javax.xml.bind.JAXBContext;
    1.34 +import javax.xml.stream.XMLEventWriter;
    1.35 +import javax.xml.stream.XMLStreamWriter;
    1.36 +import javax.xml.transform.dom.DOMResult;
    1.37 +
    1.38 +import org.w3c.dom.Node;
    1.39 +
    1.40 +// be careful about changing this class. this class is supposed to be
    1.41 +// extended by users and therefore we are not allowed to break
    1.42 +// those user code.
    1.43 +//
    1.44 +// this means:
    1.45 +// - don't add any abstract method
    1.46 +// - don't change any existing method signature
    1.47 +// - don't remove any existing method.
    1.48 +
    1.49 +/**
    1.50 + * Implemented by the user application to determine URI -> prefix
    1.51 + * mapping.
    1.52 + *
    1.53 + * This is considered as an interface, though it's implemented
    1.54 + * as an abstract class to make it easy to add new methods in
    1.55 + * a future.
    1.56 + *
    1.57 + * @author
    1.58 + *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.59 + */
    1.60 +public abstract class NamespacePrefixMapper {
    1.61 +
    1.62 +    private static final String[] EMPTY_STRING = new String[0];
    1.63 +
    1.64 +    /**
    1.65 +     * Returns a preferred prefix for the given namespace URI.
    1.66 +     *
    1.67 +     * This method is intended to be overrided by a derived class.
    1.68 +     *
    1.69 +     * <p>
    1.70 +     * As noted in the return value portion of the javadoc, there
    1.71 +     * are several cases where the preference cannot be honored.
    1.72 +     * Specifically, as of JAXB RI 2.0 and onward:
    1.73 +     *
    1.74 +     * <ol>
    1.75 +     * <li>
    1.76 +     * If the prefix returned is already in use as one of the in-scope
    1.77 +     * namespace bindings. This is partly necessary for correctness
    1.78 +     * (so that we don't unexpectedly change the meaning of QNames
    1.79 +     * bound to {@link String}), partly to simplify the marshaller.
    1.80 +     * <li>
    1.81 +     * If the prefix returned is "" yet the current {@link JAXBContext}
    1.82 +     * includes classes that use the empty namespace URI. This allows
    1.83 +     * the JAXB RI to reserve the "" prefix for the empty namespace URI,
    1.84 +     * which is the only possible prefix for the URI.
    1.85 +     * This restriction is also to simplify the marshaller.
    1.86 +     * </ol>
    1.87 +     *
    1.88 +     * @param namespaceUri
    1.89 +     *      The namespace URI for which the prefix needs to be found.
    1.90 +     *      Never be null. "" is used to denote the default namespace.
    1.91 +     * @param suggestion
    1.92 +     *      When the content tree has a suggestion for the prefix
    1.93 +     *      to the given namespaceUri, that suggestion is passed as a
    1.94 +     *      parameter. Typicall this value comes from the QName.getPrefix
    1.95 +     *      to show the preference of the content tree. This parameter
    1.96 +     *      may be null, and this parameter may represent an already
    1.97 +     *      occupied prefix.
    1.98 +     * @param requirePrefix
    1.99 +     *      If this method is expected to return non-empty prefix.
   1.100 +     *      When this flag is true, it means that the given namespace URI
   1.101 +     *      cannot be set as the default namespace.
   1.102 +     *
   1.103 +     * @return
   1.104 +     *      null if there's no prefered prefix for the namespace URI.
   1.105 +     *      In this case, the system will generate a prefix for you.
   1.106 +     *
   1.107 +     *      Otherwise the system will try to use the returned prefix,
   1.108 +     *      but generally there's no guarantee if the prefix will be
   1.109 +     *      actually used or not.
   1.110 +     *
   1.111 +     *      return "" to map this namespace URI to the default namespace.
   1.112 +     *      Again, there's no guarantee that this preference will be
   1.113 +     *      honored.
   1.114 +     *
   1.115 +     *      If this method returns "" when requirePrefix=true, the return
   1.116 +     *      value will be ignored and the system will generate one.
   1.117 +     *
   1.118 +     * @since JAXB 1.0.1
   1.119 +     */
   1.120 +    public abstract String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix);
   1.121 +
   1.122 +    /**
   1.123 +     * Returns a list of namespace URIs that should be declared
   1.124 +     * at the root element.
   1.125 +     *
   1.126 +     * <p>
   1.127 +     * By default, the JAXB RI 1.0.x produces namespace declarations only when
   1.128 +     * they are necessary, only at where they are used. Because of this
   1.129 +     * lack of look-ahead, sometimes the marshaller produces a lot of
   1.130 +     * namespace declarations that look redundant to human eyes. For example,
   1.131 +     * <pre><xmp>
   1.132 +     * <?xml version="1.0"?>
   1.133 +     * <root>
   1.134 +     *   <ns1:child xmlns:ns1="urn:foo"> ... </ns1:child>
   1.135 +     *   <ns2:child xmlns:ns2="urn:foo"> ... </ns2:child>
   1.136 +     *   <ns3:child xmlns:ns3="urn:foo"> ... </ns3:child>
   1.137 +     *   ...
   1.138 +     * </root>
   1.139 +     * </xmp></pre>
   1.140 +     *
   1.141 +     * <p>
   1.142 +     * The JAXB RI 2.x mostly doesn't exhibit this behavior any more,
   1.143 +     * as it declares all statically known namespace URIs (those URIs
   1.144 +     * that are used as element/attribute names in JAXB annotations),
   1.145 +     * but it may still declare additional namespaces in the middle of
   1.146 +     * a document, for example when (i) a QName as an attribute/element value
   1.147 +     * requires a new namespace URI, or (ii) DOM nodes as a portion of an object
   1.148 +     * tree requires a new namespace URI.
   1.149 +     *
   1.150 +     * <p>
   1.151 +     * If you know in advance that you are going to use a certain set of
   1.152 +     * namespace URIs, you can override this method and have the marshaller
   1.153 +     * declare those namespace URIs at the root element.
   1.154 +     *
   1.155 +     * <p>
   1.156 +     * For example, by returning <code>new String[]{"urn:foo"}</code>,
   1.157 +     * the marshaller will produce:
   1.158 +     * <pre><xmp>
   1.159 +     * <?xml version="1.0"?>
   1.160 +     * <root xmlns:ns1="urn:foo">
   1.161 +     *   <ns1:child> ... </ns1:child>
   1.162 +     *   <ns1:child> ... </ns1:child>
   1.163 +     *   <ns1:child> ... </ns1:child>
   1.164 +     *   ...
   1.165 +     * </root>
   1.166 +     * </xmp></pre>
   1.167 +     * <p>
   1.168 +     * To control prefixes assigned to those namespace URIs, use the
   1.169 +     * {@link #getPreferredPrefix(String, String, boolean)} method.
   1.170 +     *
   1.171 +     * @return
   1.172 +     *      A list of namespace URIs as an array of {@link String}s.
   1.173 +     *      This method can return a length-zero array but not null.
   1.174 +     *      None of the array component can be null. To represent
   1.175 +     *      the empty namespace, use the empty string <code>""</code>.
   1.176 +     *
   1.177 +     * @since
   1.178 +     *      JAXB RI 1.0.2
   1.179 +     */
   1.180 +    public String[] getPreDeclaredNamespaceUris() {
   1.181 +        return EMPTY_STRING;
   1.182 +    }
   1.183 +
   1.184 +    /**
   1.185 +     * Similar to {@link #getPreDeclaredNamespaceUris()} but allows the
   1.186 +     * (prefix,nsUri) pairs to be returned.
   1.187 +     *
   1.188 +     * <p>
   1.189 +     * With {@link #getPreDeclaredNamespaceUris()}, applications who wish to control
   1.190 +     * the prefixes as well as the namespaces needed to implement both
   1.191 +     * {@link #getPreDeclaredNamespaceUris()} and {@link #getPreferredPrefix(String, String, boolean)}.
   1.192 +     *
   1.193 +     * <p>
   1.194 +     * This version eliminates the needs by returning an array of pairs.
   1.195 +     *
   1.196 +     * @return
   1.197 +     *      always return a non-null (but possibly empty) array. The array stores
   1.198 +     *      data like (prefix1,nsUri1,prefix2,nsUri2,...) Use an empty string to represent
   1.199 +     *      the empty namespace URI and the default prefix. Null is not allowed as a value
   1.200 +     *      in the array.
   1.201 +     *
   1.202 +     * @since
   1.203 +     *      JAXB RI 2.0 beta
   1.204 +     */
   1.205 +    public String[] getPreDeclaredNamespaceUris2() {
   1.206 +        return EMPTY_STRING;
   1.207 +    }
   1.208 +
   1.209 +    /**
   1.210 +     * Returns a list of (prefix,namespace URI) pairs that represents
   1.211 +     * namespace bindings available on ancestor elements (that need not be repeated
   1.212 +     * by the JAXB RI.)
   1.213 +     *
   1.214 +     * <p>
   1.215 +     * Sometimes JAXB is used to marshal an XML document, which will be
   1.216 +     * used as a subtree of a bigger document. When this happens, it's nice
   1.217 +     * for a JAXB marshaller to be able to use in-scope namespace bindings
   1.218 +     * of the larger document and avoid declaring redundant namespace URIs.
   1.219 +     *
   1.220 +     * <p>
   1.221 +     * This is automatically done when you are marshalling to {@link XMLStreamWriter},
   1.222 +     * {@link XMLEventWriter}, {@link DOMResult}, or {@link Node}, because
   1.223 +     * those output format allows us to inspect what's currently available
   1.224 +     * as in-scope namespace binding. However, with other output format,
   1.225 +     * such as {@link OutputStream}, the JAXB RI cannot do this automatically.
   1.226 +     * That's when this method comes into play.
   1.227 +     *
   1.228 +     * <p>
   1.229 +     * Namespace bindings returned by this method will be used by the JAXB RI,
   1.230 +     * but will not be re-declared. They are assumed to be available when you insert
   1.231 +     * this subtree into a bigger document.
   1.232 +     *
   1.233 +     * <p>
   1.234 +     * It is <b>NOT</b> OK to return  the same binding, or give
   1.235 +     * the receiver a conflicting binding information.
   1.236 +     * It's a responsibility of the caller to make sure that this doesn't happen
   1.237 +     * even if the ancestor elements look like:
   1.238 +     * <pre><xmp>
   1.239 +     *   <foo:abc xmlns:foo="abc">
   1.240 +     *     <foo:abc xmlns:foo="def">
   1.241 +     *       <foo:abc xmlns:foo="abc">
   1.242 +     *         ... JAXB marshalling into here.
   1.243 +     *       </foo:abc>
   1.244 +     *     </foo:abc>
   1.245 +     *   </foo:abc>
   1.246 +     * </xmp></pre>
   1.247 +     *
   1.248 +     * @return
   1.249 +     *      always return a non-null (but possibly empty) array. The array stores
   1.250 +     *      data like (prefix1,nsUri1,prefix2,nsUri2,...) Use an empty string to represent
   1.251 +     *      the empty namespace URI and the default prefix. Null is not allowed as a value
   1.252 +     *      in the array.
   1.253 +     *
   1.254 +     * @since JAXB RI 2.0 beta
   1.255 +     */
   1.256 +    public String[] getContextualNamespaceDecls() {
   1.257 +        return EMPTY_STRING;
   1.258 +    }
   1.259 +}

mercurial