1 package org.openprovenance.model;
2
3 /*** Prefix definition for OPM serialisations. */
4
5
6 public class NamespacePrefixMapper extends com.sun.xml.bind.marshaller.NamespacePrefixMapper {
7
8
9
10
11 String defaultNamespace;
12
13 public NamespacePrefixMapper(String defaultNamespace) {
14 this.defaultNamespace=defaultNamespace;
15 }
16
17 /***
18 * Returns a preferred prefix for the given namespace URI.
19 *
20 * This method is intended to be overrided by a derived class.
21 *
22 * @param namespaceUri
23 * The namespace URI for which the prefix needs to be found.
24 * Never be null. "" is used to denote the default namespace.
25 * @param suggestion
26 * When the content tree has a suggestion for the prefix
27 * to the given namespaceUri, that suggestion is passed as a
28 * parameter. Typically this value comes from QName.getPrefix()
29 * to show the preference of the content tree. This parameter
30 * may be null, and this parameter may represent an already
31 * occupied prefix.
32 * @param requirePrefix
33 * If this method is expected to return non-empty prefix.
34 * When this flag is true, it means that the given namespace URI
35 * cannot be set as the default namespace.
36 *
37 * @return
38 * null if there's no preferred prefix for the namespace URI.
39 * In this case, the system will generate a prefix for you.
40 *
41 * Otherwise the system will try to use the returned prefix,
42 * but generally there's no guarantee if the prefix will be
43 * actually used or not.
44 *
45 * return "" to map this namespace URI to the default namespace.
46 * Again, there's no guarantee that this preference will be
47 * honored.
48 *
49 * If this method returns "" when requirePrefix=true, the return
50 * value will be ignored and the system will generate one.
51 */
52 public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
53 if (namespaceUri.equals("http://openprovenance.org/model/v1.1.a")) {
54 return "opm";
55 }
56 if (namespaceUri.equals("http://openprovenance.org/model/opmPrinterConfig")) {
57 return "prn";
58 }
59 if (namespaceUri.equals("http://www.w3.org/2001/XMLSchema")) {
60 return "xsd";
61 }
62 if (namespaceUri.equals("http://www.w3.org/2001/XMLSchema-instance")) {
63 return "xsi";
64 }
65 if ((defaultNamespace!=null) && (namespaceUri.equals(defaultNamespace))) {
66 return "";
67 }
68 return suggestion;
69 }
70
71 /***
72 * Returns a list of namespace URIs that should be declared
73 * at the root element.
74 * <p>
75 * By default, the JAXB RI produces namespace declarations only when
76 * they are necessary, only at where they are used. Because of this
77 * lack of look-ahead, sometimes the marshaller produces a lot of
78 * namespace declarations that look redundant to human eyes. For example,
79 * <pre><xmp>
80 * <?xml version="1.0"?>
81 * <root>
82 * <ns1:child xmlns:ns1="urn:foo"> ... </ns1:child>
83 * <ns2:child xmlns:ns2="urn:foo"> ... </ns2:child>
84 * <ns3:child xmlns:ns3="urn:foo"> ... </ns3:child>
85 * ...
86 * </root>
87 * <xmp></pre>
88 * <p>
89 * If you know in advance that you are going to use a certain set of
90 * namespace URIs, you can override this method and have the marshaller
91 * declare those namespace URIs at the root element.
92 * <p>
93 * For example, by returning <code>new String[]{"urn:foo"}</code>,
94 * the marshaller will produce:
95 * <pre><xmp>
96 * <?xml version="1.0"?>
97 * <root xmlns:ns1="urn:foo">
98 * <ns1:child> ... </ns1:child>
99 * <ns1:child> ... </ns1:child>
100 * <ns1:child> ... </ns1:child>
101 * ...
102 * </root>
103 * <xmp></pre>
104 * <p>
105 * To control prefixes assigned to those namespace URIs, use the
106 * {@link #getPreferredPrefix} method.
107 *
108 * @return
109 * A list of namespace URIs as an array of {@link String}s.
110 * This method can return a length-zero array but not null.
111 * None of the array component can be null. To represent
112 * the empty namespace, use the empty string <code>""</code>.
113 *
114 * @since
115 * JAXB RI 1.0.2
116 */
117 public String[] getPreDeclaredNamespaceUris() {
118 if (defaultNamespace!=null) {
119 return new String[] { "http://www.w3.org/2001/XMLSchema-instance",
120 "http://www.w3.org/2001/XMLSchema",
121 defaultNamespace};
122 } else {
123 return new String[] { "http://www.w3.org/2001/XMLSchema-instance",
124 "http://www.w3.org/2001/XMLSchema"};
125 }
126 }
127
128
129 }