1 /*
2 * Portions of this software was developed by employees of the National Institute
3 * of Standards and Technology (NIST), an agency of the Federal Government and is
4 * being made available as a public service. Pursuant to title 17 United States
5 * Code Section 105, works of NIST employees are not subject to copyright
6 * protection in the United States. This software may be subject to foreign
7 * copyright. Permission in the United States and in foreign countries, to the
8 * extent that NIST may hold copyright, to use, copy, modify, create derivative
9 * works, and distribute this software and its documentation without fee is hereby
10 * granted on a non-exclusive basis, provided that this notice and disclaimer
11 * of warranty appears in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25 */
26
27 package gov.nist.secauto.metaschema.core.metapath;
28
29 import gov.nist.secauto.metaschema.core.util.CollectionUtil;
30
31 import java.net.URI;
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentHashMap;
34
35 import edu.umd.cs.findbugs.annotations.NonNull;
36 import edu.umd.cs.findbugs.annotations.Nullable;
37
38 public final class StaticContext {
39 @Nullable
40 private final URI baseUri;
41 @NonNull
42 private final Map<String, URI> knownNamespaces;
43
44 @NonNull
45 public static StaticContext newInstance() {
46 return builder().build();
47 }
48
49 @NonNull
50 public static Builder builder() {
51 return new Builder();
52 }
53
54 private StaticContext(
55 @Nullable URI baseUri,
56 @NonNull Map<String, URI> knownNamespaces) {
57 this.baseUri = baseUri;
58 this.knownNamespaces = knownNamespaces;
59 }
60
61 /**
62 * Get the static base URI to use in resolving URIs handled by the Metapath
63 * processor. This URI, if provided, will be used when a document base URI is
64 * not available.
65 *
66 * @return the base URI or {@code null} if not defined
67 */
68 @Nullable
69 public URI getBaseUri() {
70 synchronized (this) {
71 return baseUri;
72 }
73 }
74
75 @Nullable
76 public URI getUriForPrefix(@NonNull String prefix) {
77 return knownNamespaces.get(prefix);
78 }
79
80 /**
81 * Generate a new dynamic context.
82 *
83 * @return the generated dynamic context
84 */
85 @NonNull
86 public DynamicContext newDynamicContext() {
87 return new DynamicContext(this);
88 }
89
90 public static class Builder {
91 private URI baseUri;
92 @NonNull
93 private final Map<String, URI> knownNamespaces = new ConcurrentHashMap<>();
94
95 private Builder() {
96 knownNamespaces.put(
97 MetapathConstants.PREFIX_METAPATH,
98 MetapathConstants.NS_METAPATH);
99 knownNamespaces.put(
100 MetapathConstants.PREFIX_XML_SCHEMA,
101 MetapathConstants.NS_XML_SCHEMA);
102 knownNamespaces.put(
103 MetapathConstants.PREFIX_XPATH_FUNCTIONS,
104 MetapathConstants.NS_XPATH_FUNCTIONS);
105 knownNamespaces.put(
106 MetapathConstants.PREFIX_XPATH_FUNCTIONS_MATH,
107 MetapathConstants.NS_XPATH_FUNCTIONS_MATH);
108 }
109
110 /**
111 * Sets the static base URI to use in resolving URIs handled by the Metapath
112 * processor, when a document base URI is not available. There is only a single
113 * base URI. Subsequent calls to this method will change the base URI.
114 *
115 * @param uri
116 * the base URI to use
117 * @return this builder
118 */
119 @NonNull
120 public Builder baseUri(@NonNull URI uri) {
121 this.baseUri = uri;
122 return this;
123 }
124
125 @NonNull
126 public Builder namespace(@NonNull String prefix, @NonNull URI uri) {
127 this.knownNamespaces.put(prefix, uri);
128 return this;
129 }
130
131 @NonNull
132 public StaticContext build() {
133 return new StaticContext(
134 baseUri,
135 CollectionUtil.unmodifiableMap(knownNamespaces));
136 }
137 }
138 }