Skip to content

Commit

Permalink
Addresses #189 again - use just the features for entity expansion tra…
Browse files Browse the repository at this point in the history
…cking
  • Loading branch information
rolfl committed Jul 2, 2021
1 parent 07f3169 commit 118853b
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions core/src/java/org/jdom2/input/SAXBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT

package org.jdom2.input;

import static org.jdom2.JDOMConstants.SAX_FEATURE_EXTERNAL_ENT;
import static org.jdom2.JDOMConstants.SAX_PROPERTY_DECLARATION_HANDLER;
import static org.jdom2.JDOMConstants.SAX_PROPERTY_LEXICAL_HANDLER;
import static org.jdom2.JDOMConstants.SAX_PROPERTY_LEXICAL_HANDLER_ALT;
Expand All @@ -72,7 +71,6 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLFilter;
Expand Down Expand Up @@ -193,9 +191,6 @@ public class SAXBuilder implements SAXEngine {
/** XMLFilter instance to use */
private XMLFilter saxXMLFilter = null;

/** Whether expansion of entities should occur */
private boolean expand = true;

/** Whether to ignore ignorable whitespace */
private boolean ignoringWhite = false;

Expand Down Expand Up @@ -336,6 +331,8 @@ public SAXBuilder(final XMLReaderJDOMFactory readersouce) {
*/
public SAXBuilder(final XMLReaderJDOMFactory xmlreaderfactory, final SAXHandlerFactory handlerfactory,
final JDOMFactory jdomfactory) {
// force default state of Expanding DTD entries, ensures features map has entry to support getExpandEntities
setExpandEntities(true);
this.readerfac = xmlreaderfactory == null
? XMLReaders.NONVALIDATING
: xmlreaderfactory;
Expand Down Expand Up @@ -691,7 +688,7 @@ public void setIgnoringBoundaryWhitespace(final boolean ignoringBoundaryWhite) {
*/
@Override
public boolean getExpandEntities() {
return expand;
return Boolean.TRUE.equals(features.get(JDOMConstants.SAX_FEATURE_EXTERNAL_ENT));
}

/**
Expand All @@ -718,7 +715,7 @@ public boolean getExpandEntities() {
* occur.
*/
public void setExpandEntities(final boolean expand) {
this.expand = expand;
features.put(JDOMConstants.SAX_FEATURE_EXTERNAL_ENT, expand ? Boolean.TRUE : Boolean.FALSE);

This comment has been minimized.

Copy link
@sourabhsparkala

This comment has been minimized.

Copy link
@sourabhsparkala

sourabhsparkala Oct 22, 2021

@hunterhacker any updates on this. Please let me know.

engine = null;
}

Expand Down Expand Up @@ -853,7 +850,7 @@ public SAXEngine buildEngine() throws JDOMException {
// Create and configure the content handler.
final SAXHandler contentHandler = handlerfac.createSAXHandler(jdomfac);

contentHandler.setExpandEntities(expand);
contentHandler.setExpandEntities(getExpandEntities());
contentHandler.setIgnoringElementContentWhitespace(ignoringWhite);
contentHandler.setIgnoringBoundaryWhitespace(ignoringBoundaryWhite);

Expand Down Expand Up @@ -982,22 +979,13 @@ protected void configureParser(final XMLReader parser, final SAXHandler contentH
internalSetProperty(parser, me.getKey(), me.getValue(), me.getKey());
}

// Set entity expansion
// Note SAXHandler can work regardless of how this is set, but when
// entity expansion it's worth it to try to tell the parser not to
// even bother with external general entities.
// Apparently no parsers yet support this feature.
// XXX It might make sense to setEntityResolver() with a resolver
// that simply ignores external general entities
try {
if (parser.getFeature(SAX_FEATURE_EXTERNAL_ENT) != expand) {
parser.setFeature(SAX_FEATURE_EXTERNAL_ENT, expand);
}
} catch (final SAXException e) { /* Ignore... */
// Set any user-specified features on the parser (always includes entity expansion true/false).
for (final Map.Entry<String, Boolean> me : features.entrySet()) {
internalSetFeature(parser, me.getKey(), me.getValue().booleanValue(), me.getKey());
}

// Try setting the DeclHandler if entity expansion is off
if (!expand) {
if (!getExpandEntities()) {
try {
parser.setProperty(SAX_PROPERTY_DECLARATION_HANDLER,
contentHandler);
Expand All @@ -1008,10 +996,6 @@ protected void configureParser(final XMLReader parser, final SAXHandler contentH
// No lexical reporting available
}
}
// Set any user-specified features on the parser.
for (final Map.Entry<String, Boolean> me : features.entrySet()) {
internalSetFeature(parser, me.getKey(), me.getValue().booleanValue(), me.getKey());
}
}

/**
Expand All @@ -1024,10 +1008,10 @@ private void internalSetFeature(final XMLReader parser, final String feature,
parser.setFeature(feature, value);
} catch (final SAXNotSupportedException e) {
throw new JDOMException(
displayName + " feature not supported for SAX driver " + parser.getClass().getName());
displayName + " feature " + feature + " not supported for SAX driver " + parser.getClass().getName());
} catch (final SAXNotRecognizedException e) {
throw new JDOMException(
displayName + " feature not recognized for SAX driver " + parser.getClass().getName());
displayName + " feature " + feature + " not recognized for SAX driver " + parser.getClass().getName());
}
}

Expand All @@ -1043,10 +1027,10 @@ private void internalSetProperty(final XMLReader parser, final String property,
parser.setProperty(property, value);
} catch (final SAXNotSupportedException e) {
throw new JDOMException(
displayName + " property not supported for SAX driver " + parser.getClass().getName());
displayName + " property " + property + " not supported for SAX driver " + parser.getClass().getName());
} catch (final SAXNotRecognizedException e) {
throw new JDOMException(
displayName + " property not recognized for SAX driver " + parser.getClass().getName());
displayName + " property " + property + " not recognized for SAX driver " + parser.getClass().getName());
}
}

Expand Down

0 comments on commit 118853b

Please sign in to comment.