001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.metapath;
028
029import gov.nist.secauto.metaschema.core.util.CollectionUtil;
030
031import java.net.URI;
032import java.util.Map;
033import java.util.concurrent.ConcurrentHashMap;
034
035import edu.umd.cs.findbugs.annotations.NonNull;
036import edu.umd.cs.findbugs.annotations.Nullable;
037
038public final class StaticContext {
039  @Nullable
040  private final URI baseUri;
041  @NonNull
042  private final Map<String, URI> knownNamespaces;
043
044  @NonNull
045  public static StaticContext newInstance() {
046    return builder().build();
047  }
048
049  @NonNull
050  public static Builder builder() {
051    return new Builder();
052  }
053
054  private StaticContext(
055      @Nullable URI baseUri,
056      @NonNull Map<String, URI> knownNamespaces) {
057    this.baseUri = baseUri;
058    this.knownNamespaces = knownNamespaces;
059  }
060
061  /**
062   * Get the static base URI to use in resolving URIs handled by the Metapath
063   * processor. This URI, if provided, will be used when a document base URI is
064   * not available.
065   *
066   * @return the base URI or {@code null} if not defined
067   */
068  @Nullable
069  public URI getBaseUri() {
070    synchronized (this) {
071      return baseUri;
072    }
073  }
074
075  @Nullable
076  public URI getUriForPrefix(@NonNull String prefix) {
077    return knownNamespaces.get(prefix);
078  }
079
080  /**
081   * Generate a new dynamic context.
082   *
083   * @return the generated dynamic context
084   */
085  @NonNull
086  public DynamicContext newDynamicContext() {
087    return new DynamicContext(this);
088  }
089
090  public static class Builder {
091    private URI baseUri;
092    @NonNull
093    private final Map<String, URI> knownNamespaces = new ConcurrentHashMap<>();
094
095    private Builder() {
096      knownNamespaces.put(
097          MetapathConstants.PREFIX_METAPATH,
098          MetapathConstants.NS_METAPATH);
099      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}