Skip to content
Permalink
Browse files
8276141: XPathFactory set/getProperty method
Reviewed-by: rriggs, naoto, lancea, iris, alanb
  • Loading branch information
JoeWang-Java committed Dec 2, 2021
1 parent 09522db commit b226ab99c872e791d3ed9fca015cf92847904c34
@@ -36,7 +36,7 @@
*
* @author Ramesh Mandava
*
* @LastModified: May 2021
* @LastModified: Nov 2021
*/
public class XPathFactoryImpl extends XPathFactory {

@@ -310,4 +310,38 @@ public void setXPathVariableResolver(XPathVariableResolver resolver) {

xPathVariableResolver = resolver;
}

@Override
public void setProperty(String name, String value) {
// property name cannot be null
if (name == null) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_PROPERTY_NAME_NULL,
new Object[] {CLASS_NAME, value} );
throw new NullPointerException(fmsg);
}

// property name not recognized
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_PROPERTY_UNKNOWN,
new Object[] {name, CLASS_NAME, value} );
throw new IllegalArgumentException(fmsg);
}

@Override
public String getProperty(String name) {
// property name cannot be null
if (name == null) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_GETTING_NULL_PROPERTY,
new Object[] {CLASS_NAME} );
throw new NullPointerException(fmsg);
}

// unknown property
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_GETTING_UNKNOWN_PROPERTY,
new Object[] {name, CLASS_NAME} );
throw new IllegalArgumentException(fmsg);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -31,7 +31,7 @@
* Also you need to update the count of messages(MAX_CODE)or
* the count of warnings(MAX_WARNING) [ Information purpose only]
* @xsl.usage advanced
* @LastModified: May 2019
* @LastModified: Nov 2021
*/
public class XPATHErrorResources extends ListResourceBundle
{
@@ -322,6 +322,10 @@ public class XPATHErrorResources extends ListResourceBundle
public static final String ER_SECUREPROCESSING_FEATURE = "ER_SECUREPROCESSING_FEATURE";
public static final String ER_NULL_XPATH_FUNCTION_RESOLVER = "ER_NULL_XPATH_FUNCTION_RESOLVER";
public static final String ER_NULL_XPATH_VARIABLE_RESOLVER = "ER_NULL_XPATH_VARIABLE_RESOLVER";
public static final String ER_PROPERTY_NAME_NULL = "ER_PROPERTY_NAME_NULL";
public static final String ER_PROPERTY_UNKNOWN = "ER_PROPERTY_UNKNOWN";
public static final String ER_GETTING_NULL_PROPERTY = "ER_GETTING_NULL_PROPERTY";
public static final String ER_GETTING_UNKNOWN_PROPERTY = "ER_GETTING_UNKNOWN_PROPERTY";
//END: Keys needed for exception messages of JAXP 1.3 XPath API implementation

public static final String WG_LOCALE_NAME_NOT_HANDLED =
@@ -836,6 +840,26 @@ public class XPATHErrorResources extends ListResourceBundle
{ ER_NULL_XPATH_VARIABLE_RESOLVER,
"Attempting to set a null XPathVariableResolver:{0}#setXPathVariableResolver(null)"},

/** Field ER_PROPERTY_NAME_NULL */

{ ER_PROPERTY_NAME_NULL,
"Trying to set a property with a null name: {0}#setProperty( null, {1})"},

/** Field ER_PROPERTY_UNKNOWN */

{ ER_PROPERTY_UNKNOWN,
"Trying to set the unknown property \"{0}\":{1}#setProperty({0},{2})"},

/** Field ER_GETTING_NULL_PROPERTY */

{ ER_GETTING_NULL_PROPERTY,
"Trying to get a property with a null name: {0}#getProperty(null)"},

/** Field ER_GETTING_NULL_PROPERTY */

{ ER_GETTING_UNKNOWN_PROPERTY,
"Trying to get the unknown property \"{0}\":{1}#getProperty({0})"},

//END: Definitions of error keys used in exception messages of JAXP 1.3 XPath API implementation

// Warnings...
@@ -275,8 +275,9 @@ public static XPathFactory newInstance(String uri, String factoryClassName, Clas
public abstract boolean isObjectModelSupported(String objectModel);

/**
* <p>Set a feature for this {@code XPathFactory} and
* <code>XPath</code>s created by this factory.</p>
* Sets a feature for this {@code XPathFactory}. The feature applies to
* {@code XPath} objects that the {@code XPathFactory} creates. It has no
* impact on {@code XPath} objects that are already created.
*
* <p>
* Feature names are fully qualified {@link java.net.URI}s.
@@ -369,4 +370,60 @@ public abstract boolean getFeature(String name)
* @return New instance of an <code>XPath</code>.
*/
public abstract XPath newXPath();

/**
* Sets a property for this {@code XPathFactory}. The property applies to
* {@code XPath} objects that the {@code XPathFactory} creates. It has no
* impact on {@code XPath} objects that are already created.
* <p>
* A property can either be defined in this {@code XPathFactory}, or by the
* underlying implementation.
*
* @implSpec
* The default implementation throws
* {@link java.lang.UnsupportedOperationException}.
*
* @param name the property name
* @param value the value for the property
*
* @throws IllegalArgumentException if the property name is not recognized,
* or the value can not be assigned
* @throws UnsupportedOperationException if the implementation does not
* support the method
* @throws NullPointerException if the {@code name} is {@code null}
*
* @since 18
*/
public void setProperty(String name, String value) {

if (name == null) {
throw new NullPointerException("the name parameter is null");
}
throw new UnsupportedOperationException("not implemented");
}

/**
* Returns the value of the specified property.
*
* @implSpec
* The default implementation throws
* {@link java.lang.UnsupportedOperationException}.
*
* @param name the property name
* @return the value of the property.
*
* @throws IllegalArgumentException if the property name is not recognized
* @throws UnsupportedOperationException if the implementation does not
* support the method
* @throws NullPointerException if the {@code name} is {@code null}
*
* @since 18
*/
public String getProperty(String name) {

if (name == null) {
throw new NullPointerException("the name parameter is null");
}
throw new UnsupportedOperationException("not implemented");
}
}
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package xpath;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;
import javax.xml.xpath.XPathFunctionResolver;
import javax.xml.xpath.XPathVariableResolver;

/**
* A dummy implementation of the XPathFactory without implementing
* the setProperty/getProperty methods
*/
public class XPathFactoryDummyImpl extends XPathFactory {

@Override
public boolean isObjectModelSupported(String objectModel) {
// support the default object model, W3C DOM
if (objectModel.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
return true;
}

// no support
return false;
}


@Override
public void setFeature(String name, boolean value) throws XPathFactoryConfigurationException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean getFeature(String name) throws XPathFactoryConfigurationException {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void setXPathVariableResolver(XPathVariableResolver resolver) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void setXPathFunctionResolver(XPathFunctionResolver resolver) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public XPath newXPath() {
throw new UnsupportedOperationException("Not supported yet.");
}

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, 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
@@ -30,22 +30,77 @@
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/*
* @test
* @bug 6376058
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow xpath.XPathTest
* @bug 6376058 8276141
* @build xpath.XPathTest xpath.XPathFactoryDummyImpl
* @run testng/othervm xpath.XPathTest
* @summary Test XPath functions. See details for each test.
*/
@Listeners({jaxp.library.BasePolicy.class})
public class XPathTest {
/*
* DataProvider for testXPathFactory
*/
@DataProvider(name = "xpath")
public Object[][] getXPathFactory() throws Exception {
return new Object[][]{
{null},
{"xpath.XPathFactoryDummyImpl"},
};
}

/*
* @bug 8276141
* Tests the setProperty/getProperty method for the XPathFactory.
*/
@Test(dataProvider = "xpath")
public void testXPathFactory(String factoryName)
throws Exception {

XPathFactory xpf;
if (factoryName == null) {
xpf = XPathFactory.newInstance();
} else {
xpf = XPathFactory.newInstance(
XPathFactory.DEFAULT_OBJECT_MODEL_URI, factoryName, null);
}

// NPE
Assert.assertThrows(NullPointerException.class,
() -> setProperty(xpf, null, "value"));
Assert.assertThrows(NullPointerException.class,
() -> getProperty(xpf, null));

if (factoryName == null) {
// default factory impl
Assert.assertThrows(IllegalArgumentException.class,
() -> setProperty(xpf, "unknown", "value"));
Assert.assertThrows(IllegalArgumentException.class,
() -> getProperty(xpf, "unknown"));
} else {
// the DummyImpl does not implement the method
Assert.assertThrows(UnsupportedOperationException.class,
() -> setProperty(xpf, "unknown", "value"));
Assert.assertThrows(UnsupportedOperationException.class,
() -> getProperty(xpf, "unknown"));
}
}

private void setProperty(XPathFactory xpf, String name, String value)
throws Exception {
xpf.setProperty(name, value);
}

private void getProperty(XPathFactory xpf, String name)
throws Exception {
xpf.getProperty(name);
}

/*
@bug 6211561

3 comments on commit b226ab9

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on b226ab9 Dec 2, 2021

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on b226ab9 Feb 16, 2022

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on b226ab9 Feb 16, 2022

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-b226ab99 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit b226ab99 from the openjdk/jdk repository.

The commit being backported was authored by Joe Wang on 2 Dec 2021 and was reviewed by Roger Riggs, Naoto Sato, Lance Andersen, Iris Clark and Alan Bateman.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-b226ab99:GoeLin-backport-b226ab99
$ git checkout GoeLin-backport-b226ab99
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-b226ab99

Please sign in to comment.