4949import java .math .BigInteger ;
5050import java .net .URISyntaxException ;
5151import java .util .ArrayList ;
52+ import java .util .Arrays ;
5253import java .util .List ;
5354import java .util .Map ;
5455
@@ -605,7 +606,7 @@ public static final Sequence javaObjectToXPath(final Object obj, final XQueryCon
605606 * directly returned, other objects are converted into the corresponding
606607 * internal types.
607608 *
608- * @param obj The java object.
609+ * @param obj The java object. A Java Array or List will be interpreted as an XDM Sequence.
609610 * @param context XQuery context.
610611 * @param expression the expression from which the object derives.
611612 *
@@ -641,7 +642,7 @@ public static final Sequence javaObjectToXPath(final Object obj, final XQueryCon
641642 * directly returned, other objects are converted into the corresponding
642643 * internal types.
643644 *
644- * @param obj The java object
645+ * @param obj The java object. A Java Array or List will be interpreted as an XDM Sequence.
645646 * @param context XQuery context
646647 * @param expandChars true if characters should be expanded, false otherwise.
647648 * @param expression the expression from which the object derives.
@@ -662,8 +663,8 @@ public static final Sequence javaObjectToXPath(final Object obj, final XQueryCon
662663 * @param obj The java object.
663664 * @param context XQuery context.
664665 * @param expandChars true if characters should be expanded, false otherwise.
665- * @param listToSequence true if lists should be converted to sequences, false otherwise.
666- * @param arrayToSequence true if arrays should be converted to sequences, false otherwise.
666+ * @param listToSequence true if Java Lists should be converted to sequences, false otherwise.
667+ * @param arrayToSequence true if Java Arrays should be converted to sequences, false otherwise.
667668 * @param expression the expression from which the object derives.
668669 *
669670 * @return the XDM sequence.
@@ -741,21 +742,8 @@ public static final Sequence javaObjectToXPath(final Object obj, final XQueryCon
741742 SerializerPool .getInstance ().returnObject (streamer );
742743 }
743744
744- } else if (listToSequence && obj instanceof List <?>) {
745- boolean createNodeSequence = true ;
746-
747- final List <?> lst = (List <?>) obj ;
748- for (final Object next : lst ) {
749- if (!(next instanceof NodeProxy )) {
750- createNodeSequence = false ;
751- break ;
752- }
753- }
754- final Sequence seq = createNodeSequence ? new AVLTreeNodeSet () : new ValueSequence (lst .size ());
755- for (final Object o : lst ) {
756- seq .add ((Item ) javaObjectToXPath (o , context , expandChars , listToSequence , arrayToSequence , expression ));
757- }
758- return seq ;
745+ } else if (listToSequence && obj instanceof List <?> list ) {
746+ return javaArrayToXPath ((List <Object >) list , context , expandChars , listToSequence , arrayToSequence , expression );
759747
760748 } else if (obj instanceof NodeList ) {
761749 context .pushDocumentContext ();
@@ -786,20 +774,7 @@ public static final Sequence javaObjectToXPath(final Object obj, final XQueryCon
786774 }
787775
788776 } else if (arrayToSequence && obj instanceof Object [] array ) {
789- boolean createNodeSequence = true ;
790- for (Object arrayItem : array ) {
791- if (!(arrayItem instanceof NodeProxy )) {
792- createNodeSequence = false ;
793- break ;
794- }
795- }
796-
797- final Sequence seq = createNodeSequence ? new AVLTreeNodeSet () : new ValueSequence ();
798- for (final Object arrayItem : array ) {
799- seq .add ((Item ) javaObjectToXPath (arrayItem , context , expandChars , listToSequence , arrayToSequence , expression ));
800- }
801- return seq ;
802-
777+ return javaArrayToXPath (Arrays .asList (array ), context , expandChars , listToSequence , arrayToSequence , expression );
803778 }
804779
805780 final int xdmType = javaClassToXdmType (obj .getClass ());
@@ -906,6 +881,23 @@ public static final Sequence javaObjectToXPath(final Object obj, final XQueryCon
906881 }
907882 }
908883
884+ private static Sequence javaArrayToXPath (final Iterable <Object > objects , final XQueryContext context , final boolean expandChars , final boolean listToSequence , final boolean arrayToSequence , final Expression expression ) throws XPathException {
885+ boolean createNodeSequence = true ;
886+ for (final Object object : objects ) {
887+ if (!(object instanceof NodeProxy )) {
888+ createNodeSequence = false ;
889+ break ;
890+ }
891+ }
892+
893+ final Sequence result = createNodeSequence ? new AVLTreeNodeSet () : new ValueSequence ();
894+ for (final Object object : objects ) {
895+ final Sequence seq = javaObjectToXPath (object , context , expandChars , listToSequence , arrayToSequence , expression );
896+ result .addAll (seq );
897+ }
898+ return result ;
899+ }
900+
909901 /**
910902 * Converts an XMLResource into a NodeProxy.
911903 *
0 commit comments