Quellcode durchsuchen

fix: W3ElementFactory#parse supports custom entities

Ranides Atterwim vor 4 Jahren
Ursprung
Commit
cdb53f54ed

+ 5 - 4
assira.core/src/main/java/net/ranides/assira/xml/impl/W3ElementFactory.java

@@ -26,9 +26,7 @@ import net.ranides.assira.xml.XMLElements;
 import net.ranides.assira.xml.XMLException;
 
 import lombok.experimental.UtilityClass;
-import org.w3c.dom.DOMException;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
+import org.w3c.dom.*;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
@@ -91,7 +89,10 @@ public final class W3ElementFactory {
 
     private static Node parse(XMLElement target, String xml) {
         try {
-            String wxml="<__fragment__>" + xml + "</__fragment__>";
+            DocumentType tt = target.document().node().getDoctype();
+            String doctype = tt == null ? "" : "<!DOCTYPE __fragment__ [" + tt.getInternalSubset() + "] >";
+
+            String wxml = doctype + "<__fragment__>" + xml + "</__fragment__>";
             Document cdoc = builder().parse(new InputSource(new StringReader(wxml)));
             return target.document().node().importNode(cdoc.getDocumentElement(),true);
         } catch(SAXException | DOMException | ParserConfigurationException | IOException cause) {

+ 5 - 3
assira.core/src/test/java/net/ranides/assira/xml/impl/W3DocumentTest.java

@@ -38,8 +38,8 @@ public class W3DocumentTest {
     
     @Test
     public void testBuilder() throws IOException {
-        XMLDocument builder = XMLElement.fromString("<root></root>").document();
-        XMLElement doc = XMLElement.fromString("<target></target>");
+        XMLDocument builder = XMLElement.fromString("<!DOCTYPE root [\n<!ENTITY custom \"world\"> \n]><root></root>").document();
+        XMLElement doc = XMLElement.fromString("<!DOCTYPE target [\n<!ENTITY custom \"WORLD\"> \n]><target></target>");
 
         XMLElement comment1 = builder.comment("hello world");
         XMLElement comment2 = builder.comment("comment");
@@ -86,7 +86,9 @@ public class W3DocumentTest {
         fragment.append(builder.tag("h1").append(builder.text("header")));
         fragment.append(builder.tag("p").append(builder.text("paragraph")));
         fragment.append(builder.tag("span").append(builder.text("summary")));
-        
+        fragment.append(builder.tag("span").content("hello &custom;!"));
+        fragment.append(builder.text("hello <b> &custom;!"));
+
         doc.find("body").first().append(fragment);
 
         String out = doc.writer().indent(4).toString();

+ 2 - 0
assira.core/src/test/resources/xml/doc-jdk11.xml

@@ -10,5 +10,7 @@
         <h1>header</h1>
         <p>paragraph</p>
         <span>summary</span>
+        <span>hello world!</span>
+        hello &lt;b&gt; &amp;custom;!
     </body>
 </target><!--hello world--><!--comment--><?EOF meta-eof?>

+ 2 - 0
assira.core/src/test/resources/xml/doc-jdk8.xml

@@ -9,5 +9,7 @@
         <h1>header</h1>
         <p>paragraph</p>
         <span>summary</span>
+        <span>hello world!</span>
+        hello &lt;b&gt; &amp;custom;!
     </body>
 </target><!--hello world--><!--comment--><?EOF meta-eof?>