Skip to content

Commit 65210de

Browse files
committed
[bugfix] Correct the null namespace check
1 parent 2454ca5 commit 65210de

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
<groupId>net.sf.saxon</groupId>
3535
<artifactId>Saxon-HE</artifactId>
3636
</dependency>
37+
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<scope>test</scope>
42+
</dependency>
3743
</dependencies>
3844

3945
</project>

src/main/java/org/expath/tools/saxon/model/SaxonSequence.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.expath.tools.saxon.util.SequenceIteratorFactory;
2727
import org.expath.tools.serial.SerialParameters;
2828

29+
import static javax.xml.XMLConstants.NULL_NS_URI;
30+
2931
/**
3032
* Saxon implementation of {@link Sequence}, relying on {@link SequenceIterator}.
3133
*
@@ -185,37 +187,37 @@ private void setOutputKey(Properties props, String name, SerialParameters.Standa
185187
}
186188
}
187189

188-
private void setOutputKey(Properties props, String name, QName value)
190+
void setOutputKey(Properties props, String name, QName value)
189191
throws ToolsException
190192
{
191193
if ( value != null ) {
192-
if ( value.getNamespaceURI() != null ) {
193-
throw new ToolsException(
194-
"A QName with a non-null namespace not supported as a serialization param: {"
195-
+ value.getNamespaceURI() + "}" + value.getLocalPart());
196-
}
194+
checkForNullNs(value);
197195
props.setProperty(name, value.getLocalPart());
198196
}
199197
}
200198

201-
private void setOutputKey(Properties props, String name, Iterable<QName> value)
199+
void setOutputKey(Properties props, String name, Iterable<QName> value)
202200
throws ToolsException
203201
{
204202
if ( value != null ) {
205203
StringBuilder buf = new StringBuilder();
206204
for ( QName qname : value ) {
207-
if ( qname.getNamespaceURI() != null ) {
208-
throw new ToolsException(
209-
"A QName with a non-null namespace not supported as a serialization param: {"
210-
+ qname.getNamespaceURI() + "}" + qname.getLocalPart());
211-
}
205+
checkForNullNs(qname);
212206
buf.append(qname.getLocalPart());
213207
buf.append(" ");
214208
}
215209
props.setProperty(name, buf.toString());
216210
}
217211
}
218212

213+
private static void checkForNullNs(final QName qname) throws ToolsException {
214+
if ( qname.getNamespaceURI() != null && !NULL_NS_URI.equals(qname.getNamespaceURI()) ) {
215+
throw new ToolsException(
216+
"A QName with a non-null namespace not supported as a serialization param: {"
217+
+ qname.getNamespaceURI() + "}" + qname.getLocalPart());
218+
}
219+
}
220+
219221
private final SequenceIteratorFactory myItFactory;
220222
private final SequenceIterator myIt;
221223
private final XPathContext myCtxt;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/****************************************************************************/
2+
/* File: SaxonSequenceTest.java */
3+
/* Author: A. Retter - adamretter.org.uk */
4+
/* Date: 2024-06-28 */
5+
/* Tags: */
6+
/* Copyright (c) 2024 Adam Retter (see end of file.) */
7+
/* ------------------------------------------------------------------------ */
8+
9+
10+
package org.expath.tools.saxon.model;
11+
12+
import net.sf.saxon.trans.XPathException;
13+
import org.expath.tools.ToolsException;
14+
import org.junit.Test;
15+
16+
import javax.xml.namespace.QName;
17+
import javax.xml.transform.OutputKeys;
18+
import java.util.Properties;
19+
20+
import static javax.xml.XMLConstants.NULL_NS_URI;
21+
import static org.junit.Assert.assertEquals;
22+
23+
public class SaxonSequenceTest {
24+
25+
@Test
26+
public void setOutputKeyNullNs() throws XPathException, ToolsException {
27+
final SaxonSequence saxonSequence = new SaxonSequence(null, null);
28+
29+
final Properties properties = new Properties();
30+
31+
QName expected = new QName("xml");
32+
saxonSequence.setOutputKey(properties, OutputKeys.METHOD, expected);
33+
assertEquals(expected.getLocalPart(), properties.get(OutputKeys.METHOD));
34+
35+
expected = new QName(NULL_NS_URI, "xml");
36+
saxonSequence.setOutputKey(properties, OutputKeys.METHOD, expected);
37+
assertEquals(expected.getLocalPart(), properties.get(OutputKeys.METHOD));
38+
39+
expected = new QName(null, "xml");
40+
saxonSequence.setOutputKey(properties, OutputKeys.METHOD, expected);
41+
assertEquals(expected.getLocalPart(), properties.get(OutputKeys.METHOD));
42+
}
43+
44+
@Test(expected = ToolsException.class)
45+
public void setOutputKeyNonNullNs() throws XPathException, ToolsException {
46+
final SaxonSequence saxonSequence = new SaxonSequence(null, null);
47+
48+
final Properties properties = new Properties();
49+
50+
final QName expected = new QName("urn:test:ns", "xml");
51+
saxonSequence.setOutputKey(properties, OutputKeys.METHOD, expected);
52+
}
53+
}
54+
55+
56+
/* ------------------------------------------------------------------------ */
57+
/* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT. */
58+
/* */
59+
/* The contents of this file are subject to the Mozilla Public License */
60+
/* Version 1.0 (the "License"); you may not use this file except in */
61+
/* compliance with the License. You may obtain a copy of the License at */
62+
/* http://www.mozilla.org/MPL/. */
63+
/* */
64+
/* Software distributed under the License is distributed on an "AS IS" */
65+
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
66+
/* the License for the specific language governing rights and limitations */
67+
/* under the License. */
68+
/* */
69+
/* The Original Code is: all this file. */
70+
/* */
71+
/* The Initial Developer of the Original Code is Adam Retter. */
72+
/* */
73+
/* Contributor(s): none. */
74+
/* ------------------------------------------------------------------------ */

0 commit comments

Comments
 (0)