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


##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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) {

Review Comment:
   This constructor likely isn't needed



##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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);

Review Comment:
   assertInstanceOf?



##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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) {

Review Comment:
   private



##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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.*;

Review Comment:
   does this project use wildcard imports?>



##########
src/org/apache/xerces/parsers/AbstractDOMParser.java:
##########
@@ -933,6 +940,10 @@ public void startElement (QName element, XMLAttributes 
attributes, Augmentations
                 return;
             }
             Element el = createElementNode (element);
+            // Extract location info if feature is enabled
+            if (fIncludeLocationInfo) {
+                el.setUserData( Constants.LOCATION_INFO, String.valueOf( 
fLocator.getLineNumber() ), null ); // Save location String into node

Review Comment:
   1. These values should be Integers, not strings.
   2. This might be an abuse of user data. That's meant for the application, 
not the parser.



##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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() {

Review Comment:
   do we really need this?



##########
tests/loc/XercesDOMLocationTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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);

Review Comment:
   Why is this a string instead of an int?



-- 
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: j-dev-unsubscr...@xerces.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscr...@xerces.apache.org
For additional commands, e-mail: j-dev-h...@xerces.apache.org

Reply via email to