| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.jaxb; |
|---|
| 22 | |
|---|
| 23 | import java.util.Iterator; |
|---|
| 24 | |
|---|
| 25 | import javax.xml.XMLConstants; |
|---|
| 26 | import javax.xml.namespace.NamespaceContext; |
|---|
| 27 | |
|---|
| 28 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 29 | |
|---|
| 30 | public class NamespacePrefixMapper extends com.sun.xml.bind.marshaller.NamespacePrefixMapper { |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Returns a preferred prefix for the given namespace URI. |
|---|
| 34 | * |
|---|
| 35 | * This method is intended to be overrided by a derived class. |
|---|
| 36 | * |
|---|
| 37 | * @param namespaceUri |
|---|
| 38 | * The namespace URI for which the prefix needs to be found. |
|---|
| 39 | * Never be null. "" is used to denote the default namespace. |
|---|
| 40 | * @param suggestion |
|---|
| 41 | * When the content tree has a suggestion for the prefix |
|---|
| 42 | * to the given namespaceUri, that suggestion is passed as a |
|---|
| 43 | * parameter. Typically this value comes from QName.getPrefix() |
|---|
| 44 | * to show the preference of the content tree. This parameter |
|---|
| 45 | * may be null, and this parameter may represent an already |
|---|
| 46 | * occupied prefix. |
|---|
| 47 | * @param requirePrefix |
|---|
| 48 | * If this method is expected to return non-empty prefix. |
|---|
| 49 | * When this flag is true, it means that the given namespace URI |
|---|
| 50 | * cannot be set as the default namespace. |
|---|
| 51 | * |
|---|
| 52 | * @return |
|---|
| 53 | * null if there's no preferred prefix for the namespace URI. |
|---|
| 54 | * In this case, the system will generate a prefix for you. |
|---|
| 55 | * |
|---|
| 56 | * Otherwise the system will try to use the returned prefix, |
|---|
| 57 | * but generally there's no guarantee if the prefix will be |
|---|
| 58 | * actually used or not. |
|---|
| 59 | * |
|---|
| 60 | * return "" to map this namespace URI to the default namespace. |
|---|
| 61 | * Again, there's no guarantee that this preference will be |
|---|
| 62 | * honored. |
|---|
| 63 | * |
|---|
| 64 | * If this method returns "" when requirePrefix=true, the return |
|---|
| 65 | * value will be ignored and the system will generate one. |
|---|
| 66 | */ |
|---|
| 67 | public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) |
|---|
| 68 | // Implement the interface |
|---|
| 69 | { |
|---|
| 70 | return NamespacePrefixMappings.getPreferredPrefixStatic(namespaceUri, suggestion, requirePrefix); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public static String getPreferredPrefix(String namespaceUri) |
|---|
| 74 | // Implement the interface |
|---|
| 75 | { |
|---|
| 76 | return NamespacePrefixMappings.getPreferredPrefixStatic(namespaceUri, null, false); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * Returns a list of namespace URIs that should be declared |
|---|
| 81 | * at the root element. |
|---|
| 82 | * <p> |
|---|
| 83 | * By default, the JAXB RI produces namespace declarations only when |
|---|
| 84 | * they are necessary, only at where they are used. Because of this |
|---|
| 85 | * lack of look-ahead, sometimes the marshaller produces a lot of |
|---|
| 86 | * namespace declarations that look redundant to human eyes. For example, |
|---|
| 87 | * <pre><xmp> |
|---|
| 88 | * <?xml version="1.0"?> |
|---|
| 89 | * <root> |
|---|
| 90 | * <ns1:child xmlns:ns1="urn:foo"> ... </ns1:child> |
|---|
| 91 | * <ns2:child xmlns:ns2="urn:foo"> ... </ns2:child> |
|---|
| 92 | * <ns3:child xmlns:ns3="urn:foo"> ... </ns3:child> |
|---|
| 93 | * ... |
|---|
| 94 | * </root> |
|---|
| 95 | * <xmp></pre> |
|---|
| 96 | * <p> |
|---|
| 97 | * If you know in advance that you are going to use a certain set of |
|---|
| 98 | * namespace URIs, you can override this method and have the marshaller |
|---|
| 99 | * declare those namespace URIs at the root element. |
|---|
| 100 | * <p> |
|---|
| 101 | * For example, by returning <code>new String[]{"urn:foo"}</code>, |
|---|
| 102 | * the marshaller will produce: |
|---|
| 103 | * <pre><xmp> |
|---|
| 104 | * <?xml version="1.0"?> |
|---|
| 105 | * <root xmlns:ns1="urn:foo"> |
|---|
| 106 | * <ns1:child> ... </ns1:child> |
|---|
| 107 | * <ns1:child> ... </ns1:child> |
|---|
| 108 | * <ns1:child> ... </ns1:child> |
|---|
| 109 | * ... |
|---|
| 110 | * </root> |
|---|
| 111 | * <xmp></pre> |
|---|
| 112 | * <p> |
|---|
| 113 | * To control prefixes assigned to those namespace URIs, use the |
|---|
| 114 | * {@link #getPreferredPrefix} method. |
|---|
| 115 | * |
|---|
| 116 | * @return |
|---|
| 117 | * A list of namespace URIs as an array of {@link String}s. |
|---|
| 118 | * This method can return a length-zero array but not null. |
|---|
| 119 | * None of the array component can be null. To represent |
|---|
| 120 | * the empty namespace, use the empty string <code>""</code>. |
|---|
| 121 | * |
|---|
| 122 | * @since |
|---|
| 123 | * JAXB RI 1.0.2 |
|---|
| 124 | */ |
|---|
| 125 | // public String[] getPreDeclaredNamespaceUris() { |
|---|
| 126 | // return new String[] { "urn:abc", "urn:def" }; |
|---|
| 127 | // } |
|---|
| 128 | |
|---|
| 129 | } |
|---|