|
From: <pm_...@us...> - 2011-05-24 13:34:15
|
Revision: 4367
http://mxquery.svn.sourceforge.net/mxquery/?rev=4367&view=rev
Author: pm_fischer
Date: 2011-05-24 13:34:09 +0000 (Tue, 24 May 2011)
Log Message:
-----------
- fn:matches should work on Java 1.4 again
- all errors are thrown (and not just logged) for NativeFunctionCalls
- use string, not URI for file I/O
Modified Paths:
--------------
trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java
trunk/MXQuery/src/ch/ethz/mxquery/util/IOLib.java
Added Paths:
-----------
trunk/MXQuery/altsrc/ch/ethz/mxquery/functions/fn/
trunk/MXQuery/altsrc/ch/ethz/mxquery/functions/fn/Matches.java
Added: trunk/MXQuery/altsrc/ch/ethz/mxquery/functions/fn/Matches.java
===================================================================
--- trunk/MXQuery/altsrc/ch/ethz/mxquery/functions/fn/Matches.java (rev 0)
+++ trunk/MXQuery/altsrc/ch/ethz/mxquery/functions/fn/Matches.java 2011-05-24 13:34:09 UTC (rev 4367)
@@ -0,0 +1,148 @@
+/* Copyright 2006 - 2009 ETH Zurich
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ch.ethz.mxquery.functions.fn;
+
+import java.util.Vector;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.thaiopensource.datatype.xsd.regex.jdk1_4.Translator;
+
+import ch.ethz.mxquery.contextConfig.Context;
+import ch.ethz.mxquery.datamodel.types.Type;
+import ch.ethz.mxquery.datamodel.types.TypeInfo;
+import ch.ethz.mxquery.datamodel.xdm.BooleanToken;
+import ch.ethz.mxquery.datamodel.xdm.TokenInterface;
+import ch.ethz.mxquery.exceptions.DynamicException;
+import ch.ethz.mxquery.exceptions.ErrorCodes;
+import ch.ethz.mxquery.exceptions.MXQueryException;
+import ch.ethz.mxquery.exceptions.QueryLocation;
+import ch.ethz.mxquery.exceptions.StaticException;
+import ch.ethz.mxquery.model.TokenBasedIterator;
+import ch.ethz.mxquery.model.XDMIterator;
+
+public class Matches extends TokenBasedIterator {
+
+ protected void init() throws MXQueryException {
+ String text = getStringValueOrEmpty(subIters[0]);
+ if (text == null)
+ text = "";
+
+ int f = computeFlags(subIters, 3, loc);
+
+ String pat = getStringValue(subIters[1]);
+
+ if (pat.equals("")) {
+ throw new DynamicException(ErrorCodes.E0004_TYPE_INAPPROPRIATE_TYPE, "Pattern must be a String value!", loc);
+ }
+
+
+ if ((f & Pattern.COMMENTS) != 0) {
+ pat = removeWsFromPattern(pat);
+ f = f ^ Pattern.COMMENTS;
+ }
+
+ Pattern p = null;
+ try {
+ String toMatch = pat;
+// if ((f & Pattern.LITERAL) == 0)
+ toMatch = Translator.translate(pat);
+ p = Pattern.compile(toMatch, f);
+ } catch (Exception e) {
+ throw new DynamicException(ErrorCodes.F0032_INVALID_REGULAR_EXPRESSION, "Invalid regular expression!", loc);
+ }
+
+ if ((f & Pattern.CASE_INSENSITIVE) != 0)
+ text = text.toLowerCase();
+ Matcher m = p.matcher(text);
+ if (m.find())
+ currentToken = BooleanToken.TRUE_TOKEN;
+ else
+ currentToken = BooleanToken.FALSE_TOKEN;
+ }
+
+ private static String removeWsFromPattern(String pattern) {
+ StringBuffer res = new StringBuffer(pattern.length());
+ boolean inCharClass = false;
+ // Simplified version of WS stripping, does not take [] into account
+ for (int i=0;i<pattern.length();i++) {
+ char chr = pattern.charAt(i);
+ if (!Character.isWhitespace(chr) || inCharClass)
+ res.append(chr);
+ if (chr == '[')
+ inCharClass = true;
+ if (chr == ']')
+ inCharClass = false;
+ }
+ return res.toString();
+ }
+
+ /**
+ * Extract and compute the flags for the regexp-based functions
+ * @param subIters
+ * @param pos: Position of the flags parameter, counting as in XQuery (1...)
+ * @param loc: QueryLocation for error reporting
+ * @return
+ * @throws MXQueryException
+ */
+
+ static int computeFlags(XDMIterator [] subIters, int pos, QueryLocation loc) throws MXQueryException {
+ int f = 0;
+ if (subIters.length == pos) {
+
+ TokenInterface flags = subIters[pos-1].next();
+ String theFlags = flags.getText();
+
+ if (theFlags == null) {
+ throw new DynamicException(ErrorCodes.E0004_TYPE_INAPPROPRIATE_TYPE, "Regex Flags must be a String value!", loc);
+ }
+
+ if (!theFlags.equals("")) { // if flags are empty, skip
+ if (!theFlags.matches("[smixq]+")) {
+ throw new DynamicException(ErrorCodes.F0031_INVALID_REGULAR_EXPRESSION_FLAGS, "Invalid regular expression flags!", loc);
+ }
+
+ if (theFlags.indexOf("s") >= 0) {
+ f += Pattern.DOTALL;
+ }
+ if (theFlags.indexOf('m') >= 0) {
+ f += Pattern.MULTILINE;
+ }
+ if (theFlags.indexOf("i") >= 0) {
+ f += Pattern.CASE_INSENSITIVE;
+ }
+ if (theFlags.indexOf("x") >= 0) {
+ f += Pattern.COMMENTS;
+ }
+ if (theFlags.indexOf("q") >= 0) {
+ throw new StaticException(ErrorCodes.A0002_EC_NOT_SUPPORTED, "'q' flag/literal matches not supported on Java 1.4", loc);
+ }
+
+ }
+ }
+ return f;
+ }
+
+ public TypeInfo getStaticType() {
+ return new TypeInfo(Type.BOOLEAN,Type.OCCURRENCE_IND_EXACTLY_ONE);
+ }
+
+ protected XDMIterator copy(Context context, XDMIterator[] subIters, Vector nestedPredCtxStack) throws MXQueryException {
+ XDMIterator copy = new Matches();
+ copy.setContext(context, true);
+ copy.setSubIters(subIters);
+ return copy;
+ }
+}
Modified: trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java
===================================================================
--- trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java 2011-05-24 13:32:19 UTC (rev 4366)
+++ trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java 2011-05-24 13:34:09 UTC (rev 4367)
@@ -149,19 +149,19 @@
this.current = new TokenIterator(context, wrap, loc, false);
}
} catch (SecurityException e) {
- e.printStackTrace();
+ throw new DynamicException(ErrorCodes.A0009_EC_EVALUATION_NOT_POSSIBLE, "Native call failed: "+e.getMessage(), loc);
} catch (NoSuchFieldException e) {
- e.printStackTrace();
+ throw new DynamicException(ErrorCodes.A0009_EC_EVALUATION_NOT_POSSIBLE, "Native call failed: "+e.getMessage(), loc);
} catch (NoSuchMethodException e) {
- e.printStackTrace();
+ throw new DynamicException(ErrorCodes.A0009_EC_EVALUATION_NOT_POSSIBLE, "Native call failed: "+e.getMessage(), loc);
} catch (IllegalArgumentException e) {
- e.printStackTrace();
+ throw new DynamicException(ErrorCodes.A0009_EC_EVALUATION_NOT_POSSIBLE, "Native call failed: "+e.getMessage(), loc);
} catch (IllegalAccessException e) {
- e.printStackTrace();
+ throw new DynamicException(ErrorCodes.A0009_EC_EVALUATION_NOT_POSSIBLE, "Native call failed: "+e.getMessage(), loc);
} catch (InvocationTargetException e) {
- e.printStackTrace();
+ throw new DynamicException(ErrorCodes.A0009_EC_EVALUATION_NOT_POSSIBLE, "Native call failed: "+e.getMessage(), loc);
} catch (InstantiationException e) {
- e.printStackTrace();
+ throw new DynamicException(ErrorCodes.A0009_EC_EVALUATION_NOT_POSSIBLE, "Native call failed: "+e.getMessage(), loc);
}
//this.current = new TokenIterator(context, 42,Type.INT, loc);
Modified: trunk/MXQuery/src/ch/ethz/mxquery/util/IOLib.java
===================================================================
--- trunk/MXQuery/src/ch/ethz/mxquery/util/IOLib.java 2011-05-24 13:32:19 UTC (rev 4366)
+++ trunk/MXQuery/src/ch/ethz/mxquery/util/IOLib.java 2011-05-24 13:34:09 UTC (rev 4367)
@@ -201,7 +201,7 @@
throw new DynamicException(ErrorCodes.F0014_ERROR_RETRIEVING_RESOURCE, "I/O Error - Remote Data cannot be accessed: " + e, loc);
}
} else {
- File xml = new File(uri);
+ File xml = new File(toOpen);
if (xml.exists()) {
try {
ins = new FileInputStream(xml);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|