Skip to content

Commit 623615e

Browse files
committed
fn:transform - code quality fixes
1 parent 36e490e commit 623615e

File tree

2 files changed

+85
-49
lines changed

2 files changed

+85
-49
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/transform/Options.java

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.w3c.dom.Node;
4848

4949
import javax.annotation.Nullable;
50-
import javax.xml.XMLConstants;
5150
import javax.xml.stream.XMLEventReader;
5251
import javax.xml.stream.XMLInputFactory;
5352
import javax.xml.stream.XMLStreamConstants;
@@ -166,7 +165,7 @@ private Map<QName, XdmValue> readParamsMap(final Optional<MapType> option, final
166165
} catch (final XPathException e) {
167166
throw new XPathException(fnTransform, ErrorCodes.FOXT0002, "Supplied xslt-version is not a valid xs:decimal: " + e.getMessage(), explicitXsltVersion.get(), e);
168167
}
169-
if (xsltVersion != v1_0 && xsltVersion != v2_0 && xsltVersion != v3_0) {
168+
if (xsltVersion != V1_0 && xsltVersion != V2_0 && xsltVersion != V3_0) {
170169
throw new XPathException(fnTransform, ErrorCodes.FOXT0001, "Supplied xslt-version is an unknown XSLT version: " + explicitXsltVersion.get());
171170
}
172171
} else {
@@ -290,72 +289,72 @@ private void validateRequestedProperties(final MapType requestedProperties) thro
290289
}
291290
}
292291
private static final Option<StringValue> BASE_OUTPUT_URI = new ItemOption<>(
293-
Type.STRING, "base-output-uri", v1_0, v2_0, v3_0);
292+
Type.STRING, "base-output-uri", V1_0, V2_0, V3_0);
294293
private static final Option<BooleanValue> CACHE = new ItemOption<>(
295-
Type.BOOLEAN, "cache", BooleanValue.TRUE, v1_0, v2_0, v3_0);
294+
Type.BOOLEAN, "cache", BooleanValue.TRUE, V1_0, V2_0, V3_0);
296295
private static final Option<StringValue> DELIVERY_FORMAT = new ItemOption<>(
297-
Type.STRING, "delivery-format", new StringValue("document"), v1_0, v2_0, v3_0);
296+
Type.STRING, "delivery-format", new StringValue("document"), V1_0, V2_0, V3_0);
298297
private static final Option<BooleanValue> ENABLE_ASSERTIONS = new ItemOption<>(
299-
Type.BOOLEAN, "enable-assertions", BooleanValue.FALSE, v3_0);
298+
Type.BOOLEAN, "enable-assertions", BooleanValue.FALSE, V3_0);
300299
private static final Option<BooleanValue> ENABLE_MESSAGES = new ItemOption<>(
301-
Type.BOOLEAN, "enable-messages", BooleanValue.TRUE, v1_0, v2_0, v3_0);
300+
Type.BOOLEAN, "enable-messages", BooleanValue.TRUE, V1_0, V2_0, V3_0);
302301
private static final Option<BooleanValue> ENABLE_TRACE = new ItemOption<>(
303-
Type.BOOLEAN, "enable-trace", BooleanValue.TRUE, v2_0, v3_0);
302+
Type.BOOLEAN, "enable-trace", BooleanValue.TRUE, V2_0, V3_0);
304303
private static final Option<ArrayType> FUNCTION_PARAMS = new ItemOption<>(
305-
Type.ARRAY,"function-params", v3_0);
304+
Type.ARRAY,"function-params", V3_0);
306305
private static final Option<Item> GLOBAL_CONTEXT_ITEM = new ItemOption<>(
307-
Type.ITEM, "global-context-item", v3_0);
306+
Type.ITEM, "global-context-item", V3_0);
308307
static final Option<QNameValue> INITIAL_FUNCTION = new ItemOption<>(
309-
Type.QNAME,"initial-function", v3_0);
308+
Type.QNAME,"initial-function", V3_0);
310309
static final Option<Sequence> INITIAL_MATCH_SELECTION = new SequenceOption<>(
311-
Type.ATOMIC,Type.NODE, "initial-match-selection", v3_0);
310+
Type.ATOMIC,Type.NODE, "initial-match-selection", V3_0);
312311
static final Option<QNameValue> INITIAL_MODE = new ItemOption<>(
313-
Type.QNAME,"initial-mode", v1_0, v2_0, v3_0);
312+
Type.QNAME,"initial-mode", V1_0, V2_0, V3_0);
314313
static final Option<QNameValue> INITIAL_TEMPLATE = new ItemOption<>(
315-
Type.QNAME,"initial-template", v2_0, v3_0);
314+
Type.QNAME,"initial-template", V2_0, V3_0);
316315
private static final Option<StringValue> PACKAGE_NAME = new ItemOption<>(
317-
Type.STRING,"package-name", v3_0);
316+
Type.STRING,"package-name", V3_0);
318317
private static final Option<StringValue> PACKAGE_LOCATION = new ItemOption<>(
319-
Type.STRING,"package-location", v3_0);
318+
Type.STRING,"package-location", V3_0);
320319
private static final Option<NodeValue> PACKAGE_NODE = new ItemOption<>(
321-
Type.NODE,"package-node", v3_0);
320+
Type.NODE,"package-node", V3_0);
322321
private static final Option<StringValue> PACKAGE_TEXT = new ItemOption<>(
323-
Type.STRING,"package-text", v3_0);
322+
Type.STRING,"package-text", V3_0);
324323
private static final Option<StringValue> PACKAGE_VERSION = new ItemOption<>(
325-
Type.STRING,"package-version", new StringValue("*"), v3_0);
324+
Type.STRING,"package-version", new StringValue("*"), V3_0);
326325
private static final Option<FunctionReference> POST_PROCESS = new ItemOption<>(
327-
Type.FUNCTION_REFERENCE,"post-process", v1_0, v2_0, v3_0);
326+
Type.FUNCTION_REFERENCE,"post-process", V1_0, V2_0, V3_0);
328327
private static final Option<MapType> REQUESTED_PROPERTIES = new ItemOption<>(
329-
Type.MAP,"requested-properties", v1_0, v2_0, v3_0);
328+
Type.MAP,"requested-properties", V1_0, V2_0, V3_0);
330329
private static final Option<MapType> SERIALIZATION_PARAMS = new ItemOption<>(
331-
Type.MAP,"serialization-params", v1_0, v2_0, v3_0);
330+
Type.MAP,"serialization-params", V1_0, V2_0, V3_0);
332331
static final Option<NodeValue> SOURCE_NODE = new ItemOption<>(
333-
Type.NODE,"source-node", v1_0, v2_0, v3_0);
332+
Type.NODE,"source-node", V1_0, V2_0, V3_0);
334333
private static final Option<MapType> STATIC_PARAMS = new ItemOption<>(
335-
Type.MAP,"static-params", v3_0);
334+
Type.MAP,"static-params", V3_0);
336335
private static final Option<StringValue> STYLESHEET_BASE_URI = new ItemOption<>(
337-
Type.STRING, "stylesheet-base-uri", v1_0, v2_0, v3_0);
336+
Type.STRING, "stylesheet-base-uri", V1_0, V2_0, V3_0);
338337
static final Option<StringValue> STYLESHEET_LOCATION = new ItemOption<>(
339-
Type.STRING,"stylesheet-location", v1_0, v2_0, v3_0);
338+
Type.STRING,"stylesheet-location", V1_0, V2_0, V3_0);
340339
static final Option<NodeValue> STYLESHEET_NODE = new ItemOption<>(
341-
Type.NODE,"stylesheet-node", v1_0, v2_0, v3_0);
340+
Type.NODE,"stylesheet-node", V1_0, V2_0, V3_0);
342341
private static final Option<MapType> STYLESHEET_PARAMS = new ItemOption<>(
343-
Type.MAP,"stylesheet-params", v1_0, v2_0, v3_0);
342+
Type.MAP,"stylesheet-params", V1_0, V2_0, V3_0);
344343
static final Option<StringValue> STYLESHEET_TEXT = new ItemOption<>(
345-
Type.STRING,"stylesheet-text", v1_0, v2_0, v3_0);
344+
Type.STRING,"stylesheet-text", V1_0, V2_0, V3_0);
346345
private static final Option<MapType> TEMPLATE_PARAMS = new ItemOption<>(
347-
Type.MAP,"template-params", v3_0);
346+
Type.MAP,"template-params", V3_0);
348347
private static final Option<MapType> TUNNEL_PARAMS = new ItemOption<>(
349-
Type.MAP,"tunnel-params", v3_0);
348+
Type.MAP,"tunnel-params", V3_0);
350349
private static final Option<MapType> VENDOR_OPTIONS = new ItemOption<>(
351-
Type.MAP,"vendor-options", v1_0, v2_0, v3_0);
350+
Type.MAP,"vendor-options", V1_0, V2_0, V3_0);
352351
private static final Option<DecimalValue> XSLT_VERSION = new ItemOption<>(
353-
Type.DECIMAL,"xslt-version", v1_0, v2_0, v3_0);
352+
Type.DECIMAL,"xslt-version", V1_0, V2_0, V3_0);
354353

355-
static abstract class Option<T> {
356-
public static final float v1_0 = 1.0f;
357-
public static final float v2_0 = 2.0f;
358-
public static final float v3_0 = 3.0f;
354+
abstract static class Option<T> {
355+
public static final float V1_0 = 1.0f;
356+
public static final float V2_0 = 2.0f;
357+
public static final float V3_0 = 3.0f;
359358

360359
protected final StringValue name;
361360
protected final Optional<T> defaultValue;
@@ -407,18 +406,16 @@ public SequenceOption(final int sequenceSubtype, final int itemSubtype, final St
407406
public Optional<T> get(final MapType options) throws XPathException {
408407
if (options.contains(name)) {
409408
final Sequence sequence = options.get(name);
410-
if (sequence != null) {
411-
if (Type.subTypeOf(sequence.getItemType(), sequenceSubtype)) {
412-
return Optional.of((T) sequence);
413-
}
414-
final Item item0 = options.get(name).itemAt(0);
415-
if (item0 != null) {
416-
if (Type.subTypeOf(item0.getType(), itemSubtype)) {
417-
return Optional.of((T) item0);
418-
} else {
419-
throw new XPathException(
420-
ErrorCodes.XPTY0004, "Type error: expected " + Type.getTypeName(itemSubtype) + ", got " + Type.getTypeName(sequence.getItemType()));
421-
}
409+
if (Type.subTypeOf(sequence.getItemType(), sequenceSubtype)) {
410+
return Optional.of((T) sequence);
411+
}
412+
final Item item0 = options.get(name).itemAt(0);
413+
if (item0 != null) {
414+
if (Type.subTypeOf(item0.getType(), itemSubtype)) {
415+
return Optional.of((T) item0);
416+
} else {
417+
throw new XPathException(
418+
ErrorCodes.XPTY0004, "Type error: expected " + Type.getTypeName(itemSubtype) + ", got " + Type.getTypeName(sequence.getItemType()));
422419
}
423420
}
424421
}

exist-core/src/test/xquery/xquery3/transform/fnTransformParams.xqm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ function testTransform:transform-stylesheet-invalid-xslt-version() {
109109
return (contains(string-join(map:keys($result)),"section2"))
110110
};
111111

112+
declare
113+
%test:assertError("err:FOXT0002")
114+
function testTransform:transform-stylesheet-source-node-and-initial-match() {
115+
let $xsl := $testTransform:transform-33-xsl
116+
let $xml := $testTransform:transform-33-xml
117+
let $result := fn:transform(map {"stylesheet-text": $xsl, "source-node": parse-xml($xml),
118+
"initial-match-selection" : 1 to 5,
119+
"delivery-format":"serialized"})
120+
return (contains(string-join(map:keys($result)),"section2"))
121+
};
122+
112123
(: cannot have stylesheet-node and stylesheet-text :)
113124
declare
114125
%test:assertError("err:FOXT0002")
@@ -130,3 +141,31 @@ function testTransform:transform-stylesheet-not-provided() {
130141
"stylesheet-base-uri": "http://www.example.com"})
131142
return $result?output
132143
};
144+
145+
(: a bad requested property type :)
146+
declare
147+
%test:assertError("err:XPTY0004")
148+
function testTransform:transform-stylesheet-bad-delivery() {
149+
let $xsl := $testTransform:transform-33-xsl
150+
let $xml := $testTransform:transform-33-xml
151+
let $result := fn:transform(map {"stylesheet-text": $xsl, "source-node": parse-xml($xml),
152+
"base-output-uri" : resolve-uri("transform/sandbox/fn-transform-33.xml", "http://www.w3.org/fots/fn/transform/staticbaseuri.xsl"),
153+
"requested-properties" : map{fn:QName('http://www.w3.org/1999/XSL/Transform','supports-dynamic-evaluation'): 2.5 }})
154+
return (contains(string-join(map:keys($result)),"section2"))
155+
};
156+
157+
158+
(: a bad requested property key :)
159+
declare
160+
%test:assertError("err:XPTY0004")
161+
function testTransform:transform-stylesheet-bad-delivery-key-not-qname() {
162+
let $xsl := $testTransform:transform-33-xsl
163+
let $xml := $testTransform:transform-33-xml
164+
let $result := fn:transform(map {"stylesheet-text": $xsl, "source-node": parse-xml($xml),
165+
"base-output-uri" : resolve-uri("transform/sandbox/fn-transform-33.xml", "http://www.w3.org/fots/fn/transform/staticbaseuri.xsl"),
166+
"requested-properties" : map{'supports-dynamic-evaluation': false() }})
167+
return (contains(string-join(map:keys($result)),"section2"))
168+
};
169+
170+
171+

0 commit comments

Comments
 (0)