|
From: <max...@us...> - 2011-06-07 14:50:19
|
Revision: 4390
http://mxquery.svn.sourceforge.net/mxquery/?rev=4390&view=rev
Author: maxspeicher
Date: 2011-06-07 14:50:09 +0000 (Tue, 07 Jun 2011)
Log Message:
-----------
Extended NativeFuncCall so that a method is also recognized if the signature contains interfaces/superclasses of the actual parameter objects.
Modified Paths:
--------------
trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java
trunk/MXQuery/android/src/ch/ethz/mxquery/functions/fn/Doc.java
trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java
Added Paths:
-----------
trunk/MXQuery/android/src/ch/ethz/mxquery/android/UiListener.java
Modified: trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java
===================================================================
--- trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java 2011-06-07 08:33:26 UTC (rev 4389)
+++ trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java 2011-06-07 14:50:09 UTC (rev 4390)
@@ -4,9 +4,18 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.io.StringWriter;
+import ch.ethz.mxquery.xqj.MXQueryXQDataSource;
+import ch.ethz.repackaged.xquery.XQConnection;
+import ch.ethz.repackaged.xquery.XQDataSource;
+import ch.ethz.repackaged.xquery.XQException;
+import ch.ethz.repackaged.xquery.XQExpression;
+import ch.ethz.repackaged.xquery.XQSequence;
+
import android.app.Activity;
import android.content.Context;
+import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.widget.Button;
@@ -14,8 +23,10 @@
public class MXQuery {
+ private static Activity act;
private static Context ctx;
- private static Activity act;
+ private static String xQueryFile;
+ private static XQDataSource xqjd = new MXQueryXQDataSource(MXQueryXQDataSource.XQJ_UPDATE_MODE);
public static void init(Activity act) {
MXQuery.ctx = act.getApplicationContext();
@@ -30,26 +41,18 @@
return act;
}
- public static String doc(String fileName) {
- String result = "";
-
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(
- ctx.openFileInput(fileName)));
- String line;
-
- while ((line = in.readLine()) != null) {
- result += line;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return result;
+ public static void startActivity(Class<?> next) {
+ act.startActivity(new Intent(act, next));
}
+ public static String getXQueryFile() {
+ return xQueryFile;
+ }
+
+ public static void setXQueryFile(int rawResourceId) {
+ xQueryFile = readXQueryFile(rawResourceId);
+ }
+
public static Button getButton(int id) {
return (Button) act.findViewById(id);
}
@@ -58,6 +61,40 @@
return (TextView) act.findViewById(id);
}
+ public static String doQuery(String query) {
+ StringWriter result = new StringWriter();
+
+ try {
+ XQConnection xqjc = xqjd.getConnection();
+ XQExpression xqje = xqjc.createExpression();
+ XQSequence xqjs = xqje.executeQuery(query);
+
+ xqjs.writeSequence(result, null);
+ xqjc.close();
+ } catch (XQException xqe) {
+ xqe.printStackTrace();
+ return xqe.getMessage();
+ }
+ return result.toString();
+ }
+
+ public static String readXQueryFile(int rawResourceId) {
+ BufferedReader in = new BufferedReader(new InputStreamReader(
+ act.getResources().openRawResource(rawResourceId)));
+ String query = "";
+ String line;
+
+ try {
+ while ((line = in.readLine()) != null) {
+ query += line;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return query;
+ }
+
public static Location getLocation() {
Location lastKnownLocation = null;
LocationManager locationManager = (LocationManager) act.getSystemService(Context.LOCATION_SERVICE);
Added: trunk/MXQuery/android/src/ch/ethz/mxquery/android/UiListener.java
===================================================================
--- trunk/MXQuery/android/src/ch/ethz/mxquery/android/UiListener.java (rev 0)
+++ trunk/MXQuery/android/src/ch/ethz/mxquery/android/UiListener.java 2011-06-07 14:50:09 UTC (rev 4390)
@@ -0,0 +1,26 @@
+package ch.ethz.mxquery.android;
+
+import android.view.View;
+import android.view.View.OnClickListener;
+import ch.ethz.mxquery.android.MXQuery;
+
+public class UiListener implements OnClickListener {
+
+ public String methodName;
+ public static View view;
+
+ public UiListener(String methodName) {
+ this.methodName = methodName;
+ }
+
+ public void onClick(View v) {
+ view = v;
+
+ MXQuery.doQuery(
+ MXQuery.getXQueryFile() +
+ methodName +
+ "(uil:view())"
+ );
+ }
+
+}
Modified: trunk/MXQuery/android/src/ch/ethz/mxquery/functions/fn/Doc.java
===================================================================
--- trunk/MXQuery/android/src/ch/ethz/mxquery/functions/fn/Doc.java 2011-06-07 08:33:26 UTC (rev 4389)
+++ trunk/MXQuery/android/src/ch/ethz/mxquery/functions/fn/Doc.java 2011-06-07 14:50:09 UTC (rev 4390)
@@ -121,13 +121,19 @@
else
throw de;
}
- //if (operation!=OPERATION_DOC)
+
+ /*
+ * Next five commented-out lines: quick workaround to get
+ * fn:doc(...) working on Android.
+ */
+
+// if (operation!=OPERATION_DOC)
rd = IOLib.getInput(uri, false, encoding, loc);
XDMIterator cur = null;
try {
switch(operation) {
case OPERATION_DOC:
- cur = XDMInputFactory.createXMLInput(context, rd, true, Context.NO_VALIDATION, loc);
+ cur = XDMInputFactory.createXMLInput(context, rd, true, context.getInputValidationMode(), loc);
// if (isInValidateExpression())
// cur = XDMInputFactory.createXMLInput(context, rd, true, Context.SCHEMA_VALIDATION_STRICT, loc);
// else
Modified: trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java
===================================================================
--- trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java 2011-06-07 08:33:26 UTC (rev 4389)
+++ trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java 2011-06-07 14:50:09 UTC (rev 4390)
@@ -19,6 +19,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.ArrayList;
import java.util.Vector;
import ch.ethz.mxquery.contextConfig.Context;
@@ -132,17 +133,56 @@
Method meth = null;
Field field = null;
Object res;
+
if (methodName.equals("new")) {
Constructor con = native_function.getConstructor(invocationParamsTypes);
res = con.newInstance(invocationParams);
- }
- else {
+ } else {
+ ArrayList candidates = new ArrayList();
+ Method[] methods = native_function.getMethods();
+
+ /*
+ * This retrieves a correct method even if the parameter
+ * types of the method signature are interfaces or abstract
+ * classes, which are not the actual types of the objects
+ * passed.
+ */
+ for (int i=0; i<methods.length; ++i) {
+ if (methodName.equals(methods[i].getName())) {
+ Class[] params = methods[i].getParameterTypes();
+ boolean add = true;
+
+ if (params.length == invocationParams.length) {
+ for (int j=0; j<params.length; ++j) {
+ if (!params[j].isInstance(invocationParams[j])) {
+ add = false;
+ }
+ }
+
+ if (add) {
+ candidates.add(methods[i]);
+ }
+ }
+ }
+ }
+
try {
meth = native_function.getMethod(methodName, invocationParamsTypes);
res = meth.invoke(instanceToCall, invocationParams);
} catch (NoSuchMethodException e) {
- field = native_function.getField(methodName);
- res = field.get(instanceToCall);
+ /*
+ * The method name provided by XQuery is either the
+ * name of a field, or the method could not be
+ * retrieved b/c the parameter types of the signature
+ * are interfaces or abstract classes.
+ */
+ if (candidates.size() == 1) {
+ meth = (Method) candidates.get(0);
+ res = meth.invoke(instanceToCall, invocationParams);
+ } else {
+ field = native_function.getField(methodName);
+ res = field.get(instanceToCall);
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|