Skip to content

Commit

Permalink
Issue #10066 - Allow customization of SAXParserFactory / SAXParser in…
Browse files Browse the repository at this point in the history
… XmlParser

Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Jul 5, 2023
1 parent 17c593f commit 1db3f0d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class XmlParser
*/
public XmlParser()
{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParserFactory factory = newSAXParserFactory();
boolean validatingDefault = factory.getClass().toString().contains("org.apache.xerces.");
String validatingProp = System.getProperty("org.eclipse.jetty.xml.XmlParser.Validating", validatingDefault ? "true" : "false");
boolean validating = Boolean.valueOf(validatingProp).booleanValue();
Expand All @@ -83,11 +83,16 @@ AutoLock lock()
return _lock.lock();
}

protected SAXParserFactory newSAXParserFactory()
{
return SAXParserFactory.newInstance();
}

public void setValidating(boolean validating)
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParserFactory factory = newSAXParserFactory();
factory.setValidating(validating);
_parser = factory.newSAXParser();

Expand Down Expand Up @@ -129,6 +134,11 @@ public boolean isValidating()
return _parser.isValidating();
}

public SAXParser getParser()
{
return _parser;
}

public void redirectEntity(String name, URL entity)
{
if (entity != null)
Expand Down
33 changes: 33 additions & 0 deletions jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@

import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.jupiter.api.Test;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class XmlParserTest
Expand All @@ -38,4 +47,28 @@ public void testXmlParser() throws Exception
assertTrue(testDocStr.startsWith("<Configure"));
assertTrue(testDocStr.endsWith("</Configure>"));
}

/**
* Customize SAXParserFactory behavior.
*/
@Test
public void testNewSAXParserFactory()
{
XmlParser xmlParser = new XmlParser()
{
@Override
protected SAXParserFactory newSAXParserFactory()
{
SAXParserFactory saxParserFactory = super.newSAXParserFactory();
// Configure at factory level
saxParserFactory.setXIncludeAware(false);
return saxParserFactory;
}
};

SAXParser saxParser = xmlParser.getParser();
assertNotNull(saxParser);
// look to see it was set at parser level
assertFalse(saxParser.isNamespaceAware());
}
}

0 comments on commit 1db3f0d

Please sign in to comment.