Skip to content

Commit

Permalink
Merge pull request #190 from hunterhacker/tmp-cleanup
Browse files Browse the repository at this point in the history
Include cumulative fixes from jdom-2.0.x branch
  • Loading branch information
rolfl authored Jul 2, 2021
2 parents 2a25ee4 + 07f3169 commit 768c7e8
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 34 deletions.
5 changes: 5 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,14 @@ For instructions on how to build JDOM, please view the README.txt file.
</jar>
<jar destfile="${package}/${jarbase}-javadoc.jar"
basedir="${build.javadocs}" includes="**/*" />

<jar jarfile="${package}/${jarbase}-sources.jar" >
<fileset dir="${core.src}" includes="**/*" />
</jar>
<jar jarfile="${package}/${jarbase}-junit-sources.jar" >
<fileset dir="${junit.src}" includes="**/*" />
</jar>
<jar jarfile="${package}/${jarbase}-contrib-sources.jar" >
<fileset dir="${contrib.src}" includes="**/*" />
</jar>

Expand Down
10 changes: 10 additions & 0 deletions core/src/java/org/jdom2/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ public List<Content> getContent() {
* </p>
*
* @param filter <code>Filter</code> to apply
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return <code>List</code> - filtered Document content
* @throws IllegalStateException if the root element hasn't been set
*/
Expand All @@ -452,6 +454,8 @@ public List<Content> removeContent() {
* Remove all child content from this parent matching the supplied filter.
*
* @param filter filter to select which content to remove
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return list of the old children detached from this parent
*/
@Override
Expand Down Expand Up @@ -729,6 +733,8 @@ public IteratorIterable<Content> getDescendants() {
* Comments, only Elements with a given name and/or prefix, and so on.
*
* @param filter filter to select which descendants to see
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return an iterator to walk descendants within a filter
*/
@Override
Expand Down Expand Up @@ -826,6 +832,10 @@ public void canContainContent(Content child, int index, boolean replace) {
}

if (child instanceof Text) {
if(Verifier.isAllXMLWhitespace(((Text) child).getText())) {
// only whitespace, not a problem.
return;
}
throw new IllegalAddException("A Text is not allowed at the document root");
}

Expand Down
24 changes: 17 additions & 7 deletions core/src/java/org/jdom2/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
import org.jdom2.ContentList.FilterList;
import org.jdom2.filter.ElementFilter;
import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import org.jdom2.util.IteratorIterable;

/**
Expand Down Expand Up @@ -773,6 +774,8 @@ public List<Content> getContent() {
* </p>
*
* @param filter <code>Filter</code> to apply
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return <code>List</code> - filtered Element content
*/
@Override
Expand All @@ -796,6 +799,8 @@ public List<Content> removeContent() {
* Remove all child content from this parent matching the supplied filter.
*
* @param filter filter to select which content to remove
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return list of the old children detached from this parent
*/
@Override
Expand Down Expand Up @@ -1494,6 +1499,8 @@ public IteratorIterable<Content> getDescendants() {
* Comments, only Elements with a given name and/or prefix, and so on.
*
* @param filter filter to select which descendants to see
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return an iterator to walk descendants within a filter
*/
@Override
Expand Down Expand Up @@ -1583,8 +1590,8 @@ public List<Element> getChildren(final String cname, final Namespace ns) {
* If no elements exist for the specified name and namespace, null is
* returned.
*
* @param cname local name of child element to match
* @param ns <code>Namespace</code> to search within. A null implies Namespace.NO_NAMESPACE.
* @param cname local name of child element to match. A null implies any name
* @param ns <code>Namespace</code> to search within. A null implies any namespace.
* @return the first matching child element, or null if not found
*/
public Element getChild(final String cname, final Namespace ns) {
Expand Down Expand Up @@ -1697,7 +1704,10 @@ public boolean removeChildren(final String cname, final Namespace ns) {
* <li>The {@link Namespace#XML_NAMESPACE} is added
* <li>The element's namespace is added (commonly
* {@link Namespace#NO_NAMESPACE})
* <li>All the attributes are inspected and their Namespaces are included
* <li>All the attributes are inspected and for those that have a namespace
* prefix then their Namespaces are included (the "default" namespace for
* attributes is not the same as the "default" namespace for the element that
* attribute is on).
* <li>All Namespaces declared on this Element using
* {@link #addNamespaceDeclaration(Namespace)} are included.
* <li>If the element has a parent then the parent's Namespace scope is
Expand All @@ -1708,9 +1718,7 @@ public boolean removeChildren(final String cname, final Namespace ns) {
* included.
* </ul>
* The Element's Namespace scope consist of its inherited Namespaces and
* any modifications to that scope derived from the Element itself. If the
* element is detached then its inherited scope consists of just
* If an element has no parent then
* any modifications to that scope derived from the Element itself.
* <p>
* Note that the Element's Namespace will always be reported first.
* <p>
Expand Down Expand Up @@ -1743,7 +1751,7 @@ public List<Namespace> getNamespacesInScope() {
if (attributes != null) {
for (Attribute att : getAttributes()) {
Namespace ns = att.getNamespace();
if (!namespaces.containsKey(ns.getPrefix())) {
if (!Namespace.NO_NAMESPACE.equals(ns) && !namespaces.containsKey(ns.getPrefix())) {
namespaces.put(ns.getPrefix(), ns);
}
}
Expand Down Expand Up @@ -1943,6 +1951,8 @@ public void sortAttributes(Comparator <? super Attribute> comparator) {
* @param <E> The generic type of the Filter used to select the content to
* sort.
* @param filter The Filter used to select which child content to sort.
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @param comparator The Comparator to use for the sorting.
*/
public <E extends Content> void sortContent(Filter<E> filter, Comparator <? super E> comparator) {
Expand Down
9 changes: 8 additions & 1 deletion core/src/java/org/jdom2/Parent.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
import java.util.*;

import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import org.jdom2.util.IteratorIterable;

/**
Expand Down Expand Up @@ -150,7 +151,9 @@ public interface Parent extends Cloneable, NamespaceAware, Serializable {
* starting at the beginning each time.
* @param <E> The Generic type of the returned content (the Filter's type)
*
* @param filter filter to apply
* @param filter filter to apply.
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return a list of the content of the parent matching the filter
* @throws IllegalStateException if parent is a Document
* and the root element is not set
Expand All @@ -171,6 +174,8 @@ public interface Parent extends Cloneable, NamespaceAware, Serializable {
* @param <E> The Generic type of the content to remove.
*
* @param filter filter to apply
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return list of the detached children matching the filter
*/
<E extends Content> List<E> removeContent(Filter<E> filter);
Expand Down Expand Up @@ -256,6 +261,8 @@ public interface Parent extends Cloneable, NamespaceAware, Serializable {
*
* @param <E> The generic type of the returned descendant data
* @param filter filter to select which descendants to see
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return an iterator to walk descendants that match a filter
*/
<E extends Content> IteratorIterable<E> getDescendants(Filter<E> filter);
Expand Down
6 changes: 6 additions & 0 deletions core/src/java/org/jdom2/input/SAXBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.EntityRef;
import org.jdom2.JDOMConstants;
import org.jdom2.JDOMException;
import org.jdom2.JDOMFactory;
import org.jdom2.Verifier;
Expand Down Expand Up @@ -797,6 +798,11 @@ public void setFastReconfigure(final boolean fastReconfigure) {
public void setFeature(final String name, final boolean value) {
// Save the specified feature for later.
features.put(name, value ? Boolean.TRUE : Boolean.FALSE);
if (JDOMConstants.SAX_FEATURE_EXTERNAL_ENT.equals(name)) {
// See issue https://github.com/hunterhacker/jdom/issues/189
// And PR https://github.com/hunterhacker/jdom/pull/188
setExpandEntities(value);
}
engine = null;
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/java/org/jdom2/input/StAXStreamBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ private static final Document process(final JDOMFactory factory,
case CDATA:
throw new JDOMException("Unexpected XMLStream event at Document level: CDATA");
case SPACE:
// I have not been able to identify a StAX Stream handler that produces
// space data outside the root element, but just in case, we ignore it.
break; //throw new JDOMException("Unexpected XMLStream event at Document level: SPACE");
// Can happen when XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE is set to true
document.addContent(factory.text(stream.getText()));
break;
case CHARACTERS:
final String badtxt = stream.getText();
if (!Verifier.isAllXMLWhitespace(badtxt)) {
Expand Down
15 changes: 1 addition & 14 deletions core/src/java/org/jdom2/input/sax/AbstractReaderXSDFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,6 @@ protected interface SchemaFactoryProvider {
SchemaFactory getSchemaFactory();
}

/**
* Use a Thread-Local system to manage SchemaFactory. SchemaFactory is not
* thread-safe, so we need some mechanism to isolate it, and thread-local is
* a logical way because it only creates an instance when needed in each
* thread, and they die when the thread dies. Does not need any
* synchronisation either.
*/
private static final ThreadLocal<SchemaFactory> schemafactl = new ThreadLocal<SchemaFactory>();

/**
* Compile an array of String URLs in to Sources which are then compiled in
* to a single Schema
Expand Down Expand Up @@ -239,11 +230,7 @@ private static final Schema getSchemaFromSource(final SchemaFactoryProvider sfp,
"XSD Source for an XML Schema validator");
}
try {
SchemaFactory sfac = schemafactl.get();
if (sfac == null) {
sfac = sfp.getSchemaFactory();
schemafactl.set(sfac);
}
SchemaFactory sfac = sfp.getSchemaFactory();
if (sfac == null) {
throw new JDOMException("Unable to create XSDSchema validator.");
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/java/org/jdom2/input/sax/SAXHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ protected void flushCharacters(final String data) throws SAXException {
final CDATA cdata = currentLocator == null ? factory.cdata(data)
: factory.cdata(lastline, lastcol, data);
factory.addContent(getCurrentElement(), cdata);
} else {
} else if (data.length() > 0) {
final Text text = currentLocator == null ? factory.text(data)
: factory.text(lastline, lastcol, data);
factory.addContent(getCurrentElement(), text);
Expand Down
1 change: 1 addition & 0 deletions core/src/java/org/jdom2/input/sax/TextBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ final class TextBuffer {
void append(final char[] source, final int start, final int count) {
if ((count + arraySize) > array.length) {
// grow by 25%
// Fixes #112
array = ArrayCopy.copyOf(array, count + arraySize + (array.length >> 2));
}
System.arraycopy(source, start, array, arraySize, count);
Expand Down
17 changes: 15 additions & 2 deletions core/src/java/org/jdom2/input/sax/XMLReaderXSDFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,23 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
public class XMLReaderXSDFactory extends AbstractReaderXSDFactory {

private static final SchemaFactoryProvider xsdschemas = new SchemaFactoryProvider() {
/**
* Use a Thread-Local system to manage SchemaFactory. SchemaFactory is not
* thread-safe, so we need some mechanism to isolate it, and thread-local is
* a logical way because it only creates an instance when needed in each
* thread, and they die when the thread dies. Does not need any
* synchronisation either.
*/
private final ThreadLocal<SchemaFactory> schemafactl = new ThreadLocal<SchemaFactory>();

@Override
public SchemaFactory getSchemaFactory() {
return SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI);
SchemaFactory sfac = schemafactl.get();
if (sfac == null) {
sfac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemafactl.set(sfac);
}
return sfac;
}
};

Expand Down
6 changes: 4 additions & 2 deletions core/src/java/org/jdom2/output/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Locale;

import org.jdom2.IllegalDataException;
import org.jdom2.Verifier;
Expand Down Expand Up @@ -535,14 +536,15 @@ public static final String escapeText(final EscapeStrategy strategy,

}


private static final EscapeStrategy chooseStrategy(String encoding) {
if ("UTF-8".equalsIgnoreCase(encoding) ||
"UTF-16".equalsIgnoreCase(encoding)) {
return UTFEscapeStrategy;
}

if (encoding.toUpperCase().startsWith("ISO-8859-") ||
// Note issue #149: https://github.com/hunterhacker/jdom/issues/149
// require locale for case conversion to avoid potential security issue.
if (encoding.toUpperCase(Locale.ENGLISH).startsWith("ISO-8859-") ||
"Latin1".equalsIgnoreCase(encoding)) {
return Bits8EscapeStrategy;
}
Expand Down
13 changes: 12 additions & 1 deletion core/src/java/org/jdom2/xpath/XPathFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public static final XPathFactory newInstance(String factoryclass) {
* XPathFactory implementations override this method to implement support
* for the JDOM/XPath API.
* <p>
* A Filter is used to coerce resulting XPath data in to a suitable JDOM generic
* type. Note that the {@link Filters} class has a number of predefined, useful
* filters.
* <p>
* <h2>Namespace</h2> XPath expressions are always namespace aware, and
* expect to be able to resolve prefixes to namespace URIs. In XPath
* expressions the prefix "" always resolves to the empty Namespace URI "".
Expand Down Expand Up @@ -211,8 +215,10 @@ public static final XPathFactory newInstance(String factoryclass) {
* @param expression
* The XPath expression.
* @param filter
* The Filter that is used to coerce the xpath result data in to the
* The Filter that is used to coerce the XPath result data in to the
* generic-typed results.
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @param variables
* Any variable values that may be referenced from the query. A null
* value indicates that there are no variables.
Expand All @@ -226,6 +232,7 @@ public static final XPathFactory newInstance(String factoryclass) {
* @throws IllegalArgumentException
* if any two Namespace values share the same prefix, or if there is
* any other reason that the XPath query cannot be compiled.
* @see org.jdom2.filter.Filters
*/
public abstract <T> XPathExpression<T> compile(String expression,
Filter<T> filter, Map<String, Object> variables,
Expand All @@ -242,6 +249,8 @@ public abstract <T> XPathExpression<T> compile(String expression,
* @param filter
* The Filter that is used to coerce the xpath result data in to the
* generic-typed results.
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @param variables
* Any variable values that may be referenced from the query. A null
* value indicates that there are no variables.
Expand Down Expand Up @@ -272,6 +281,8 @@ public <T> XPathExpression<T> compile(String expression, Filter<T> filter,
* @param filter
* The Filter that is used to coerce the xpath result data in to the
* generic-typed results.
* Note that the {@link Filters} class has a number of predefined, useful
* filters.
* @return an XPathExpression&lt;T&gt; instance.
* @throws NullPointerException
* if the query or filter is null
Expand Down
Loading

0 comments on commit 768c7e8

Please sign in to comment.