8290740: Catalog not used when the handler is null

Reviewed-by: lancea, naoto, iris
This commit is contained in:
Joe Wang 2022-07-30 01:42:05 +00:00
parent dd9bd31b86
commit 470c0eb216
3 changed files with 67 additions and 54 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2022, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -128,7 +128,7 @@ import org.xml.sax.InputSource;
*
*
* @see XIncludeNamespaceSupport
* @LastModified: May 2021
* @LastModified: July 2022
*/
public class XIncludeHandler
implements XMLComponent, XMLDocumentFilter, XMLDTDFilter {
@ -1638,8 +1638,8 @@ public class XIncludeHandler
}
XMLInputSource includedSource = null;
if (fEntityResolver != null) {
try {
try {
if (fEntityResolver != null) {
XMLResourceIdentifier resourceIdentifier =
new XMLResourceIdentifierImpl(
null,
@ -1652,57 +1652,56 @@ public class XIncludeHandler
includedSource =
fEntityResolver.resolveEntity(resourceIdentifier);
}
if (includedSource == null && fUseCatalog) {
if (fCatalogFeatures == null) {
fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
}
fCatalogFile = fCatalogFeatures.get(CatalogFeatures.Feature.FILES);
if (fCatalogFile != null) {
/*
Although URI entry is preferred for resolving XInclude, system entry
is allowed as well.
*/
Source source = null;
try {
if (fCatalogResolver == null) {
fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
}
source = fCatalogResolver.resolve(href, fCurrentBaseURI.getExpandedSystemId());
} catch (CatalogException e) {}
if (includedSource == null && fUseCatalog) {
if (fCatalogFeatures == null) {
fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
}
fCatalogFile = fCatalogFeatures.get(CatalogFeatures.Feature.FILES);
if (fCatalogFile != null) {
/*
Although URI entry is preferred for resolving XInclude, system entry
is allowed as well.
*/
Source source = null;
try {
if (fCatalogResolver == null) {
fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
}
source = fCatalogResolver.resolve(href, fCurrentBaseURI.getExpandedSystemId());
} catch (CatalogException e) {}
if (source != null && !source.isEmpty()) {
includedSource = new XMLInputSource(null, source.getSystemId(),
fCurrentBaseURI.getExpandedSystemId(), true);
} else {
if (fCatalogResolver == null) {
fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
}
InputSource is = fCatalogResolver.resolveEntity(href, href);
if (is != null && !is.isEmpty()) {
includedSource = new XMLInputSource(is, true);
}
if (source != null && !source.isEmpty()) {
includedSource = new XMLInputSource(null, source.getSystemId(),
fCurrentBaseURI.getExpandedSystemId(), true);
} else {
if (fCatalogResolver == null) {
fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
}
InputSource is = fCatalogResolver.resolveEntity(href, href);
if (is != null && !is.isEmpty()) {
includedSource = new XMLInputSource(is, true);
}
}
}
if (includedSource != null &&
!(includedSource instanceof HTTPInputSource) &&
(accept != null || acceptLanguage != null) &&
includedSource.getCharacterStream() == null &&
includedSource.getByteStream() == null) {
includedSource = createInputSource(includedSource.getPublicId(), includedSource.getSystemId(),
includedSource.getBaseSystemId(), accept, acceptLanguage);
}
}
catch (IOException | CatalogException e) {
reportResourceError(
"XMLResourceError",
new Object[] { href, e.getMessage()}, e);
return false;
if (includedSource != null &&
!(includedSource instanceof HTTPInputSource) &&
(accept != null || acceptLanguage != null) &&
includedSource.getCharacterStream() == null &&
includedSource.getByteStream() == null) {
includedSource = createInputSource(includedSource.getPublicId(), includedSource.getSystemId(),
includedSource.getBaseSystemId(), accept, acceptLanguage);
}
}
catch (IOException | CatalogException e) {
reportResourceError(
"XMLResourceError",
new Object[] { href, e.getMessage()}, e);
return false;
}
if (includedSource == null) {
// setup an HTTPInputSource if either of the content negotation attributes were specified.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -41,7 +41,7 @@ import org.xml.sax.InputSource;
/**
* @test
* @bug 8158084 8162438 8162442 8166220 8166398
* @bug 8158084 8162438 8162442 8166220 8166398 8290740
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow catalog.CatalogSupport
* @run testng/othervm catalog.CatalogSupport
@ -103,6 +103,17 @@ public class CatalogSupport extends CatalogSupportBase {
testXInclude(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
Verifies that the Catalog is used when the handler is null. The test shall
run through without an Exception (that was thrown before the fix).
*/
@Test(dataProvider = "data_XIA")
public void testXIncludeA_NullHandler(boolean setUseCatalog, boolean useCatalog, String catalog,
String xml, MyHandler handler, String expected) throws Exception {
handler = null;
testXInclude(setUseCatalog, useCatalog, catalog, xml, handler, expected);
}
/*
Verifies the Catalog support on DOM parser.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -306,10 +306,13 @@ public class CatalogSupportBase {
public void testXInclude(boolean setUseCatalog, boolean useCatalog, String catalog,
String xml, MyHandler handler, String expected) throws Exception {
SAXParser parser = getSAXParser(setUseCatalog, useCatalog, catalog);
parser.parse(new InputSource(new StringReader(xml)), handler);
debugPrint("handler.result:" + handler.getResult());
Assert.assertEquals(handler.getResult().trim(), expected);
// the test verifies the result if handler != null, or no exception
// is thrown if handler == null.
if (handler != null) {
debugPrint("handler.result:" + handler.getResult());
Assert.assertEquals(handler.getResult().trim(), expected);
}
}
/*