Skip to content

Commit

Permalink
Added possibility to provide custom SAXParserFactory to SAXReader
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Jan 21, 2025
1 parent 0af3e8b commit 4b9f48f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
Expand Down Expand Up @@ -64,14 +65,21 @@ public interface ISAXReaderSettings extends IBaseXMLReaderSettings
*/
boolean isRequiresNewXMLParserExplicitly ();

/**
* @return A custom SAX parser factory. This is only needed to work around
* some of the default SAXParserFactory configuration cannot be
* applied.
* @since 11.1.13
*/
@Nullable
SAXParserFactory getCustomSAXParserFactory ();

/**
* Check if the current settings require a separate
* {@link javax.xml.parsers.DocumentBuilderFactory} or if a pooled default
* object can be used.
* {@link org.xml.sax.XMLReader} or if a pooled default object can be used.
*
* @return <code>true</code> if a separate
* {@link javax.xml.parsers.DocumentBuilderFactory} is required,
* <code>false</code> if not.
* @return <code>true</code> if a separate {@link org.xml.sax.XMLReader} is
* required, <code>false</code> if not.
*/
boolean requiresNewXMLParser ();

Expand Down
27 changes: 19 additions & 8 deletions ph-xml/src/main/java/com/helger/xml/serialize/read/SAXReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import javax.annotation.Nonnull;
import javax.annotation.WillClose;
import javax.annotation.concurrent.ThreadSafe;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.PresentForCodeCoverage;
Expand Down Expand Up @@ -205,19 +207,28 @@ public static ESuccess readXMLSAX (@WillClose @Nonnull final InputSource aIS,

try
{
final XMLReader aParser;
final boolean bFromPool;
final org.xml.sax.XMLReader aParser;
if (aSettings.requiresNewXMLParser ())

final SAXParserFactory aCustomSaxParserFactory = aSettings.getCustomSAXParserFactory ();
if (aCustomSaxParserFactory != null)
{
aParser = SAXReaderFactory.createXMLReader ();
// Using the customer XMLReader
aParser = SAXReaderFactory.createXMLReader (aCustomSaxParserFactory);
bFromPool = false;
}
else
{
// use parser from pool
aParser = POOL.borrowObject ();
bFromPool = true;
}
if (aSettings.requiresNewXMLParser ())
{
aParser = SAXReaderFactory.createXMLReader ();
bFromPool = false;
}
else
{
// use parser from pool
aParser = POOL.borrowObject ();
bFromPool = true;
}

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ public XMLReader get ()

@Nonnull
public static XMLReader createXMLReader ()
{
final SAXParserFactory aFactory = XMLFactory.createDefaultSAXParserFactory ();
return createXMLReader (aFactory);
}

@Nonnull
public static XMLReader createXMLReader (@Nonnull final SAXParserFactory aFactory)
{
try
{
final SAXParserFactory aFactory = XMLFactory.createDefaultSAXParserFactory ();
return aFactory.newSAXParser ().getXMLReader ();
}
catch (final ParserConfigurationException | SAXException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import javax.xml.parsers.SAXParserFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class SAXReaderSettings implements ISAXReaderSettings, ICloneable <SAXRea
private final ICommonsMap <EXMLParserProperty, Object> m_aProperties = new CommonsEnumMap <> (EXMLParserProperty.class);
private final ICommonsMap <EXMLParserFeature, Boolean> m_aFeatures = new CommonsEnumMap <> (EXMLParserFeature.class);
private final CallbackList <IExceptionCallback <Throwable>> m_aExceptionCallbacks = new CallbackList <> ();
private SAXParserFactory m_aCustomSAXParserFactory;
private boolean m_bRequiresNewXMLParserExplicitly;

/**
Expand All @@ -76,6 +78,8 @@ public SAXReaderSettings ()
setPropertyValues (SAXReaderDefaultSettings.getAllPropertyValues ());
setFeatureValues (SAXReaderDefaultSettings.getAllFeatureValues ());
exceptionCallbacks ().set (SAXReaderDefaultSettings.exceptionCallbacks ());
// Custom factory is always null
setCustomSAXParserFactory (null);
setRequiresNewXMLParserExplicitly (SAXReaderDefaultSettings.isRequiresNewXMLParserExplicitly ());
}

Expand All @@ -97,6 +101,7 @@ public SAXReaderSettings (@Nonnull final ISAXReaderSettings aOther)
setPropertyValues (aOther.getAllPropertyValues ());
setFeatureValues (aOther.getAllFeatureValues ());
exceptionCallbacks ().set (aOther.exceptionCallbacks ());
setCustomSAXParserFactory (aOther.getCustomSAXParserFactory ());
setRequiresNewXMLParserExplicitly (aOther.isRequiresNewXMLParserExplicitly ());
}

Expand Down Expand Up @@ -351,6 +356,19 @@ public CallbackList <IExceptionCallback <Throwable>> exceptionCallbacks ()
return m_aExceptionCallbacks;
}

@Nullable
public SAXParserFactory getCustomSAXParserFactory ()
{
return m_aCustomSAXParserFactory;
}

@Nonnull
public final SAXReaderSettings setCustomSAXParserFactory (@Nullable final SAXParserFactory a)
{
m_aCustomSAXParserFactory = a;
return this;
}

public boolean isRequiresNewXMLParserExplicitly ()
{
return m_bRequiresNewXMLParserExplicitly;
Expand Down Expand Up @@ -392,14 +410,15 @@ public void applyToSAXReader (@Nonnull final org.xml.sax.XMLReader aParser)
@Override
public String toString ()
{
return new ToStringGenerator (this).append ("entityResolver", m_aEntityResolver)
.append ("dtdHandler", m_aDTDHandler)
.append ("contentHandler", m_aContentHandler)
.append ("errorHandler", m_aErrorHandler)
.append ("properties", m_aProperties)
.append ("features", m_aFeatures)
.append ("exceptionHandler", m_aExceptionCallbacks)
.append ("requiresNewXMLParserExplicitly", m_bRequiresNewXMLParserExplicitly)
return new ToStringGenerator (this).append ("EntityResolver", m_aEntityResolver)
.append ("DtdHandler", m_aDTDHandler)
.append ("ContentHandler", m_aContentHandler)
.append ("ErrorHandler", m_aErrorHandler)
.append ("Properties", m_aProperties)
.append ("Features", m_aFeatures)
.append ("ExceptionHandler", m_aExceptionCallbacks)
.append ("CustomSAXParserFactory", m_aCustomSAXParserFactory)
.append ("RequiresNewXMLParserExplicitly", m_bRequiresNewXMLParserExplicitly)
.getToString ();
}

Expand Down

0 comments on commit 4b9f48f

Please sign in to comment.