PageRenderTime 27ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jaxws/sources/jaxws_src/src/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java

#
Java | 256 lines | 20 code | 11 blank | 225 comment | 0 complexity | 4069228eba7dfcedd97071c4e1f37311 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /*
  2. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package com.sun.xml.internal.bind.marshaller;
  26. import java.io.OutputStream;
  27. import javax.xml.bind.JAXBContext;
  28. import javax.xml.stream.XMLEventWriter;
  29. import javax.xml.stream.XMLStreamWriter;
  30. import javax.xml.transform.dom.DOMResult;
  31. import org.w3c.dom.Node;
  32. // be careful about changing this class. this class is supposed to be
  33. // extended by users and therefore we are not allowed to break
  34. // those user code.
  35. //
  36. // this means:
  37. // - don't add any abstract method
  38. // - don't change any existing method signature
  39. // - don't remove any existing method.
  40. /**
  41. * Implemented by the user application to determine URI -> prefix
  42. * mapping.
  43. *
  44. * This is considered as an interface, though it's implemented
  45. * as an abstract class to make it easy to add new methods in
  46. * a future.
  47. *
  48. * @author
  49. * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  50. */
  51. public abstract class NamespacePrefixMapper {
  52. private static final String[] EMPTY_STRING = new String[0];
  53. /**
  54. * Returns a preferred prefix for the given namespace URI.
  55. *
  56. * This method is intended to be overrided by a derived class.
  57. *
  58. * <p>
  59. * As noted in the return value portion of the javadoc, there
  60. * are several cases where the preference cannot be honored.
  61. * Specifically, as of JAXB RI 2.0 and onward:
  62. *
  63. * <ol>
  64. * <li>
  65. * If the prefix returned is already in use as one of the in-scope
  66. * namespace bindings. This is partly necessary for correctness
  67. * (so that we don't unexpectedly change the meaning of QNames
  68. * bound to {@link String}), partly to simplify the marshaller.
  69. * <li>
  70. * If the prefix returned is "" yet the current {@link JAXBContext}
  71. * includes classes that use the empty namespace URI. This allows
  72. * the JAXB RI to reserve the "" prefix for the empty namespace URI,
  73. * which is the only possible prefix for the URI.
  74. * This restriction is also to simplify the marshaller.
  75. * </ol>
  76. *
  77. * @param namespaceUri
  78. * The namespace URI for which the prefix needs to be found.
  79. * Never be null. "" is used to denote the default namespace.
  80. * @param suggestion
  81. * When the content tree has a suggestion for the prefix
  82. * to the given namespaceUri, that suggestion is passed as a
  83. * parameter. Typicall this value comes from the QName.getPrefix
  84. * to show the preference of the content tree. This parameter
  85. * may be null, and this parameter may represent an already
  86. * occupied prefix.
  87. * @param requirePrefix
  88. * If this method is expected to return non-empty prefix.
  89. * When this flag is true, it means that the given namespace URI
  90. * cannot be set as the default namespace.
  91. *
  92. * @return
  93. * null if there's no prefered prefix for the namespace URI.
  94. * In this case, the system will generate a prefix for you.
  95. *
  96. * Otherwise the system will try to use the returned prefix,
  97. * but generally there's no guarantee if the prefix will be
  98. * actually used or not.
  99. *
  100. * return "" to map this namespace URI to the default namespace.
  101. * Again, there's no guarantee that this preference will be
  102. * honored.
  103. *
  104. * If this method returns "" when requirePrefix=true, the return
  105. * value will be ignored and the system will generate one.
  106. *
  107. * @since JAXB 1.0.1
  108. */
  109. public abstract String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix);
  110. /**
  111. * Returns a list of namespace URIs that should be declared
  112. * at the root element.
  113. *
  114. * <p>
  115. * By default, the JAXB RI 1.0.x produces namespace declarations only when
  116. * they are necessary, only at where they are used. Because of this
  117. * lack of look-ahead, sometimes the marshaller produces a lot of
  118. * namespace declarations that look redundant to human eyes. For example,
  119. * <pre><xmp>
  120. * <?xml version="1.0"?>
  121. * <root>
  122. * <ns1:child xmlns:ns1="urn:foo"> ... </ns1:child>
  123. * <ns2:child xmlns:ns2="urn:foo"> ... </ns2:child>
  124. * <ns3:child xmlns:ns3="urn:foo"> ... </ns3:child>
  125. * ...
  126. * </root>
  127. * </xmp></pre>
  128. *
  129. * <p>
  130. * The JAXB RI 2.x mostly doesn't exhibit this behavior any more,
  131. * as it declares all statically known namespace URIs (those URIs
  132. * that are used as element/attribute names in JAXB annotations),
  133. * but it may still declare additional namespaces in the middle of
  134. * a document, for example when (i) a QName as an attribute/element value
  135. * requires a new namespace URI, or (ii) DOM nodes as a portion of an object
  136. * tree requires a new namespace URI.
  137. *
  138. * <p>
  139. * If you know in advance that you are going to use a certain set of
  140. * namespace URIs, you can override this method and have the marshaller
  141. * declare those namespace URIs at the root element.
  142. *
  143. * <p>
  144. * For example, by returning <code>new String[]{"urn:foo"}</code>,
  145. * the marshaller will produce:
  146. * <pre><xmp>
  147. * <?xml version="1.0"?>
  148. * <root xmlns:ns1="urn:foo">
  149. * <ns1:child> ... </ns1:child>
  150. * <ns1:child> ... </ns1:child>
  151. * <ns1:child> ... </ns1:child>
  152. * ...
  153. * </root>
  154. * </xmp></pre>
  155. * <p>
  156. * To control prefixes assigned to those namespace URIs, use the
  157. * {@link #getPreferredPrefix(String, String, boolean)} method.
  158. *
  159. * @return
  160. * A list of namespace URIs as an array of {@link String}s.
  161. * This method can return a length-zero array but not null.
  162. * None of the array component can be null. To represent
  163. * the empty namespace, use the empty string <code>""</code>.
  164. *
  165. * @since
  166. * JAXB RI 1.0.2
  167. */
  168. public String[] getPreDeclaredNamespaceUris() {
  169. return EMPTY_STRING;
  170. }
  171. /**
  172. * Similar to {@link #getPreDeclaredNamespaceUris()} but allows the
  173. * (prefix,nsUri) pairs to be returned.
  174. *
  175. * <p>
  176. * With {@link #getPreDeclaredNamespaceUris()}, applications who wish to control
  177. * the prefixes as well as the namespaces needed to implement both
  178. * {@link #getPreDeclaredNamespaceUris()} and {@link #getPreferredPrefix(String, String, boolean)}.
  179. *
  180. * <p>
  181. * This version eliminates the needs by returning an array of pairs.
  182. *
  183. * @return
  184. * always return a non-null (but possibly empty) array. The array stores
  185. * data like (prefix1,nsUri1,prefix2,nsUri2,...) Use an empty string to represent
  186. * the empty namespace URI and the default prefix. Null is not allowed as a value
  187. * in the array.
  188. *
  189. * @since
  190. * JAXB RI 2.0 beta
  191. */
  192. public String[] getPreDeclaredNamespaceUris2() {
  193. return EMPTY_STRING;
  194. }
  195. /**
  196. * Returns a list of (prefix,namespace URI) pairs that represents
  197. * namespace bindings available on ancestor elements (that need not be repeated
  198. * by the JAXB RI.)
  199. *
  200. * <p>
  201. * Sometimes JAXB is used to marshal an XML document, which will be
  202. * used as a subtree of a bigger document. When this happens, it's nice
  203. * for a JAXB marshaller to be able to use in-scope namespace bindings
  204. * of the larger document and avoid declaring redundant namespace URIs.
  205. *
  206. * <p>
  207. * This is automatically done when you are marshalling to {@link XMLStreamWriter},
  208. * {@link XMLEventWriter}, {@link DOMResult}, or {@link Node}, because
  209. * those output format allows us to inspect what's currently available
  210. * as in-scope namespace binding. However, with other output format,
  211. * such as {@link OutputStream}, the JAXB RI cannot do this automatically.
  212. * That's when this method comes into play.
  213. *
  214. * <p>
  215. * Namespace bindings returned by this method will be used by the JAXB RI,
  216. * but will not be re-declared. They are assumed to be available when you insert
  217. * this subtree into a bigger document.
  218. *
  219. * <p>
  220. * It is <b>NOT</b> OK to return the same binding, or give
  221. * the receiver a conflicting binding information.
  222. * It's a responsibility of the caller to make sure that this doesn't happen
  223. * even if the ancestor elements look like:
  224. * <pre><xmp>
  225. * <foo:abc xmlns:foo="abc">
  226. * <foo:abc xmlns:foo="def">
  227. * <foo:abc xmlns:foo="abc">
  228. * ... JAXB marshalling into here.
  229. * </foo:abc>
  230. * </foo:abc>
  231. * </foo:abc>
  232. * </xmp></pre>
  233. *
  234. * @return
  235. * always return a non-null (but possibly empty) array. The array stores
  236. * data like (prefix1,nsUri1,prefix2,nsUri2,...) Use an empty string to represent
  237. * the empty namespace URI and the default prefix. Null is not allowed as a value
  238. * in the array.
  239. *
  240. * @since JAXB RI 2.0 beta
  241. */
  242. public String[] getContextualNamespaceDecls() {
  243. return EMPTY_STRING;
  244. }
  245. }