Class OpenNamespaceContext

java.lang.Object
cloud.opencode.base.xml.namespace.OpenNamespaceContext
All Implemented Interfaces:
NamespaceContext

public final class OpenNamespaceContext extends Object implements NamespaceContext
Open Namespace Context - NamespaceContext implementation for XPath 开放命名空间上下文 - 用于 XPath 的 NamespaceContext 实现

This class provides a simple implementation of NamespaceContext for use with XPath.

此类提供用于 XPath 的 NamespaceContext 简单实现。

Usage Examples | 使用示例:

// Create namespace context
OpenNamespaceContext ctx = OpenNamespaceContext.create()
    .bind("soap", "http://schemas.xmlsoap.org/soap/envelope/")
    .bind("xsd", "http://www.w3.org/2001/XMLSchema");

// Use with XPath
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(ctx);
String result = xpath.evaluate("//soap:Body", document);

Features | 主要功能:

  • NamespaceContext implementation for XPath evaluation - 用于 XPath 求值的 NamespaceContext 实现
  • Fluent API for prefix-URI binding - 用于前缀-URI 绑定的流式 API
  • Pre-bound standard XML namespaces - 预绑定的标准 XML 命名空间

Security | 安全性:

  • Thread-safe: No (mutable bindings) - 线程安全: 否(可变绑定)
  • Null-safe: No (throws on null prefix/URI) - 空值安全: 否(null 前缀/URI 抛异常)
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static OpenNamespaceContext create()
      Creates a new namespace context. 创建新的命名空间上下文。
      Returns:
      a new context | 新上下文
    • of

      public static OpenNamespaceContext of(Map<String,String> prefixToUri)
      Creates a namespace context from a map. 从映射创建命名空间上下文。
      Parameters:
      prefixToUri - the prefix to URI map | 前缀到 URI 映射
      Returns:
      a new context | 新上下文
    • of

      public static OpenNamespaceContext of(String prefix, String namespaceUri)
      Creates a namespace context with a single binding. 创建具有单个绑定的命名空间上下文。
      Parameters:
      prefix - the prefix | 前缀
      namespaceUri - the namespace URI | 命名空间 URI
      Returns:
      a new context | 新上下文
    • bind

      public OpenNamespaceContext bind(String prefix, String namespaceUri)
      Binds a prefix to a namespace URI. 将前缀绑定到命名空间 URI。
      Parameters:
      prefix - the prefix | 前缀
      namespaceUri - the namespace URI | 命名空间 URI
      Returns:
      this context for chaining | 此上下文以便链式调用
    • setDefaultNamespace

      public OpenNamespaceContext setDefaultNamespace(String namespaceUri)
      Sets the default namespace URI. 设置默认命名空间 URI。
      Parameters:
      namespaceUri - the namespace URI | 命名空间 URI
      Returns:
      this context for chaining | 此上下文以便链式调用
    • unbind

      public OpenNamespaceContext unbind(String prefix)
      Removes a prefix binding. 移除前缀绑定。
      Parameters:
      prefix - the prefix to remove | 要移除的前缀
      Returns:
      this context for chaining | 此上下文以便链式调用
    • isBound

      public boolean isBound(String prefix)
      Checks if a prefix is bound. 检查前缀是否已绑定。
      Parameters:
      prefix - the prefix | 前缀
      Returns:
      true if bound | 如果已绑定则返回 true
    • getPrefixes

      public Set<String> getPrefixes()
      Gets all bound prefixes. 获取所有已绑定的前缀。
      Returns:
      set of prefixes | 前缀集合
    • getBindings

      public Map<String,String> getBindings()
      Gets all bindings. 获取所有绑定。
      Returns:
      map of prefix to URI | 前缀到 URI 的映射
    • getNamespaceURI

      public String getNamespaceURI(String prefix)
      Specified by:
      getNamespaceURI in interface NamespaceContext
    • getPrefix

      public String getPrefix(String namespaceUri)
      Specified by:
      getPrefix in interface NamespaceContext
    • getPrefixes

      public Iterator<String> getPrefixes(String namespaceUri)
      Specified by:
      getPrefixes in interface NamespaceContext
    • toString

      public String toString()
      Overrides:
      toString in class Object