Class NamespaceUtil

java.lang.Object
cloud.opencode.base.xml.namespace.NamespaceUtil

public final class NamespaceUtil extends Object
Namespace Utility - Utilities for XML namespace operations 命名空间工具 - XML 命名空间操作的工具类

This class provides utility methods for working with XML namespaces.

此类提供处理 XML 命名空间的工具方法。

Features | 主要功能:

  • Utility methods for XML namespace operations - XML 命名空间操作的工具方法
  • Namespace extraction from DOM documents - 从 DOM 文档提取命名空间
  • Common namespace URI constants - 常用命名空间 URI 常量

Usage Examples | 使用示例:

// Extract namespaces from document
Map<String, String> namespaces = NamespaceUtil.extractNamespaces(document);

// Get namespace URI for prefix
String uri = NamespaceUtil.getNamespaceURI(element, "soap");

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
  • Null-safe: No (throws on null document) - 空值安全: 否(null 文档抛异常)

Performance | 性能特性:

  • Time complexity: O(d×a) for extractNamespaces where d=ancestor depth and a=attributes per element; O(n) for string operations (getLocalPart, getPrefix, createQName) where n=string length; O(1) for namespace checks - 时间复杂度: extractNamespaces 为 O(d×a),d 为祖先深度,a 为每个元素的属性数;字符串操作 O(n),n 为字符串长度;命名空间检查为 O(1)
  • Space complexity: O(d) for namespace map entries from ancestor chain - 空间复杂度: 祖先链的命名空间 map 条目为 O(d)
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Method Details

    • extractNamespaces

      public static Map<String,String> extractNamespaces(XmlDocument document)
      Extracts all namespace declarations from a document. 从文档中提取所有命名空间声明。
      Parameters:
      document - the document | 文档
      Returns:
      map of prefix to namespace URI | 前缀到命名空间 URI 的映射
    • extractNamespaces

      public static Map<String,String> extractNamespaces(XmlElement element)
      Extracts all namespace declarations from an element and its ancestors. 从元素及其祖先中提取所有命名空间声明。
      Parameters:
      element - the element | 元素
      Returns:
      map of prefix to namespace URI | 前缀到命名空间 URI 的映射
    • extractNamespaces

      public static Map<String,String> extractNamespaces(Element element)
      Extracts namespace declarations from a DOM element and its ancestors. 从 DOM 元素及其祖先中提取命名空间声明。
      Parameters:
      element - the DOM element | DOM 元素
      Returns:
      map of prefix to namespace URI | 前缀到命名空间 URI 的映射
    • createContext

      public static OpenNamespaceContext createContext(XmlDocument document)
      Creates a namespace context from a document. 从文档创建命名空间上下文。
      Parameters:
      document - the document | 文档
      Returns:
      a namespace context | 命名空间上下文
    • createContext

      public static OpenNamespaceContext createContext(XmlElement element)
      Creates a namespace context from an element. 从元素创建命名空间上下文。
      Parameters:
      element - the element | 元素
      Returns:
      a namespace context | 命名空间上下文
    • createContextFromMap

      public static OpenNamespaceContext createContextFromMap(Map<String,String> namespaces)
      Creates a namespace context from a map. 从映射创建命名空间上下文。
      Parameters:
      namespaces - the namespace map | 命名空间映射
      Returns:
      a namespace context | 命名空间上下文
    • getLocalPart

      public static String getLocalPart(String qName)
      Gets the local part of a qualified name. 获取限定名的本地部分。
      Parameters:
      qName - the qualified name (e.g., "prefix:localName") | 限定名
      Returns:
      the local part | 本地部分
    • getPrefix

      public static String getPrefix(String qName)
      Gets the prefix of a qualified name. 获取限定名的前缀。
      Parameters:
      qName - the qualified name (e.g., "prefix:localName") | 限定名
      Returns:
      the prefix, or empty string if no prefix | 前缀,如果没有前缀则返回空字符串
    • createQName

      public static String createQName(String prefix, String localPart)
      Creates a qualified name from prefix and local part. 从前缀和本地部分创建限定名。
      Parameters:
      prefix - the prefix (can be null or empty) | 前缀(可以为 null 或空)
      localPart - the local part | 本地部分
      Returns:
      the qualified name | 限定名
    • isQualifiedName

      public static boolean isQualifiedName(String name)
      Checks if a name is a qualified name (has a prefix). 检查名称是否是限定名(具有前缀)。
      Parameters:
      name - the name | 名称
      Returns:
      true if qualified | 如果是限定名则返回 true
    • declareNamespace

      public static void declareNamespace(XmlElement element, String prefix, String namespaceUri)
      Adds a namespace declaration to an element. 向元素添加命名空间声明。
      Parameters:
      element - the element | 元素
      prefix - the prefix | 前缀
      namespaceUri - the namespace URI | 命名空间 URI
    • declareDefaultNamespace

      public static void declareDefaultNamespace(XmlElement element, String namespaceUri)
      Adds a default namespace declaration to an element. 向元素添加默认命名空间声明。
      Parameters:
      element - the element | 元素
      namespaceUri - the namespace URI | 命名空间 URI
    • removeNamespaceDeclaration

      public static void removeNamespaceDeclaration(XmlElement element, String prefix)
      Removes a namespace declaration from an element. 从元素中移除命名空间声明。
      Parameters:
      element - the element | 元素
      prefix - the prefix to remove | 要移除的前缀
    • getNamespaceUri

      public static String getNamespaceUri(XmlElement element)
      Gets the namespace URI of an element. 获取元素的命名空间 URI。
      Parameters:
      element - the element | 元素
      Returns:
      the namespace URI, or null | 命名空间 URI,或 null
    • getNamespacePrefix

      public static String getNamespacePrefix(XmlElement element)
      Gets the namespace prefix of an element. 获取元素的命名空间前缀。
      Parameters:
      element - the element | 元素
      Returns:
      the prefix, or null | 前缀,或 null
    • isInNamespace

      public static boolean isInNamespace(XmlElement element, String namespaceUri)
      Checks if an element is in a specific namespace. 检查元素是否在特定命名空间中。
      Parameters:
      element - the element | 元素
      namespaceUri - the namespace URI | 命名空间 URI
      Returns:
      true if in the namespace | 如果在命名空间中则返回 true
    • hasNoNamespace

      public static boolean hasNoNamespace(XmlElement element)
      Checks if an element has no namespace. 检查元素是否没有命名空间。
      Parameters:
      element - the element | 元素
      Returns:
      true if no namespace | 如果没有命名空间则返回 true
    • soapContext

      public static OpenNamespaceContext soapContext()
      Creates a namespace context for SOAP 1.1. 创建 SOAP 1.1 的命名空间上下文。
      Returns:
      a namespace context | 命名空间上下文
    • soap12Context

      public static OpenNamespaceContext soap12Context()
      Creates a namespace context for SOAP 1.2. 创建 SOAP 1.2 的命名空间上下文。
      Returns:
      a namespace context | 命名空间上下文
    • xsdContext

      public static OpenNamespaceContext xsdContext()
      Creates a namespace context for XML Schema. 创建 XML Schema 的命名空间上下文。
      Returns:
      a namespace context | 命名空间上下文