31
31
import org .exist .xquery .XPathException ;
32
32
import org .exist .xquery .functions .array .ArrayType ;
33
33
import org .exist .xquery .functions .fn .FnTransform ;
34
+ import org .exist .xquery .functions .map .AbstractMapType ;
34
35
import org .exist .xquery .value .*;
35
36
import org .w3c .dom .Document ;
36
37
import org .w3c .dom .Node ;
37
38
39
+ import io .lacuna .bifurcan .IEntry ;
40
+
38
41
import javax .xml .transform .dom .DOMSource ;
39
42
import java .util .ArrayList ;
43
+ import java .util .HashMap ;
40
44
import java .util .List ;
45
+ import java .util .Map ;
41
46
42
47
/**
43
48
* Type conversion to and from Saxon
@@ -128,6 +133,10 @@ XdmValue of(final Item item) throws XPathException {
128
133
return ofAtomic ((AtomicValue ) item );
129
134
} else if (Type .subTypeOf (itemType , Type .NODE )) {
130
135
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 );
131
140
}
132
141
throw new XPathException (ErrorCodes .XPTY0004 ,
133
142
"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
144
153
} else if (Type .subTypeOf (itemType , Type .STRING )) {
145
154
return XdmValue .makeValue (((StringValue ) atomicValue ).getStringValue ());
146
155
}
147
-
148
156
throw new XPathException (ErrorCodes .XPTY0004 ,
149
157
"Atomic value " + atomicValue + " of type " + Type .getTypeName (itemType ) +
150
158
COULD_NOT_BE_CONVERTED + "XdmValue" );
151
159
}
152
160
153
161
private XdmValue ofNode (final Node node ) throws XPathException {
154
-
155
162
final DocumentBuilder sourceBuilder = newDocumentBuilder ();
156
163
try {
157
164
if (node instanceof DocumentImpl ) {
@@ -171,6 +178,25 @@ private XdmValue ofNode(final Node node) throws XPathException {
171
178
}
172
179
}
173
180
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
+
174
200
XdmValue [] of (final ArrayType values ) throws XPathException {
175
201
final int size = values .getSize ();
176
202
final XdmValue [] result = new XdmValue [size ];
0 commit comments