|
From: <max...@us...> - 2011-05-13 18:16:54
|
Revision: 4335
http://mxquery.svn.sourceforge.net/mxquery/?rev=4335&view=rev
Author: maxspeicher
Date: 2011-05-13 18:16:48 +0000 (Fri, 13 May 2011)
Log Message:
-----------
- added java.lang.CharSequence to Java/XQuery type mapping
- added helper class (MXQuery.java) for passing the Android application context to the library
- changed IOLib.java to use Android-specific IO (not working yet)
Modified Paths:
--------------
trunk/MXQuery/src/ch/ethz/mxquery/functions/NativeFunctionImporter.java
trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java
Added Paths:
-----------
trunk/MXQuery/android/src/ch/ethz/mxquery/android/
trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java
trunk/MXQuery/android/src/ch/ethz/mxquery/util/
trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java
Added: trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java
===================================================================
--- trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java (rev 0)
+++ trunk/MXQuery/android/src/ch/ethz/mxquery/android/MXQuery.java 2011-05-13 18:16:48 UTC (rev 4335)
@@ -0,0 +1,59 @@
+package ch.ethz.mxquery.android;
+
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import android.app.Activity;
+import android.content.Context;
+import android.widget.Button;
+import android.widget.TextView;
+
+public class MXQuery {
+
+ private static Context ctx;
+ private static Activity act;
+
+ public static void init(Activity act) {
+ MXQuery.ctx = act.getApplicationContext();
+ MXQuery.act = act;
+ }
+
+ public static Context getContext() {
+ return ctx;
+ }
+
+ public static Activity getActivity() {
+ 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 Button getButton(int id) {
+ return (Button) act.findViewById(id);
+ }
+
+ public static TextView getTextView(int id) {
+ return (TextView) act.findViewById(id);
+ }
+
+}
Added: trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java
===================================================================
--- trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java (rev 0)
+++ trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java 2011-05-13 18:16:48 UTC (rev 4335)
@@ -0,0 +1,263 @@
+/* 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.util;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import android.content.Context;
+import ch.ethz.mxquery.android.MXQuery;
+import ch.ethz.mxquery.exceptions.DynamicException;
+import ch.ethz.mxquery.exceptions.ErrorCodes;
+import ch.ethz.mxquery.exceptions.MXQueryException;
+import ch.ethz.mxquery.exceptions.QueryLocation;
+
+public class IOLib {
+
+ private static Context ctx = MXQuery.getContext();
+
+ public static void copyFile(String source, String destination) throws MXQueryException{
+ FileInputStream in = null;
+ FileOutputStream out = null;
+ try {
+ File src = new File(new URI(source));
+ File dst = new File(new URI(destination));
+ if (src.exists()) {
+ // create a copy of the file, possibly overwriting previous copies
+ in = new FileInputStream(src);
+ out = new FileOutputStream(dst, false);
+ byte[] buf = new byte[4096];
+ int len;
+ while ((len = in.read(buf)) > 0){
+ out.write(buf, 0, len);
+ }
+ }
+
+ } catch (IOException io) {
+ throw new DynamicException(ErrorCodes.A0007_EC_IO,"I/O Error copying file "+io.toString(),QueryLocation.OUTSIDE_QUERY_LOC);
+ } catch (URISyntaxException e) {
+ throw new DynamicException(ErrorCodes.A0007_EC_IO,"I/O Error copying file "+e.toString(),QueryLocation.OUTSIDE_QUERY_LOC);
+ }
+ finally {
+ if (in != null)
+ try {
+ in.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ if (out != null)
+ try {
+ out.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Check the file encoding using a 8bit representation
+ * @param ins Input stream to check
+ * @param xquery snoop an xquery file, not an XML file
+ * @return Encoding String if detected, otherwise null
+ * @throws UnsupportedEncodingException
+ * @throws IOException
+ */
+
+ public static final String snoopEncoding8bit(InputStream ins, boolean xquery) throws UnsupportedEncodingException, IOException {
+ String encoding = null;
+ String typeToSearch = "?xml";
+ if (xquery)
+ typeToSearch = "xquery";
+
+ // Check all the encoding that are 8bit-oriented for the normal characters
+ InputStreamReader rs = new InputStreamReader(ins, "us-ascii");
+ char [] cbuf = new char[1025];
+ int bytes = rs.read(cbuf, 0, 1024);
+ if (bytes > 0 ) {
+ String startLine = new String(cbuf);
+ if (startLine.indexOf(typeToSearch) >= 0) {
+ int encodingPos;
+ if (!xquery) {
+ encodingPos = startLine.indexOf("encoding=\"");
+ if (encodingPos > 0) {
+ String tempEncoding = startLine.substring(encodingPos + 10);
+ int endEncoding = tempEncoding.indexOf('"');
+ encoding = tempEncoding.substring(0, endEncoding);
+ }
+ }
+ else {
+ int xqPos = startLine.indexOf("xquery");
+ if (xqPos >= 0 ) {
+ encodingPos = startLine.indexOf("encoding", xqPos+1);
+ if (encodingPos > 0) {
+ int sep = startLine.indexOf(";", encodingPos + 8);
+ if (sep > 0) {
+ String tempEncoding = startLine.substring(encodingPos + 8, sep);
+ char delim = '"';
+ int startDelim = tempEncoding.indexOf(delim);
+ int endDelim = tempEncoding.indexOf(delim,startDelim+1);
+ if (endDelim >=0 && endDelim > startDelim+1)
+ encoding = tempEncoding.substring(startDelim+1, endDelim);
+ }
+ }
+ }
+ }
+ }
+ }
+ return encoding;
+ }
+
+ /**
+ * Check the file encoding using a 16bit representation
+ * @param ins Input stream to check
+ * @param xquery TODO
+ * @return Encoding String if detected, otherwise null
+ * @throws UnsupportedEncodingException
+ * @throws IOException
+ */
+
+ public static final String snoopEncoding16bit(InputStream ins, boolean xquery) throws UnsupportedEncodingException, IOException {
+
+ String encoding = null;
+ String typeToSearch = "?xml";
+ if (xquery)
+ typeToSearch = "xquery";
+
+ InputStreamReader rs = new InputStreamReader(ins, "utf-16");
+ char [] cbuf = new char[1025];
+ int bytes = rs.read(cbuf, 0, 1024);
+ if (bytes > 0 ) {
+ String startLine = new String(cbuf);
+ if (startLine.indexOf(typeToSearch) >= 0) {
+ encoding = "utf-16";
+ }
+ }
+ return encoding;
+ }
+
+ public static Reader getInput (String toOpen, boolean xquery, QueryLocation loc) throws MXQueryException {
+ String encoding = "utf-8";
+ InputStream ins = null;
+ URI uri;
+ try {
+ uri = new URI(toOpen);
+ } catch (URISyntaxException e) {
+ throw new DynamicException(ErrorCodes.F0017_INVALID_ARGUMENT_TO_FN_DOC, "Invalid URI given to fn:doc", loc);
+ }
+ if (uri.isAbsolute()) {
+ try {
+ URL url = uri.toURL();
+ URLConnection conn = url.openConnection();
+ conn.setRequestProperty("User-Agent", "MXQuery");
+ ins = conn.getInputStream();
+ // try to use content-type specific
+ String contentType = conn.getContentType();
+ int encPos = contentType.indexOf("charset=");
+ if (encPos > 0)
+ encoding = contentType.substring(encPos+8); //lenght of "charset="
+ //encoding = "iso-8859-1";
+ BufferedInputStream bufs = new BufferedInputStream(ins);
+ bufs.mark(16384);
+ String tempEncoding = IOLib.snoopEncoding8bit(bufs, xquery);
+ if (tempEncoding != null) {
+ encoding = tempEncoding;
+ } else {
+ bufs.reset();
+ bufs.mark(16384);
+ tempEncoding = IOLib.snoopEncoding16bit(bufs, xquery);
+ if (tempEncoding != null) {
+ encoding = tempEncoding;
+ }
+ }
+ bufs.reset();
+ return new BufferedReader(new UnicodeReader(bufs, encoding));
+ } catch (IOException e) {
+ throw new DynamicException(ErrorCodes.F0014_ERROR_RETRIEVING_RESOURCE, "I/O Error - Remote Data cannot be accessed: " + e, loc);
+ }
+ } else {
+ try {
+ ins = ctx.openFileInput(toOpen);
+
+ // snoop encoding
+ encoding = "utf-8";
+ String tempEncoding = IOLib.snoopEncoding8bit(ins, xquery);
+ if (tempEncoding != null) {
+ encoding = tempEncoding;
+ } else {
+ ins.close();
+ ins = ctx.openFileInput(toOpen);
+ tempEncoding = IOLib.snoopEncoding16bit(ins, xquery);
+ if (tempEncoding != null) {
+ encoding = tempEncoding;
+ }
+ }
+ ins.close();
+ ins = ctx.openFileInput(toOpen);
+ return new BufferedReader(new UnicodeReader(ins, encoding));
+ } catch (FileNotFoundException e1) {
+ throw new DynamicException(ErrorCodes.F0014_ERROR_RETRIEVING_RESOURCE, "File '" + uri + "' does not exist!", loc);
+ } catch (UnsupportedEncodingException e) {
+ try {
+ ins.close();
+ } catch (IOException ie) {
+ //
+ }
+ throw new DynamicException(ErrorCodes.A0007_EC_IO, "Unsupported encoding '" + encoding + "' in File '" + uri, loc);
+ } catch (Exception e) {
+ try {
+ if (ins != null)
+ ins.close();
+ } catch (IOException ie) {
+ //
+ }
+ throw new DynamicException(ErrorCodes.F0014_ERROR_RETRIEVING_RESOURCE, "File '" + uri + "' could not be openend " + e.toString(), loc);
+ }
+ }
+ }
+
+ public static PrintStream getOutput(String url, boolean append, String encoding) throws IOException, MXQueryException {
+ URI uri = null;
+ try {
+ uri = new URI(url);
+ } catch (URISyntaxException e) {
+ throw new DynamicException(ErrorCodes.A0007_EC_IO,"Error Creating output file - invalid file name/URI: "+url,QueryLocation.OUTSIDE_QUERY_LOC);
+ }
+ OutputStream outw = new FileOutputStream(new File(uri), append);
+ return new PrintStream(outw,false,encoding);
+ }
+
+ public static String getSystemBaseUri() {
+ String curDir = System.getProperty("user.dir");
+ File fl = new File(curDir);
+ return fl.toURI().toString();
+ }
+}
Modified: trunk/MXQuery/src/ch/ethz/mxquery/functions/NativeFunctionImporter.java
===================================================================
--- trunk/MXQuery/src/ch/ethz/mxquery/functions/NativeFunctionImporter.java 2011-05-12 16:21:56 UTC (rev 4334)
+++ trunk/MXQuery/src/ch/ethz/mxquery/functions/NativeFunctionImporter.java 2011-05-13 18:16:48 UTC (rev 4335)
@@ -1,6 +1,7 @@
package ch.ethz.mxquery.functions;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
@@ -73,6 +74,7 @@
public static Context getNativeMethods(String className) throws MXQueryException{
Context ctx = new Context();
+
try {
Class toImport = Class.forName(className);
@@ -151,6 +153,29 @@
}
}
+
+ Field [] fields = toImport.getFields();
+
+ for (int i=0;i<fields.length;i++) {
+ Field cur = fields[i];
+ int mod = cur.getModifiers();
+ if (Modifier.isPublic(mod)) {
+ String name = cur.getName();
+ TypeInfo[] params;
+ if (Modifier.isStatic(mod)) {
+ params = new TypeInfo[0];
+ }
+ else {
+ params = new TypeInfo[1];
+ params[0] = new TypeInfo(Type.ITEM,Type.OCCURRENCE_IND_EXACTLY_ONE);
+ }
+ TypeInfo resultType = getXQueryType(cur.getType());
+ MethodData md = new MethodData(toImport, name, params, resultType);
+ if (!functions.containsKey(md))
+ functions.put(md,md);
+ }
+ }
+
java.util.Iterator allFuncts = functions.values().iterator();
while (allFuncts.hasNext()) {
MethodData md = (MethodData)allFuncts.next();
@@ -181,7 +206,9 @@
return new TypeInfo(Type.UNTYPED_ATOMIC,Type.OCCURRENCE_IND_EXACTLY_ONE);
}
if (class1.getName().equals("java.lang.String"))
- return new TypeInfo(Type.STRING,Type.OCCURRENCE_IND_EXACTLY_ONE);
+ return new TypeInfo(Type.STRING,Type.OCCURRENCE_IND_EXACTLY_ONE);
+ if (class1.getName().equals("java.lang.CharSequence"))
+ return new TypeInfo(Type.STRING,Type.OCCURRENCE_IND_EXACTLY_ONE);
if (class1.getName().equals("java.lang.Double"))
return new TypeInfo(Type.DOUBLE,Type.OCCURRENCE_IND_EXACTLY_ONE);
if (class1.getName().equals("java.lang.Float"))
Modified: trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java
===================================================================
--- trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java 2011-05-12 16:21:56 UTC (rev 4334)
+++ trunk/MXQuery/src/ch/ethz/mxquery/iterators/NativeFuncCall.java 2011-05-13 18:16:48 UTC (rev 4335)
@@ -15,6 +15,7 @@
package ch.ethz.mxquery.iterators;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Vector;
@@ -54,7 +55,6 @@
this.native_function = function;
this.resultSeqTypeIt = returnSeqTypeIt;
//this.paramNames = paramNames;
- this.native_function = function;
this.methodName = methodName;
this.returnType = returnType;
this.paramTypes = paramTypes;
@@ -114,14 +114,20 @@
try {
Method meth = null;
+ Field field = null;
Object res;
if (methodName.equals("new")) {
Constructor con = native_function.getConstructor(invocationParamsTypes);
res = con.newInstance(invocationParams);
}
else {
- meth = native_function.getMethod(methodName, invocationParamsTypes);
- res = meth.invoke(instanceToCall, invocationParams);
+ try {
+ meth = native_function.getMethod(methodName, invocationParamsTypes);
+ res = meth.invoke(instanceToCall, invocationParams);
+ } catch (NoSuchMethodException e) {
+ field = native_function.getField(methodName);
+ res = field.get(instanceToCall);
+ }
}
try {
@@ -135,22 +141,18 @@
this.current = new TokenIterator(context, wrap, loc, false);
}
} catch (SecurityException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
+ } catch (NoSuchFieldException e) {
+ e.printStackTrace();
} catch (NoSuchMethodException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|