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 *
2751import org .exist .xquery .value .Item ;
2852import org .exist .xquery .value .Sequence ;
2953
54+ import javax .annotation .Nullable ;
55+
3056/**
3157 * Runtime-check for the cardinality of a function parameter.
3258 *
@@ -47,52 +73,71 @@ public DynamicCardinalityCheck(final XQueryContext context, final Cardinality re
4773 setLocation (expression .getLine (), expression .getColumn ());
4874 }
4975
50- /* (non-Javadoc)
51- * @see org.exist.xquery.Expression#analyze(org.exist.xquery.Expression)
52- */
53- public void analyze (AnalyzeContextInfo contextInfo ) throws XPathException {
76+ @ Override
77+ public void analyze (final AnalyzeContextInfo contextInfo ) throws XPathException {
5478 contextInfo .setParent (this );
5579 expression .analyze (contextInfo );
5680 }
5781
58- /* (non-Javadoc)
59- * @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
60- */
61- public Sequence eval (Sequence contextSequence , Item contextItem ) throws XPathException {
82+ @ Override
83+ public Sequence eval (final Sequence contextSequence , final Item contextItem ) throws XPathException {
6284 if (context .getProfiler ().isEnabled ()) {
6385 context .getProfiler ().start (this );
64- context .getProfiler ().message (this , Profiler .DEPENDENCIES ,
65- "DEPENDENCIES" , Dependency .getDependenciesName (this .getDependencies ()));
66- if (contextSequence != null )
67- {context .getProfiler ().message (this , Profiler .START_SEQUENCES ,
68- "CONTEXT SEQUENCE" , contextSequence );}
69- if (contextItem != null )
70- {context .getProfiler ().message (this , Profiler .START_SEQUENCES ,
71- "CONTEXT ITEM" , contextItem .toSequence ());}
86+ context .getProfiler ().message (this , Profiler .DEPENDENCIES , "DEPENDENCIES" , Dependency .getDependenciesName (this .getDependencies ()));
87+ if (contextSequence != null ) {
88+ context .getProfiler ().message (this , Profiler .START_SEQUENCES , "CONTEXT SEQUENCE" , contextSequence );
89+ }
90+ if (contextItem != null ) {
91+ context .getProfiler ().message (this , Profiler .START_SEQUENCES , "CONTEXT ITEM" , contextItem .toSequence ());
92+ }
7293 }
94+
7395 final Sequence seq = expression .eval (contextSequence , contextItem );
74- Cardinality actualCardinality ;
75- if (seq .isEmpty ())
76- {actualCardinality = Cardinality .EMPTY_SEQUENCE ;}
77- else if (seq .hasMany ())
78- {actualCardinality = Cardinality ._MANY ;}
79- else
80- {actualCardinality = Cardinality .EXACTLY_ONE ;}
96+
97+ final Cardinality actualCardinality ;
98+ if (isEmpty (seq )) {
99+ actualCardinality = Cardinality .EMPTY_SEQUENCE ;
100+ } else if (hasMany (seq )) {
101+ actualCardinality = Cardinality ._MANY ;
102+ } else {
103+ actualCardinality = Cardinality .EXACTLY_ONE ;
104+ }
105+
81106 if (!requiredCardinality .isSuperCardinalityOrEqualOf (actualCardinality )) {
82- error .addArgs (ExpressionDumper .dump (expression ),
83- requiredCardinality .getHumanDescription (),
84- seq .getItemCount ());
107+ error .addArgs (ExpressionDumper .dump (expression ), requiredCardinality .getHumanDescription (), seq .getItemCount ());
85108 throw new XPathException (this , error .toString ());
86109 }
87- if (context .getProfiler ().isEnabled ())
88- {context .getProfiler ().end (this , "" , seq );}
110+
111+ if (context .getProfiler ().isEnabled ()) {
112+ context .getProfiler ().end (this , "" , seq );
113+ }
114+
89115 return seq ;
90116 }
91117
92- /* (non-Javadoc)
93- * @see org.exist.xquery.Expression#dump(org.exist.xquery.util.ExpressionDumper)
94- */
95- public void dump (ExpressionDumper dumper ) {
118+ private boolean isEmpty (final Sequence sequence ) throws XPathException {
119+ final boolean empty = sequence .isEmpty ();
120+ throwIfDeferredFunctionCallAndHasException (sequence );
121+ return empty ;
122+ }
123+
124+ private boolean hasMany (final Sequence sequence ) throws XPathException {
125+ final boolean hasMany = sequence .hasMany ();
126+ throwIfDeferredFunctionCallAndHasException (sequence );
127+ return hasMany ;
128+ }
129+
130+ private void throwIfDeferredFunctionCallAndHasException (final Sequence sequence ) throws XPathException {
131+ if (sequence instanceof DeferredFunctionCall ) {
132+ @ Nullable final XPathException caughtException = ((DeferredFunctionCall ) sequence ).getCaughtException ();
133+ if (caughtException != null ) {
134+ throw caughtException ;
135+ }
136+ }
137+ }
138+
139+ @ Override
140+ public void dump (final ExpressionDumper dumper ) {
96141 if (dumper .verbosity () > 1 ) {
97142 dumper .display ("dynamic-cardinality-check" );
98143 dumper .display ("(" );
@@ -108,44 +153,42 @@ public String toString() {
108153 return expression .toString ();
109154 }
110155
111- /* (non-Javadoc)
112- * @see org.exist.xquery.Expression#returnsType()
113- */
156+ @ Override
114157 public int returnsType () {
115158 return expression .returnsType ();
116159 }
117160
118- /* (non-Javadoc)
119- * @see org.exist.xquery.AbstractExpression#getDependencies()
120- */
161+ @ Override
121162 public int getDependencies () {
122163 return expression .getDependencies ();
123164 }
124165
125- public void setContextDocSet (DocumentSet contextSet ) {
166+ public void setContextDocSet (final DocumentSet contextSet ) {
126167 super .setContextDocSet (contextSet );
127168 expression .setContextDocSet (contextSet );
128169 }
129170
130- /* (non-Javadoc)
131- * @see org.exist.xquery.AbstractExpression#resetState()
132- */
133- public void resetState (boolean postOptimization ) {
171+ @ Override
172+ public void resetState (final boolean postOptimization ) {
134173 super .resetState (postOptimization );
135174 expression .resetState (postOptimization );
136175 }
137176
138- public void accept (ExpressionVisitor visitor ) {
177+ @ Override
178+ public void accept (final ExpressionVisitor visitor ) {
139179 expression .accept (visitor );
140180 }
141181
182+ @ Override
142183 public int getSubExpressionCount () {
143184 return 1 ;
144185 }
145-
146- public Expression getSubExpression (int index ) {
147- if (index == 0 )
148- {return expression ;}
149- throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " +getSubExpressionCount ());
186+
187+ @ Override
188+ public Expression getSubExpression (final int index ) {
189+ if (index == 0 ) {
190+ return expression ;
191+ }
192+ throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + getSubExpressionCount ());
150193 }
151194}
0 commit comments