Skip to content

Commit 9154468

Browse files
authored
Merge pull request #37 from evolvedbinary/6.x.x/hotfix/xmldb-serialize-array-map
[6.x.x] Allow XDM Array and Map types to be serialized over the XML:DB API
2 parents bb91d9b + cc315b3 commit 9154468

File tree

5 files changed

+96
-5
lines changed

5 files changed

+96
-5
lines changed

exist-core/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@
904904
<include>src/main/java/org/exist/xmldb/LocalXMLResource.java</include>
905905
<include>src/main/java/org/exist/xmldb/RemoteRestoreService.java</include>
906906
<include>src/test/java/org/exist/xmldb/ResourceTest.java</include>
907+
<include>src/test/java/org/exist/xmldb/SerializationTest.java</include>
907908
<include>src/test/java/org/exist/xmldb/TestEXistXMLSerialize.java</include>
908909
<include>src/test/java/org/exist/xmldb/TreeLevelOrderTest.java</include>
909910
<include>src/test/java/org/exist/xmldb/concurrent/XMLGenerator.java</include>
@@ -1240,6 +1241,7 @@
12401241
<exclude>src/main/java/org/exist/xmldb/LocalXMLResource.java</exclude>
12411242
<exclude>src/main/java/org/exist/xmldb/RemoteRestoreService.java</exclude>
12421243
<exclude>src/test/java/org/exist/xmldb/ResourceTest.java</exclude>
1244+
<exclude>src/test/java/org/exist/xmldb/SerializationTest.java</exclude>
12431245
<exclude>src/test/java/org/exist/xmldb/TestEXistXMLSerialize.java</exclude>
12441246
<exclude>src/test/java/org/exist/xmldb/TreeLevelOrderTest.java</exclude>
12451247
<exclude>src/test/java/org/exist/xmldb/concurrent/XMLGenerator.java</exclude>

exist-core/src/main/java/org/exist/xmldb/LocalXMLResource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ else if (root != null && !(root instanceof NodeValue)) {
155155
} else if (value != null) {
156156
try {
157157
if (Type.subTypeOf(value.getType(),Type.STRING)) {
158-
return ((StringValue)value).getStringValue(true);
158+
return ((StringValue) value).getStringValue(true);
159+
160+
} else if (Type.subTypeOf(value.getType(), Type.MAP) || Type.subTypeOf(value.getType(), Type.ARRAY)) {
161+
return value.toString();
162+
159163
} else {
160164
return value.getStringValue();
161165
}

exist-core/src/main/java/org/exist/xmlrpc/RpcConnection.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,10 @@ private Map<String, Object> queryResultToRpcResponse(final long startTime, final
18461846
entry.add(String.valueOf(((NodeImpl) next).getNodeNumber()));
18471847
}
18481848
result.add(entry);
1849+
1850+
} else if (Type.subTypeOf(next.getType(), Type.MAP) || Type.subTypeOf(next.getType(), Type.ARRAY)) {
1851+
result.add(next.toString());
1852+
18491853
} else {
18501854
result.add(next.getStringValue());
18511855
}
@@ -2011,7 +2015,14 @@ private Map<String, String> atomicMap(final Item item) throws XPathException {
20112015

20122016
final int type = item.getType();
20132017
result.put("type", Type.getTypeName(type));
2014-
result.put("value", item.getStringValue());
2018+
2019+
final String value;
2020+
if (Type.subTypeOf(item.getType(), Type.MAP) || Type.subTypeOf(item.getType(), Type.ARRAY)) {
2021+
value = item.toString();
2022+
} else {
2023+
value = item.getStringValue();
2024+
}
2025+
result.put("value", value);
20152026

20162027
return result;
20172028
}

exist-core/src/main/java/org/exist/xquery/functions/array/ArrayType.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,37 @@ public String toString() {
485485
builder.append(' ');
486486
}
487487
for (int i = 0; i < vector.length(); i++) {
488-
final Sequence value = vector.nth(i);
489-
builder.append(value.toString());
490-
if (i < vector.length() - 1) {
488+
489+
if (i > 0) {
491490
builder.append(", ");
492491
}
492+
493+
final Sequence sequence = vector.nth(i);
494+
495+
if (!sequence.hasOne()) {
496+
builder.append('(');
497+
}
498+
499+
for (int j = 0; j < sequence.getItemCount(); j++) {
500+
if (j > 0) {
501+
builder.append(", ");
502+
}
503+
504+
final Item item = sequence.itemAt(j);
505+
if (Type.subTypeOf(item.getType(), Type.STRING)) {
506+
builder.append('"');
507+
}
508+
509+
builder.append(item.toString());
510+
511+
if (Type.subTypeOf(item.getType(), Type.STRING)) {
512+
builder.append('"');
513+
}
514+
}
515+
516+
if (!sequence.hasOne()) {
517+
builder.append(')');
518+
}
493519
}
494520
if (vector.length() > 0) {
495521
builder.append(' ');

exist-core/src/test/java/org/exist/xmldb/SerializationTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -202,6 +226,30 @@ public void getDocTypeYes() throws XMLDBException {
202226
}
203227
}
204228

229+
@Test
230+
public void testArray() throws XMLDBException {
231+
final String query = "array { \"value 1\", \"value 2\" }";
232+
233+
final XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
234+
final ResourceSet result = service.query(query);
235+
assertEquals(1, result.getSize());
236+
237+
final Resource resource = result.getResource(0);
238+
assertEquals("[ \"value 1\", \"value 2\" ]", resource.getContent());
239+
}
240+
241+
@Test
242+
public void testMap() throws XMLDBException {
243+
final String query = "map { \"prop1\" : \"value 1\", \"prop2\" : \"value 2\" }";
244+
245+
final XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
246+
final ResourceSet result = service.query(query);
247+
assertEquals(1, result.getSize());
248+
249+
final Resource resource = result.getResource(0);
250+
assertEquals("map {\"prop2\": \"value 2\", \"prop1\": \"value 1\"}", resource.getContent());
251+
}
252+
205253
private static void assertXMLEquals(final String expected, final Resource actual) throws XMLDBException {
206254
final Source srcExpected = Input.fromString(expected).build();
207255
final Source srcActual = Input.fromString(actual.getContent().toString()).build();

0 commit comments

Comments
 (0)