This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 59b95c5e let LoadSaveUtils.xmlText2GenericDom callers pass XmlOptions 
(#166)
59b95c5e is described below

commit 59b95c5ee80f4cd2ea9c908455dfcbce4ac16f98
Author: PJ Fanning <[email protected]>
AuthorDate: Sat Nov 14 20:01:26 2026 +0100

    let LoadSaveUtils.xmlText2GenericDom callers pass XmlOptions (#166)
    
    * let LoadSaveUtils.xmlText2GenericDom callers pass XmlOptions
    
    Adds an overload taking XmlOptions so callers can set the entity
    expansion limit, disallow DOCTYPE declarations, control DTD loading,
    and opt in to entity resolution with their own EntityResolver. The
    existing two-argument method delegates with the defaults.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * cover the remaining LoadSaveUtils.xmlText2GenericDom behaviour from #165
    
    Tests for the default XmlOptions entity expansion limit, external
    parameter entities and external DTDs not being read, internal entity
    expansion, namespaces, comments via the lexical handler, and malformed
    input throwing.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../apache/xmlbeans/impl/common/LoadSaveUtils.java |  29 +++++-
 .../xmlbeans/impl/common/LoadSaveUtilsTest.java    | 101 +++++++++++++++++++++
 2 files changed, 129 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java 
b/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java
index 8ee15956..21c5124d 100644
--- a/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java
+++ b/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java
@@ -37,10 +37,37 @@ import java.io.InputStream;
 
 public class LoadSaveUtils
 {
+    /**
+     * Parses XML text into {@code emptyDoc} using the default {@link 
XmlOptions}.
+     * External entities are not resolved.
+     *
+     * @see #xmlText2GenericDom(InputStream, Document, XmlOptions)
+     */
     public static Document xmlText2GenericDom(InputStream is, Document 
emptyDoc)
             throws SAXException, ParserConfigurationException, IOException
     {
-        XMLReader reader = SAXHelper.newXMLReader(new XmlOptions());
+        return xmlText2GenericDom(is, emptyDoc, null);
+    }
+
+    /**
+     * Parses XML text into {@code emptyDoc}.
+     * <p>
+     * The parser honours {@link XmlOptions#getEntityExpansionLimit()},
+     * {@link XmlOptions#disallowDocTypeDeclaration()}, {@link 
XmlOptions#isLoadDTDGrammar()}
+     * and {@link XmlOptions#isLoadExternalDTD()}. External entities are not 
resolved unless
+     * an entity resolver is set with {@link XmlOptions#setEntityResolver}.
+     *
+     * @param options the options to use, or {@code null} for the defaults
+     * @since 5.5.0
+     */
+    public static Document xmlText2GenericDom(InputStream is, Document 
emptyDoc, XmlOptions options)
+            throws SAXException, ParserConfigurationException, IOException
+    {
+        XmlOptions opts = new XmlOptions(options);
+        XMLReader reader = SAXHelper.newXMLReader(opts);
+        if (opts.getEntityResolver() != null) {
+            reader.setEntityResolver(opts.getEntityResolver());
+        }
 
         Sax2Dom handler = new Sax2Dom(emptyDoc);
 
diff --git 
a/src/test/java/org/apache/xmlbeans/impl/common/LoadSaveUtilsTest.java 
b/src/test/java/org/apache/xmlbeans/impl/common/LoadSaveUtilsTest.java
index 5412f4ea..9ce09723 100644
--- a/src/test/java/org/apache/xmlbeans/impl/common/LoadSaveUtilsTest.java
+++ b/src/test/java/org/apache/xmlbeans/impl/common/LoadSaveUtilsTest.java
@@ -15,16 +15,24 @@
 
 package org.apache.xmlbeans.impl.common;
 
+import org.apache.xmlbeans.XmlOptions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXParseException;
 
 import java.io.ByteArrayInputStream;
+import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class LoadSaveUtilsTest {
 
@@ -36,6 +44,10 @@ class LoadSaveUtilsTest {
         assertEquals("r", doc.getDocumentElement().getNodeName());
         assertEquals("1", doc.getDocumentElement().getAttribute("a"));
         assertEquals("text", doc.getDocumentElement().getTextContent());
+        // Sax2Dom is registered as the lexical handler, so comments are kept
+        Node last = doc.getDocumentElement().getLastChild();
+        assertEquals(Node.COMMENT_NODE, last.getNodeType());
+        assertEquals("x", last.getNodeValue());
     }
 
     // xmlText2GenericDom must not resolve external entities, like every other
@@ -49,4 +61,93 @@ class LoadSaveUtilsTest {
             new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)), 
DocumentHelper.createDocument());
         assertEquals("", doc.getDocumentElement().getTextContent());
     }
+
+    @Test
+    void namespacesAreKept() throws Exception {
+        Document doc = LoadSaveUtils.xmlText2GenericDom(
+            stream("<p:r xmlns:p='urn:p' p:a='1'><c xmlns='urn:d'/></p:r>"), 
DocumentHelper.createDocument());
+        Element r = doc.getDocumentElement();
+        assertEquals("urn:p", r.getNamespaceURI());
+        assertEquals("r", r.getLocalName());
+        assertEquals("1", r.getAttributeNS("urn:p", "a"));
+        assertEquals("urn:d", ((Element) r.getFirstChild()).getNamespaceURI());
+    }
+
+    @Test
+    void internalEntityIsExpanded() throws Exception {
+        Document doc = LoadSaveUtils.xmlText2GenericDom(
+            stream("<!DOCTYPE r [<!ENTITY x 'expanded'>]><r>&x;</r>"), 
DocumentHelper.createDocument());
+        assertEquals("expanded", doc.getDocumentElement().getTextContent());
+    }
+
+    @Test
+    void malformedXmlThrows() {
+        assertThrows(SAXParseException.class, () -> 
LoadSaveUtils.xmlText2GenericDom(
+            stream("<r><c></r>"), DocumentHelper.createDocument()));
+    }
+
+    // the XmlOptions default limit (2048) applies, not the parser's own 
default
+    @Test
+    void defaultEntityExpansionLimitIsApplied() {
+        String xml = "<!DOCTYPE r [<!ENTITY a 'aaaaaaaaaa'>"
+            + "<!ENTITY b '&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;'>"
+            + "<!ENTITY c '&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;'>"
+            + "<!ENTITY d '&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;'>]><r>&d;&d;&d;</r>";
+        assertThrows(SAXParseException.class, () -> 
LoadSaveUtils.xmlText2GenericDom(
+            stream(xml), DocumentHelper.createDocument()));
+    }
+
+    @Test
+    void externalDtdIsNotLoaded(@TempDir Path dir) throws Exception {
+        Path dtd = dir.resolve("r.dtd");
+        Files.write(dtd, "<!ATTLIST r a CDATA 
'SECRET'>".getBytes(StandardCharsets.UTF_8));
+        String xml = "<!DOCTYPE r SYSTEM '" + dtd.toUri() + "'><r/>";
+        Document doc = LoadSaveUtils.xmlText2GenericDom(stream(xml), 
DocumentHelper.createDocument());
+        assertFalse(doc.getDocumentElement().hasAttribute("a"));
+    }
+
+    @Test
+    void externalParameterEntityIsNotResolved(@TempDir Path dir) throws 
Exception {
+        Path ent = dir.resolve("p.ent");
+        Files.write(ent, "<!ATTLIST r a CDATA 
'SECRET'>".getBytes(StandardCharsets.UTF_8));
+        String xml = "<!DOCTYPE r [<!ENTITY % p SYSTEM '" + ent.toUri() + "'> 
%p;]><r/>";
+        Document doc = LoadSaveUtils.xmlText2GenericDom(stream(xml), 
DocumentHelper.createDocument());
+        assertFalse(doc.getDocumentElement().hasAttribute("a"));
+    }
+
+    @Test
+    void nullOptionsUsesDefaults() throws Exception {
+        Document doc = LoadSaveUtils.xmlText2GenericDom(stream("<r>text</r>"), 
DocumentHelper.createDocument(), null);
+        assertEquals("text", doc.getDocumentElement().getTextContent());
+    }
+
+    @Test
+    void disallowDocTypeDeclaration() {
+        XmlOptions options = new 
XmlOptions().setDisallowDocTypeDeclaration(true);
+        assertThrows(SAXParseException.class, () -> 
LoadSaveUtils.xmlText2GenericDom(
+            stream("<!DOCTYPE r [<!ENTITY x 'y'>]><r>&x;</r>"), 
DocumentHelper.createDocument(), options));
+    }
+
+    @Test
+    void entityExpansionLimit() {
+        String xml = "<!DOCTYPE r [<!ENTITY a 'aaaaaaaaaa'>"
+            + "<!ENTITY b '&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;'>"
+            + "<!ENTITY c '&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;'>]><r>&c;</r>";
+        XmlOptions options = new XmlOptions().setEntityExpansionLimit(10);
+        assertThrows(SAXParseException.class, () -> 
LoadSaveUtils.xmlText2GenericDom(
+            stream(xml), DocumentHelper.createDocument(), options));
+    }
+
+    @Test
+    void entityResolverFromOptionsIsUsed() throws Exception {
+        String xml = "<!DOCTYPE r [<!ENTITY x SYSTEM 
'urn:test:x'>]><r>&x;</r>";
+        XmlOptions options = new XmlOptions().setEntityResolver((publicId, 
systemId) ->
+            "urn:test:x".equals(systemId) ? new InputSource(new 
StringReader("resolved")) : null);
+        Document doc = LoadSaveUtils.xmlText2GenericDom(stream(xml), 
DocumentHelper.createDocument(), options);
+        assertEquals("resolved", doc.getDocumentElement().getTextContent());
+    }
+
+    private static ByteArrayInputStream stream(String xml) {
+        return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to