elharo commented on code in PR #7:
URL: https://github.com/apache/xerces-j/pull/7#discussion_r2018611174


##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,114 @@
+package loc;
+
+import org.apache.xerces.impl.Constants;
+import org.apache.xerces.parsers.DOMParser;
+import junit.framework.*;
+
+import static 
org.apache.xerces.parsers.XML11Configuration.LOCATION_INFO_FEATURE;
+
+import org.w3c.dom.*;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+public class XercesDOMLocationTest extends TestCase {
+
+    private static final String TEST_XML_PATH = "tests/loc/test.xml";
+
+    public XercesDOMLocationTest(String testName) {
+        super(testName);
+    }
+
+    public static List<Element> getElementChildren(Element parent) {
+        List<Element> result = new ArrayList<>();
+        NodeList children = parent.getChildNodes();
+        for (int i = 0; i < children.getLength(); i++) {
+            Node node = children.item(i);
+            if (node.getNodeType() == Node.ELEMENT_NODE) {
+                result.add((Element) node);
+            }
+        }
+        return result;
+    }
+
+    public static TestSuite suite() {
+        return new TestSuite(XercesDOMLocationTest.class);
+    }
+
+    private Document parseXML(boolean enableLocationFeature) throws Exception {
+        DOMParser parser = new DOMParser();
+        parser.setFeature(LOCATION_INFO_FEATURE, enableLocationFeature);
+        parser.parse(new File(TEST_XML_PATH).toURI().toString());
+        Document doc = parser.getDocument();
+        // inspect structure a bit to make sure the parse worked.
+        Element root = doc.getDocumentElement();
+        String name = root.getTagName();
+        assertEquals("root", name);
+        List<Element> ec = getElementChildren(root);
+        assertEquals(2, ec.size());
+        Element n1 = ec.get(0);
+        Element n2 = ec.get(1);
+        assertEquals("child", n1.getTagName());
+        assertEquals("selfClosing", n2.getTagName());
+        return doc;
+    }
+
+    public void testFeatureDisabledByDefault() throws Exception {
+        Document doc = parseXML(false); // Feature disabled
+        Element root = doc.getDocumentElement();
+        Object locationData = root.getUserData(Constants.LOCATION_INFO);
+        assertNull("Location data should not be present when feature is 
disabled.", locationData);
+    }
+
+    public void testFeatureEnabledLocationPresent() throws Exception {
+        Document doc = parseXML(true); // Feature enabled
+
+        Element root = doc.getDocumentElement();
+        Object locationData = root.getUserData(Constants.LOCATION_INFO);
+
+        assertNotNull("Location data should be present when feature is 
enabled.", locationData);
+        assertTrue("Location data should be an instance of String.", 
locationData instanceof String);
+    }
+
+    public void testRootElementLocation() throws Exception {
+        Document doc = parseXML(true);
+        Element root = doc.getDocumentElement();
+
+        String location = (String) root.getUserData(Constants.LOCATION_INFO);
+        assertNotNull("Root element should have location data.", location);
+
+        System.out.println("Root Location: " + location);

Review Comment:
   don't use println in tests; asserts only



##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,114 @@
+package loc;
+
+import org.apache.xerces.impl.Constants;
+import org.apache.xerces.parsers.DOMParser;
+import junit.framework.*;
+
+import static 
org.apache.xerces.parsers.XML11Configuration.LOCATION_INFO_FEATURE;
+
+import org.w3c.dom.*;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+public class XercesDOMLocationTest extends TestCase {
+
+    private static final String TEST_XML_PATH = "tests/loc/test.xml";
+
+    public XercesDOMLocationTest(String testName) {
+        super(testName);
+    }
+
+    public static List<Element> getElementChildren(Element parent) {
+        List<Element> result = new ArrayList<>();
+        NodeList children = parent.getChildNodes();
+        for (int i = 0; i < children.getLength(); i++) {
+            Node node = children.item(i);
+            if (node.getNodeType() == Node.ELEMENT_NODE) {
+                result.add((Element) node);
+            }
+        }
+        return result;
+    }
+
+    public static TestSuite suite() {
+        return new TestSuite(XercesDOMLocationTest.class);
+    }
+
+    private Document parseXML(boolean enableLocationFeature) throws Exception {
+        DOMParser parser = new DOMParser();
+        parser.setFeature(LOCATION_INFO_FEATURE, enableLocationFeature);
+        parser.parse(new File(TEST_XML_PATH).toURI().toString());
+        Document doc = parser.getDocument();
+        // inspect structure a bit to make sure the parse worked.
+        Element root = doc.getDocumentElement();
+        String name = root.getTagName();
+        assertEquals("root", name);
+        List<Element> ec = getElementChildren(root);
+        assertEquals(2, ec.size());
+        Element n1 = ec.get(0);
+        Element n2 = ec.get(1);
+        assertEquals("child", n1.getTagName());
+        assertEquals("selfClosing", n2.getTagName());
+        return doc;
+    }
+
+    public void testFeatureDisabledByDefault() throws Exception {
+        Document doc = parseXML(false); // Feature disabled
+        Element root = doc.getDocumentElement();
+        Object locationData = root.getUserData(Constants.LOCATION_INFO);
+        assertNull("Location data should not be present when feature is 
disabled.", locationData);
+    }
+
+    public void testFeatureEnabledLocationPresent() throws Exception {
+        Document doc = parseXML(true); // Feature enabled
+
+        Element root = doc.getDocumentElement();
+        Object locationData = root.getUserData(Constants.LOCATION_INFO);
+
+        assertNotNull("Location data should be present when feature is 
enabled.", locationData);
+        assertTrue("Location data should be an instance of String.", 
locationData instanceof String);
+    }
+
+    public void testRootElementLocation() throws Exception {
+        Document doc = parseXML(true);
+        Element root = doc.getDocumentElement();
+
+        String location = (String) root.getUserData(Constants.LOCATION_INFO);
+        assertNotNull("Root element should have location data.", location);
+
+        System.out.println("Root Location: " + location);
+        assertEquals("Root element should be on line 2.", "2", location);
+    }
+
+    public void testChildElementLocation() throws Exception {
+        Document doc = parseXML(true);
+        Element child = (Element) 
doc.getDocumentElement().getElementsByTagName("child").item(0);
+
+        String location = (String) child.getUserData(Constants.LOCATION_INFO);
+        assertNotNull("Child element should have location data.", location);
+
+        System.out.println("Child Location: " + location);

Review Comment:
   ditto, here and below



##########
src/org/apache/xerces/parsers/AbstractDOMParser.java:
##########
@@ -123,7 +123,9 @@ public class AbstractDOMParser extends 
AbstractXMLDocumentParser {
         INCLUDE_COMMENTS_FEATURE,
         CREATE_CDATA_NODES_FEATURE,
         INCLUDE_IGNORABLE_WHITESPACE,
-        DEFER_NODE_EXPANSION
+        DEFER_NODE_EXPANSION,
+            // Why are all the other feature flags not also defined in 
XML11Configuration?

Review Comment:
   delete comment; file a bug if necessary instead



##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,114 @@
+package loc;

Review Comment:
   apache license notice



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to