|
From: <di...@us...> - 2011-01-24 17:11:56
|
Revision: 13625
http://exist.svn.sourceforge.net/exist/?rev=13625&view=rev
Author: dizzzz
Date: 2011-01-24 17:11:50 +0000 (Mon, 24 Jan 2011)
Log Message:
-----------
[ignore] an important step in try-catch
Modified Paths:
--------------
branches/adam/eXist-xq3/src/org/exist/xquery/TryCatchExpression.java
Added Paths:
-----------
branches/adam/eXist-xq3/test/src/org/exist/xquery/functions/xquery3/
branches/adam/eXist-xq3/test/src/org/exist/xquery/functions/xquery3/TryCatchTest.java
Modified: branches/adam/eXist-xq3/src/org/exist/xquery/TryCatchExpression.java
===================================================================
--- branches/adam/eXist-xq3/src/org/exist/xquery/TryCatchExpression.java 2011-01-24 16:35:05 UTC (rev 13624)
+++ branches/adam/eXist-xq3/src/org/exist/xquery/TryCatchExpression.java 2011-01-24 17:11:50 UTC (rev 13625)
@@ -26,6 +26,7 @@
import org.exist.dom.DocumentSet;
import org.exist.dom.QName;
+import org.exist.xquery.ErrorCodes.ErrorCode;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.QNameValue;
@@ -110,11 +111,13 @@
return tryTargetSeq;
} catch(XPathException xpe) {
- System.out.println("Exception=" + xpe.getMessage());
- System.out.println("Errorcode=" + xpe.getErrorCode().toString() + " qname=" + xpe.getErrorCode().getErrorQName());
+// System.out.println("Exception=" + xpe.getMessage());
+// System.out.println("Errorcode=" + xpe.getErrorCode().toString() + " qname=" + xpe.getErrorCode().getErrorQName());
// Get qname
- QName errorCodeQname = xpe.getErrorCode().getErrorQName();
+ ErrorCode errorCode = xpe.getErrorCode();
+ System.out.println("aaaaa"+xpe.toString());
+ QName errorCodeQname = errorCode.getErrorQName();
// Exception in thrown, catch expression will be evaluated.
// catchvars (CatchErrorCode (, CatchErrorDesc (, CatchErrorVal)?)? )
@@ -123,61 +126,71 @@
LocalVariable mark0 = context.markLocalVariables(false);
try {
+ boolean errorMatched = false;
+
// Iterate on all catch clauses
for (CatchClause catchClause : catchClauses) {
-//// DW: don't understand why this yields into a CCE
-// for(String txt : catchClause.getCatchErrorList()){
-// System.out.print( txt + "++++");
-// }
-// System.out.println();
+ if(isErrorInList(errorCodeQname, catchClause.getCatchErrorList()) && !errorMatched){
- // Get catch variables
- List<QName> catchVars = (List<QName>) catchClause.getCatchVars();
- LocalVariable mark1 = context.markLocalVariables(false);
- int varPos = 0;
+ errorMatched=true;
- try {
- // catch variables
- // "(" CatchErrorCode ("," CatchErrorDesc ("," CatchErrorVal)?)? ")"
- for (QName catchVar : catchVars) {
+ // Get catch variables
+ List<QName> catchVars = (List<QName>) catchClause.getCatchVars();
+ LocalVariable mark1 = context.markLocalVariables(false);
+ int varPos = 1;
- LocalVariable var = new LocalVariable(catchVar);
+ try {
+ // catch variables
+ // "(" CatchErrorCode ("," CatchErrorDesc ("," CatchErrorVal)?)? ")"
+ for (QName catchVar : catchVars) {
-// System.out.println(catchVar.getStringValue() + "_=_" + catchVar.getLocalName());
-
- // This should be in order of existance
- // xs:QName, xs:string?, and item()* respectively.
- switch (varPos) {
- case 1:
- var.setSequenceType(new SequenceType(Type.QNAME, Cardinality.EXACTLY_ONE));
-// QNameValue qnv = new QNameValue(context, catchVar);
-// qnv.add( new StringValue("aaa") );
-// var.setValue(qnv);
- context.declareGlobalVariable(var);
- break;
- case 2:
- var.setSequenceType(new SequenceType(Type.STRING, Cardinality.ZERO_OR_ONE));
- break;
- case 3:
- var.setSequenceType(new SequenceType(Type.ITEM, Cardinality.ZERO_OR_MORE));
- break;
+ catchVar.setPrefix(null);
+ catchVar.setNamespaceURI("");
+
+ LocalVariable var = new LocalVariable(catchVar);
+
+ // This should be in order of existance
+ // xs:QName, xs:string?, and item()* respectively.
+ switch (varPos) {
+ case 1:
+ var.setSequenceType(new SequenceType(Type.QNAME, Cardinality.EXACTLY_ONE));
+ QNameValue qnv = new QNameValue(context, catchVar);
+// qnv.add( new StringValue("aaa") );
+ var.setValue( new StringValue( xpe.getErrorCode().getErrorQName().getStringValue()) );
+ // context.declareGlobalVariable(var);
+ context.declareVariable(catchVar, var);
+
+ break;
+ case 2:
+ var.setSequenceType(new SequenceType(Type.STRING, Cardinality.ZERO_OR_ONE));
+ StringValue sv = new StringValue( xpe.getErrorCode().toString() );
+ var.setValue(sv);
+ break;
+ case 3:
+ var.setSequenceType(new SequenceType(Type.ITEM, Cardinality.ZERO_OR_MORE));
+ break;
+ }
+ context.declareVariableBinding(var);
+ varPos++;
}
- context.declareVariableBinding(var);
- varPos++;
+
+
+ // Evaluate catch expression
+ catchResultSeq = ((Expression) catchClause.getCatchExpr()).eval(contextSequence, contextItem);
+
+ } finally {
+ context.popLocalVariables(mark1);
}
- // Evaluate catch expression
- catchResultSeq = ((Expression) catchClause.getCatchExpr()).eval(contextSequence, contextItem);
-
- } finally {
- context.popLocalVariables(mark1);
+ } else {
+ // if in the end nothing is set, rethrow
}
}
-// } catch(Throwable ex){
-// ex.printStackTrace();
-// throw new XPathException(ex);
+ if(!errorMatched){
+ throw xpe;
+ }
} finally {
@@ -252,7 +265,22 @@
visitor.visitTryCatch(this);
}
+ private boolean isErrorInList(QName error, List<String> errors){
+ String qError = error.getStringValue();
+ for(String lError : errors){
+ if("*".equals(lError)){
+ return true;
+ }
+
+ if(qError.equals(lError)){
+ return true;
+ }
+ }
+ return false;
+ }
+
+
/**
* Data container
*/
Added: branches/adam/eXist-xq3/test/src/org/exist/xquery/functions/xquery3/TryCatchTest.java
===================================================================
--- branches/adam/eXist-xq3/test/src/org/exist/xquery/functions/xquery3/TryCatchTest.java (rev 0)
+++ branches/adam/eXist-xq3/test/src/org/exist/xquery/functions/xquery3/TryCatchTest.java 2011-01-24 17:11:50 UTC (rev 13625)
@@ -0,0 +1,166 @@
+package org.exist.xquery.functions.xquery3;
+
+import org.junit.Ignore;
+import org.xmldb.api.base.ResourceSet;
+
+import org.exist.test.EmbeddedExistTester;
+import org.exist.xquery.ErrorCodes;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author wessels
+ */
+public class TryCatchTest extends EmbeddedExistTester {
+
+ public TryCatchTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() {
+ System.out.println("***********************");
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ // TODO add test methods here.
+ // The methods must be annotated with annotation @Test. For example:
+ //
+ // @Test
+ // public void hello() {}
+ @Test
+ public void simpleCatch() {
+
+ String query = "try { a + 7 } catch * { 1 }";
+ try {
+ ResourceSet results = executeQuery(query);
+ String r = (String) results.getResource(0).getContent();
+
+ assertEquals("1", r);
+
+ } catch (Throwable ex) {
+ ex.printStackTrace();
+ fail(ex.getMessage());
+ }
+
+ }
+
+ @Test
+ public void catchWithCodeAndDescription() {
+
+ String query = "try { a + 7 } "
+ + "catch * ($errcode, $errdesc, $errval) "
+ + "{ $errcode, $errdesc } ";
+ try {
+ ResourceSet results = executeQuery(query);
+
+ assertEquals(2, results.getSize());
+
+ String r1 = (String) results.getResource(0).getContent();
+ assertEquals(ErrorCodes.XPDY0002.getErrorQName().getStringValue(), r1);
+
+ String r2 = (String) results.getResource(1).getContent();
+ assertEquals(ErrorCodes.XPDY0002.toString(), r2);
+
+ } catch (Throwable ex) {
+ ex.printStackTrace();
+ fail(ex.getMessage());
+ }
+
+ }
+
+ @Test
+ public void catchWithError3Matches() {
+
+ String query = "try { a + 7 } "
+ + "catch err:XPDY0001 { 1 }"
+ + "catch err:XPDY0002 { 2 }"
+ + "catch err:XPDY0003 { 3 }";
+ try {
+ ResourceSet results = executeQuery(query);
+ String r = (String) results.getResource(0).getContent();
+
+ assertEquals("2", r);
+
+ } catch (Throwable ex) {
+ ex.printStackTrace();
+ fail(ex.getMessage());
+ }
+ }
+
+ @Test
+ public void catchWithErrorNoMatches() {
+
+ String query = "try { a + 7 } "
+ + "catch err:XPDY0001 { 1 }"
+ + "catch err:XPDY0002 { a }"
+ + "catch err:XPDY0003 { 3 }";
+ try {
+ ResourceSet results = executeQuery(query);
+ String r = (String) results.getResource(0).getContent();
+
+ assertEquals("2", r);
+
+ fail("Exception expected");
+
+ } catch (Throwable ex) {
+ // expected
+
+ }
+ }
+
+ @Test
+ public void catchWithError5Matches() {
+
+ String query = "try { a + 7 } "
+ + "catch err:XPDY0001 | err:XPDY0003 { 13 }"
+ + "catch err:XPDY0002 { 2 }"
+ + "catch err:XPDY0004 | err:XPDY0005 { 45 }";
+ try {
+ ResourceSet results = executeQuery(query);
+ String r = (String) results.getResource(0).getContent();
+
+ assertEquals("2", r);
+
+ } catch (Throwable ex) {
+ ex.printStackTrace();
+ fail(ex.getMessage());
+ }
+ }
+
+ @Test
+ public void catchWithError51Matches() {
+
+ String query = "try { a + 7 } "
+ + "catch err:XPDY0001 | * { 13 }"
+ + "catch err:XPDY0002 { 2 }"
+ + "catch err:XPDY0004 | err:XPDY0005 { 45 }";
+ try {
+ ResourceSet results = executeQuery(query);
+ String r = (String) results.getResource(0).getContent();
+
+ assertEquals("13", r);
+
+ } catch (Throwable ex) {
+ ex.printStackTrace();
+ fail(ex.getMessage());
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|