Skip to content

Commit 2c64411

Browse files
author
Nico Verwer
committed
Allow maps and arrays as parameters in fn:transform
1 parent f8f9dd7 commit 2c64411

File tree

1 file changed

+28
-2
lines changed
  • exist-core/src/main/java/org/exist/xquery/functions/fn/transform

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@
3131
import org.exist.xquery.XPathException;
3232
import org.exist.xquery.functions.array.ArrayType;
3333
import org.exist.xquery.functions.fn.FnTransform;
34+
import org.exist.xquery.functions.map.AbstractMapType;
3435
import org.exist.xquery.value.*;
3536
import org.w3c.dom.Document;
3637
import org.w3c.dom.Node;
3738

39+
import io.lacuna.bifurcan.IEntry;
40+
3841
import javax.xml.transform.dom.DOMSource;
3942
import java.util.ArrayList;
43+
import java.util.HashMap;
4044
import java.util.List;
45+
import java.util.Map;
4146

4247
/**
4348
* Type conversion to and from Saxon
@@ -128,6 +133,10 @@ XdmValue of(final Item item) throws XPathException {
128133
return ofAtomic((AtomicValue) item);
129134
} else if (Type.subTypeOf(itemType, Type.NODE)) {
130135
return ofNode((Node) item);
136+
} else if (Type.subTypeOf(itemType, Type.MAP)) {
137+
return ofMap((AbstractMapType) item);
138+
} else if (Type.subTypeOf(itemType, Type.ARRAY)) {
139+
return ofArray((ArrayType) item);
131140
}
132141
throw new XPathException(ErrorCodes.XPTY0004,
133142
"Item " + item + " of type " + Type.getTypeName(itemType) + COULD_NOT_BE_CONVERTED + "XdmValue");
@@ -144,14 +153,12 @@ static private XdmValue ofAtomic(final AtomicValue atomicValue) throws XPathExce
144153
} else if (Type.subTypeOf(itemType, Type.STRING)) {
145154
return XdmValue.makeValue(((StringValue) atomicValue).getStringValue());
146155
}
147-
148156
throw new XPathException(ErrorCodes.XPTY0004,
149157
"Atomic value " + atomicValue + " of type " + Type.getTypeName(itemType) +
150158
COULD_NOT_BE_CONVERTED + "XdmValue");
151159
}
152160

153161
private XdmValue ofNode(final Node node) throws XPathException {
154-
155162
final DocumentBuilder sourceBuilder = newDocumentBuilder();
156163
try {
157164
if (node instanceof DocumentImpl) {
@@ -171,6 +178,25 @@ private XdmValue ofNode(final Node node) throws XPathException {
171178
}
172179
}
173180

181+
private XdmValue ofMap(final AbstractMapType map) throws XPathException {
182+
Map<XdmAtomicValue, XdmValue> xdmMap = new HashMap<XdmAtomicValue, XdmValue>();
183+
for (IEntry<AtomicValue, Sequence> entry : map) {
184+
XdmAtomicValue key = (XdmAtomicValue) ofAtomic(entry.key());
185+
XdmValue value = of(entry.value());
186+
xdmMap.put(key, value);
187+
}
188+
return new XdmMap(xdmMap);
189+
}
190+
191+
private XdmValue ofArray(final ArrayType array) throws XPathException {
192+
int size = array.getSize();
193+
XdmValue[] members = new XdmValue[size];
194+
for (int i = 0; i < size; ++i) {
195+
members[i] = of(array.get(i));
196+
}
197+
return new XdmArray(members);
198+
}
199+
174200
XdmValue[] of(final ArrayType values) throws XPathException {
175201
final int size = values.getSize();
176202
final XdmValue[] result = new XdmValue[size];

0 commit comments

Comments
 (0)