3737import static org .exist .xquery .FunctionDSL .*;
3838import static org .exist .xquery .functions .fn .FnModule .functionSignatures ;
3939
40- public class FunElementWithId extends Function {
40+ public class FunElementWithId extends BasicFunction {
4141 private static final String FN_NAME = "element-with-id" ;
4242 private static final String FN_DESCRIPTION =
4343 "Returns the sequence of element nodes that have an ID value " +
@@ -60,39 +60,35 @@ public FunElementWithId(final XQueryContext context, final FunctionSignature sig
6060 }
6161
6262 @ Override
63- public Sequence eval (Sequence contextSequence , final Item contextItem ) throws XPathException {
63+ public Sequence eval (final Sequence [] args , Sequence contextSequence ) throws XPathException {
6464 if (context .getProfiler ().isEnabled ()) {
6565 context .getProfiler ().start (this );
6666 context .getProfiler ().message (this , Profiler .DEPENDENCIES , "DEPENDENCIES" , Dependency .getDependenciesName (this .getDependencies ()));
67- if (contextSequence != null )
68- {context .getProfiler ().message (this , Profiler .START_SEQUENCES , "CONTEXT SEQUENCE" , contextSequence );}
69- if (contextItem != null )
70- {context .getProfiler ().message (this , Profiler .START_SEQUENCES , "CONTEXT ITEM" , contextItem .toSequence ());}
67+ if (contextSequence != null ) {
68+ context .getProfiler ().message (this , Profiler .START_SEQUENCES , "CONTEXT SEQUENCE" , contextSequence );
69+ }
7170 }
7271
7372 if (getArgumentCount () < 1 ) {
74- throw new XPathException (this , ErrorCodes .XPST0017 , "function element-with-id requires one argument" );
75- }
76- if (contextItem != null ) {
77- contextSequence = contextItem .toSequence ();
73+ throw new XPathException (this , ErrorCodes .XPST0017 , "function element-with-id requires one argument" );
7874 }
7975
8076 final Sequence result ;
8177 boolean processInMem = false ;
8278 final Expression arg = getArgument (0 );
83- final Sequence idval = arg .eval (contextSequence );
79+ final Sequence idval = arg .eval (contextSequence );
8480
85- if (idval .isEmpty () || (getArgumentCount () == 1 && contextSequence != null && contextSequence .isEmpty ())) {
81+ if (idval .isEmpty () || (getArgumentCount () == 1 && contextSequence != null && contextSequence .isEmpty ())) {
8682 result = Sequence .EMPTY_SEQUENCE ;
8783 } else {
88- String nextId ;
89- DocumentSet docs = null ;
84+ String nextId ;
85+ DocumentSet docs = null ;
9086 if (getArgumentCount () == 2 ) {
9187 final Sequence nodes = getArgument (1 ).eval (contextSequence );
9288 if (nodes .isEmpty ()) {
9389 throw new XPathException (this , ErrorCodes .XPDY0002 , "XPDY0002: no node or context item for fn:id" , nodes );
9490 } else if (!Type .subTypeOf (nodes .itemAt (0 ).getType (), Type .NODE )) {
95- throw new XPathException (this , ErrorCodes .XPTY0004 , "XPTY0004: fn:id() argument is not a node" , nodes );
91+ throw new XPathException (this , ErrorCodes .XPTY0004 , "XPTY0004: fn:id() argument is not a node" , nodes );
9692 }
9793 NodeValue node = (NodeValue )nodes .itemAt (0 );
9894 if (node .getImplementationType () == NodeValue .IN_MEMORY_NODE ) {
@@ -105,10 +101,10 @@ public Sequence eval(Sequence contextSequence, final Item contextItem) throws XP
105101 contextSequence = node ;
106102 } else if (contextSequence == null ) {
107103 throw new XPathException (this , ErrorCodes .XPDY0002 , "No context item specified" );
108- } else if (!Type .subTypeOf (contextSequence .getItemType (), Type .NODE )) {
109- throw new XPathException (this , ErrorCodes .XPTY0004 , "Context item is not a node" , contextSequence );
110- } else {
111- if (contextSequence .isPersistentSet ()) {
104+ } else if (!Type .subTypeOf (contextSequence .getItemType (), Type .NODE )) {
105+ throw new XPathException (this , ErrorCodes .XPTY0004 , "Context item is not a node" , contextSequence );
106+ } else {
107+ if (contextSequence .isPersistentSet ()) {
112108 docs = contextSequence .toNodeSet ().getDocumentSet ();
113109 } else {
114110 processInMem = true ;
@@ -122,7 +118,7 @@ public Sequence eval(Sequence contextSequence, final Item contextItem) throws XP
122118 }
123119
124120 for (final SequenceIterator i = idval .iterate (); i .hasNext (); ) {
125- nextId = i .nextItem ().getStringValue ();
121+ nextId = i .nextItem ().getStringValue ();
126122 if (!nextId .isEmpty ()) {
127123 if (nextId .indexOf (' ' ) != Constants .STRING_NOT_FOUND ) {
128124 final StringTokenizer tok = new StringTokenizer (nextId , " " );
@@ -146,15 +142,15 @@ public Sequence eval(Sequence contextSequence, final Item contextItem) throws XP
146142 }
147143 }
148144 }
149- }
145+ }
150146 }
151- result .removeDuplicates ();
147+ result .removeDuplicates ();
152148 if (context .getProfiler ().isEnabled ()) {
153149 context .getProfiler ().end (this , "" , result );
154150 }
155151
156152 return result ;
157- }
153+ }
158154
159155 private void getId (final NodeSet result , final DocumentSet docs , final String id ) throws XPathException {
160156 final NodeSet attribs = context .getBroker ().getValueIndex ().find (context .getWatchDog (), Comparison .EQ , docs , null , -1 , null , new StringValue (id , Type .ID ));
0 commit comments