Catch clause. @author BaseX Team, BSD License @author Christian Gruen
| 23 | * @author Christian Gruen |
| 24 | */ |
| 25 | public final class Catch extends Single { |
| 26 | /** Error tests. */ |
| 27 | private final ArrayList<Test> tests; |
| 28 | /** Error variables. */ |
| 29 | private final Var[] vars; |
| 30 | |
| 31 | /** |
| 32 | * Constructor. |
| 33 | * @param info input info (can be {@code null}) |
| 34 | * @param expr expression |
| 35 | * @param vars variables to be bound |
| 36 | * @param tests error tests |
| 37 | */ |
| 38 | public Catch(final InputInfo info, final Expr expr, final Var[] vars, |
| 39 | final ArrayList<Test> tests) { |
| 40 | super(info, expr, Types.ITEM_ZM); |
| 41 | this.tests = tests; |
| 42 | this.vars = vars; |
| 43 | this.expr = expr; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public Catch compile(final CompileContext cc) throws QueryException { |
| 48 | expr = cc.compileOrError(expr, false); |
| 49 | return optimize(cc); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Catch optimize(final CompileContext cc) { |
| 54 | return (Catch) adoptType(expr); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the value of the caught expression. |
| 59 | * @param qc query context |
| 60 | * @param ex caught exception |
| 61 | * @return resulting item |
| 62 | * @throws QueryException query exception |
| 63 | */ |
| 64 | Value value(final QueryContext qc, final QueryException ex) throws QueryException { |
| 65 | int v = 0; |
| 66 | for(final Value value : ex.values()) { |
| 67 | qc.set(vars[v++], value); |
| 68 | } |
| 69 | return expr.value(qc); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Expr copy(final CompileContext cc, final IntObjectMap<Var> vm) { |
| 74 | final Var[] vrs = QueryException.variables(cc.qc, info); |
| 75 | final int vl = vrs.length; |
| 76 | for(int v = 0; v < vl; v++) { |
| 77 | vm.put(vars[v].id, cc.vs().add(vrs[v])); |
| 78 | } |
| 79 | return copyType(new Catch(info, expr.copy(cc, vm), vrs, new ArrayList<>(tests))); |
| 80 | } |
| 81 | |
| 82 | @Override |
nothing calls this directly
no outgoing calls
no test coverage detected