Skip to content

Commit

Permalink
8290740: Catalog not used when the handler is null
Browse files Browse the repository at this point in the history
Reviewed-by: lancea, naoto, iris
  • Loading branch information
JoeWang-Java committed Jul 30, 2022
1 parent dd9bd31 commit 470c0eb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -128,7 +128,7 @@
*
*
* @see XIncludeNamespaceSupport
* @LastModified: May 2021
* @LastModified: July 2022
*/
public class XIncludeHandler
implements XMLComponent, XMLDocumentFilter, XMLDTDFilter {
Expand Down Expand Up @@ -1638,8 +1638,8 @@ protected boolean handleIncludeElement(XMLAttributes attributes)
}

XMLInputSource includedSource = null;
if (fEntityResolver != null) {
try {
try {
if (fEntityResolver != null) {
XMLResourceIdentifier resourceIdentifier =
new XMLResourceIdentifierImpl(
null,
Expand All @@ -1652,57 +1652,56 @@ protected boolean handleIncludeElement(XMLAttributes attributes)

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 (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 && 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 (includedSource != null &&
!(includedSource instanceof HTTPInputSource) &&
(accept != null || acceptLanguage != null) &&
includedSource.getCharacterStream() == null &&
includedSource.getByteStream() == null) {
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;
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.
Expand Down
15 changes: 13 additions & 2 deletions test/jaxp/javax/xml/jaxp/unittest/catalog/CatalogSupport.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,7 +41,7 @@

/**
* @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
Expand Down Expand Up @@ -103,6 +103,17 @@ public void testXIncludeA(boolean setUseCatalog, boolean useCatalog, String cata
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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -306,10 +306,13 @@ public void testXMLReader(boolean setUseCatalog, boolean useCatalog, String cata
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);
}
}

/*
Expand Down

1 comment on commit 470c0eb

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.