* Allow customization of SAXParserFactory / SAXParser in XmlParser * Introduce method `.getSAXParser()` --------- Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com> Co-authored-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
parent
dee0d4ff7d
commit
9a05c75ad2
|
@ -66,7 +66,7 @@ public class XmlParser
|
||||||
*/
|
*/
|
||||||
public XmlParser()
|
public XmlParser()
|
||||||
{
|
{
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
SAXParserFactory factory = newSAXParserFactory();
|
||||||
boolean validatingDefault = factory.getClass().toString().contains("org.apache.xerces.");
|
boolean validatingDefault = factory.getClass().toString().contains("org.apache.xerces.");
|
||||||
String validatingProp = System.getProperty("org.eclipse.jetty.xml.XmlParser.Validating", validatingDefault ? "true" : "false");
|
String validatingProp = System.getProperty("org.eclipse.jetty.xml.XmlParser.Validating", validatingDefault ? "true" : "false");
|
||||||
boolean validating = Boolean.valueOf(validatingProp).booleanValue();
|
boolean validating = Boolean.valueOf(validatingProp).booleanValue();
|
||||||
|
@ -83,11 +83,16 @@ public class XmlParser
|
||||||
return _lock.lock();
|
return _lock.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected SAXParserFactory newSAXParserFactory()
|
||||||
|
{
|
||||||
|
return SAXParserFactory.newInstance();
|
||||||
|
}
|
||||||
|
|
||||||
public void setValidating(boolean validating)
|
public void setValidating(boolean validating)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
SAXParserFactory factory = newSAXParserFactory();
|
||||||
factory.setValidating(validating);
|
factory.setValidating(validating);
|
||||||
_parser = factory.newSAXParser();
|
_parser = factory.newSAXParser();
|
||||||
|
|
||||||
|
@ -129,6 +134,11 @@ public class XmlParser
|
||||||
return _parser.isValidating();
|
return _parser.isValidating();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SAXParser getSAXParser()
|
||||||
|
{
|
||||||
|
return _parser;
|
||||||
|
}
|
||||||
|
|
||||||
public void redirectEntity(String name, URL entity)
|
public void redirectEntity(String name, URL entity)
|
||||||
{
|
{
|
||||||
if (entity != null)
|
if (entity != null)
|
||||||
|
|
|
@ -14,10 +14,17 @@
|
||||||
package org.eclipse.jetty.xml;
|
package org.eclipse.jetty.xml;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import javax.xml.parsers.SAXParser;
|
||||||
|
import javax.xml.parsers.SAXParserFactory;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
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;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||||
|
|
||||||
public class XmlParserTest
|
public class XmlParserTest
|
||||||
{
|
{
|
||||||
|
@ -38,4 +45,32 @@ public class XmlParserTest
|
||||||
assertTrue(testDocStr.startsWith("<Configure"));
|
assertTrue(testDocStr.startsWith("<Configure"));
|
||||||
assertTrue(testDocStr.endsWith("</Configure>"));
|
assertTrue(testDocStr.endsWith("</Configure>"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customize SAXParserFactory behavior.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testNewSAXParserFactory() throws SAXException
|
||||||
|
{
|
||||||
|
XmlParser xmlParser = new XmlParser()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected SAXParserFactory newSAXParserFactory()
|
||||||
|
{
|
||||||
|
SAXParserFactory saxParserFactory = super.newSAXParserFactory();
|
||||||
|
// Configure at factory level
|
||||||
|
saxParserFactory.setXIncludeAware(false);
|
||||||
|
return saxParserFactory;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SAXParser saxParser = xmlParser.getSAXParser();
|
||||||
|
assertNotNull(saxParser);
|
||||||
|
|
||||||
|
XMLReader xmlReader = saxParser.getXMLReader();
|
||||||
|
// Only run testcase if Xerces is being used.
|
||||||
|
assumeTrue(xmlReader.getClass().getName().contains("org.apache.xerces."));
|
||||||
|
// look to see it was set at XMLReader level
|
||||||
|
assertFalse(xmlReader.getFeature("http://apache.org/xml/features/xinclude"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue