Class SecureParserFactory
java.lang.Object
cloud.opencode.base.xml.security.SecureParserFactory
Secure Parser Factory - Factory for creating secure XML parsers
安全解析器工厂 - 创建安全 XML 解析器的工厂
This class provides cached, thread-safe factories for creating secure XML parsers. The factories are pre-configured with security settings to prevent XXE attacks.
此类提供缓存的线程安全工厂,用于创建安全的 XML 解析器。工厂已预配置安全设置以防止 XXE 攻击。
Features | 主要功能:
- Cached, thread-safe factories for secure XML parsers - 缓存的线程安全安全 XML 解析器工厂
- Pre-configured XXE attack prevention - 预配置的 XXE 攻击防护
- Supports DOM, SAX, StAX, and Transformer creation - 支持 DOM、SAX、StAX 和 Transformer 创建
Thread Safety | 线程安全:
The factories are thread-safe and can be reused. However, the parsers created from them are NOT thread-safe and should be used by a single thread at a time.
工厂是线程安全的,可以重复使用。但是,从它们创建的解析器不是线程安全的,应该一次只由一个线程使用。
Usage Examples | 使用示例:
// Create a secure DocumentBuilder
DocumentBuilder builder = SecureParserFactory.createDocumentBuilder();
// Create a secure SAXParser
SAXParser parser = SecureParserFactory.createSAXParser();
// Create a secure XMLStreamReader
XMLStreamReader reader = SecureParserFactory.createXMLStreamReader(inputStream);
Performance | 性能特性:
- Time complexity: O(1) for factory creation - 工厂创建 O(1)
- Space complexity: O(1) - O(1)
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcleanup()Removes all ThreadLocal instances to prevent memory leaks.static DocumentBuilderCreates a secure DocumentBuilder.static DocumentBuilderCreates a namespace-aware secure DocumentBuilder.static SAXParserCreates a namespace-aware secure SAXParser.static SAXParserCreates a secure SAXParser.static TransformerCreates a secure Transformer.static XMLInputFactoryCreates and returns a new secure XMLInputFactory.static XMLStreamReadercreateXMLStreamReader(InputStream input) Creates a secure XMLStreamReader from an InputStream.static XMLStreamReadercreateXMLStreamReader(Reader reader) Creates a secure XMLStreamReader from a Reader.static DocumentBuilderFactoryReturns the shared secure DocumentBuilderFactory.static DocumentBuilderFactoryReturns the shared namespace-aware secure DocumentBuilderFactory.static SAXParserFactoryReturns the shared namespace-aware secure SAXParserFactory.static SAXParserFactoryReturns the shared secure SAXParserFactory.static TransformerFactoryReturns the shared secure TransformerFactory.static XMLInputFactoryReturns the shared secure XMLInputFactory.
-
Method Details
-
createDocumentBuilder
Creates a secure DocumentBuilder. 创建安全的 DocumentBuilder。- Returns:
- a secure DocumentBuilder | 安全的 DocumentBuilder
-
createNamespaceAwareDocumentBuilder
Creates a namespace-aware secure DocumentBuilder. 创建支持命名空间的安全 DocumentBuilder。- Returns:
- a namespace-aware secure DocumentBuilder | 支持命名空间的安全 DocumentBuilder
-
createSAXParser
Creates a secure SAXParser. 创建安全的 SAXParser。- Returns:
- a secure SAXParser | 安全的 SAXParser
-
createNamespaceAwareSAXParser
Creates a namespace-aware secure SAXParser. 创建支持命名空间的安全 SAXParser。- Returns:
- a namespace-aware secure SAXParser | 支持命名空间的安全 SAXParser
-
createXMLStreamReader
Creates a secure XMLStreamReader from an InputStream. 从 InputStream 创建安全的 XMLStreamReader。- Parameters:
input- the input stream | 输入流- Returns:
- a secure XMLStreamReader | 安全的 XMLStreamReader
-
createXMLStreamReader
Creates a secure XMLStreamReader from a Reader. 从 Reader 创建安全的 XMLStreamReader。- Parameters:
reader- the reader | 读取器- Returns:
- a secure XMLStreamReader | 安全的 XMLStreamReader
-
createTransformer
Creates a secure Transformer. 创建安全的 Transformer。- Returns:
- a secure Transformer | 安全的 Transformer
-
getDocumentBuilderFactory
Returns the shared secure DocumentBuilderFactory. 返回共享的安全 DocumentBuilderFactory。- Returns:
- the factory | 工厂
-
getNamespaceAwareDocumentBuilderFactory
Returns the shared namespace-aware secure DocumentBuilderFactory. 返回共享的支持命名空间的安全 DocumentBuilderFactory。- Returns:
- the factory | 工厂
-
getSAXParserFactory
Returns the shared secure SAXParserFactory. 返回共享的安全 SAXParserFactory。- Returns:
- the factory | 工厂
-
getNamespaceAwareSAXParserFactory
Returns the shared namespace-aware secure SAXParserFactory. 返回共享的支持命名空间的安全 SAXParserFactory。- Returns:
- the factory | 工厂
-
getXMLInputFactory
Returns the shared secure XMLInputFactory. 返回共享的安全 XMLInputFactory。- Returns:
- the factory | 工厂
-
createXMLInputFactory
Creates and returns a new secure XMLInputFactory. 创建并返回新的安全 XMLInputFactory。Use this when you need a separate factory instance.
当需要单独的工厂实例时使用此方法。
- Returns:
- a new secure XMLInputFactory | 新的安全 XMLInputFactory
-
getTransformerFactory
Returns the shared secure TransformerFactory. 返回共享的安全 TransformerFactory。- Returns:
- the factory | 工厂
-
cleanup
public static void cleanup()Removes all ThreadLocal instances to prevent memory leaks. 移除所有 ThreadLocal 实例以防止内存泄漏。Call this method when the thread is done using XML parsers, especially in thread pool or web container environments.
当线程完成 XML 解析器使用后调用此方法,特别是在线程池或 Web 容器环境中。
-