From: <max...@us...> - 2011-05-22 22:31:33
|
Revision: 4363 http://mxquery.svn.sourceforge.net/mxquery/?rev=4363&view=rev Author: maxspeicher Date: 2011-05-22 22:31:26 +0000 (Sun, 22 May 2011) Log Message: ----------- added methods and sample queries for using XQuery Updating Facility Modified Paths: -------------- trunk/MXQuery_Android_App/AndroidManifest.xml trunk/MXQuery_Android_App/res/values/strings.xml trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/MXQueryService.java trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/Start.java Modified: trunk/MXQuery_Android_App/AndroidManifest.xml =================================================================== --- trunk/MXQuery_Android_App/AndroidManifest.xml 2011-05-22 22:29:55 UTC (rev 4362) +++ trunk/MXQuery_Android_App/AndroidManifest.xml 2011-05-22 22:31:26 UTC (rev 4363) @@ -4,6 +4,7 @@ android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"></uses-sdk> <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> + <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:icon="@drawable/xquery_icon" android:label="@string/app_name" android:permission="android.permission.READ_CONTACTS" android:debuggable="true"> <activity android:name="Start"> Modified: trunk/MXQuery_Android_App/res/values/strings.xml =================================================================== --- trunk/MXQuery_Android_App/res/values/strings.xml 2011-05-22 22:29:55 UTC (rev 4362) +++ trunk/MXQuery_Android_App/res/values/strings.xml 2011-05-22 22:31:26 UTC (rev 4363) @@ -23,7 +23,9 @@ <item>@string/native_java</item> <item>@string/manipulate_ui</item> <item>@string/fn_doc</item> - <item>@string/android_doc</item> + <item>@string/fn_doc2</item> + <item>@string/updating</item> + <item>@string/updating2</item> </string-array> <string name="hello_world">Hello World</string> @@ -34,8 +36,10 @@ <string name="fibonacci">Fibonacci</string> <string name="native_java">Native Java</string> <string name="manipulate_ui">Manipulate UI</string> - <string name="fn_doc">fn:doc()</string> - <string name="android_doc">android:doc()</string> + <string name="fn_doc">fn:doc() - local</string> + <string name="fn_doc2">fn:doc() - Internet</string> + <string name="updating">Updating 1</string> + <string name="updating2">Updating 2</string> <string name="hello_world_xquery">\"Hello World\"</string> <string name="addition_xquery">5+5</string> @@ -76,9 +80,16 @@ <string name="fn_doc_xquery"> fn:doc(\"test.xml\") </string> - <string name="android_doc_xquery"> - declare namespace android = \"java:ch.ethz.mxquery.android.MXQuery\";\n - fn:parse(android:doc(\"test.xml\")) + <string name="fn_doc2_xquery"> + fn:doc(\"http://mxquery.org/\") </string> + <string name="updating_xquery"> + copy $x := <test></test>\n + modify( insert node <bla/> into $x )\n + return $x + </string> + <string name="updating2_xquery"> + insert node <blub/> as last into fn:doc(\"test.xml\")/test + </string> </resources> Modified: trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/MXQueryService.java =================================================================== --- trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/MXQueryService.java 2011-05-22 22:29:55 UTC (rev 4362) +++ trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/MXQueryService.java 2011-05-22 22:31:26 UTC (rev 4363) @@ -1,11 +1,22 @@ package ch.ethz.mxquery.android; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; +import ch.ethz.mxquery.contextConfig.CompilerOptions; +import ch.ethz.mxquery.contextConfig.Context; +import ch.ethz.mxquery.exceptions.MXQueryException; +import ch.ethz.mxquery.model.XDMIterator; +import ch.ethz.mxquery.query.PreparedStatement; +import ch.ethz.mxquery.query.XQCompiler; +import ch.ethz.mxquery.query.impl.CompilerImpl; +import ch.ethz.mxquery.xdmio.XDMSerializer; import ch.ethz.mxquery.xqj.MXQueryXQDataSource; import ch.ethz.repackaged.xquery.XQConnection; import ch.ethz.repackaged.xquery.XQDataSource; @@ -37,8 +48,8 @@ } /** public method for clients */ - public void runQuery(String query, XQueryListener listener) { - QueryThread queryThread = new QueryThread(query, listener); + public void runQuery(String query, XQueryListener listener, boolean updating) { + QueryThread queryThread = new QueryThread(query, listener, updating); queryThread.start(); } @@ -48,13 +59,17 @@ private class QueryThread extends Thread { private String query; private XQueryListener listener; - public QueryThread(String query, XQueryListener listener) { + private boolean updating; + + public QueryThread(String query, XQueryListener listener, boolean updating) { this.query = query; this.listener = listener; + this.updating = updating; } public void run() { - String result = doQuery(this.query); + String result = updating ? doUpdatingQuery(this.query) + : doQuery(this.query); lastResult = result; System.out.println("QueryThread - saving result: " + result); listener.queryResult(result); @@ -77,5 +92,43 @@ return result.toString(); } + + protected String doUpdatingQuery(String query) { + Context ctx = new Context(); + CompilerOptions co = new CompilerOptions(); + XQCompiler comp = new CompilerImpl(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + String s = new String(); + + ctx.getStores().setSerializeStores(true); + co.setUpdate(true); + + try { + PreparedStatement statement = comp.compile(ctx, query, co, null, null); + XDMIterator result = statement.evaluate(); + XDMSerializer ser = new XDMSerializer(); + + ser.eventsToXML(out, result); + statement.applyPUL(); + statement.serializeStores(false); + result.close(false); + statement.close(); + } catch (MXQueryException err) { + MXQueryException.printErrorPosition(query, err.getLocation()); + err.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + ctx.getStores().freeRessources(); + + try { + s = out.toString("utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + return s; + } } Modified: trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/Start.java =================================================================== --- trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/Start.java 2011-05-22 22:29:55 UTC (rev 4362) +++ trunk/MXQuery_Android_App/src/ch/ethz/mxquery/android/Start.java 2011-05-22 22:31:26 UTC (rev 4363) @@ -59,6 +59,7 @@ // private XQueryAsyncTask mTask; protected MXQueryService mService; protected boolean mIsBound = false; + protected boolean mUpdating = false; protected ProgressDialog mDialog; protected XQDataSource xqjd = new MXQueryXQDataSource(); @@ -164,6 +165,8 @@ int pos, long id) { String item = parent.getItemAtPosition(pos).toString(); + mUpdating = false; + if (item.equals(getString(R.string.hello_world))) { mInputView.setText(getString(R.string.hello_world_xquery)); } else if (item.equals(getString(R.string.addition))) { @@ -182,8 +185,14 @@ mInputView.setText(getString(R.string.manipulate_ui_xquery)); } else if (item.equals(getString(R.string.fn_doc))) { mInputView.setText(getString(R.string.fn_doc_xquery)); - } else if (item.equals(getString(R.string.android_doc))) { - mInputView.setText(getString(R.string.android_doc_xquery)); + } else if (item.equals(getString(R.string.fn_doc2))) { + mInputView.setText(getString(R.string.fn_doc2_xquery)); + } else if (item.equals(getString(R.string.updating))) { + mInputView.setText(getString(R.string.updating_xquery)); + mUpdating = true; + } else if (item.equals(getString(R.string.updating2))) { + mInputView.setText(getString(R.string.updating2_xquery)); + mUpdating = true; } } @@ -279,7 +288,7 @@ doBindService(); } - mService.runQuery(query, queryListener); + mService.runQuery(query, queryListener, mUpdating); } void doBindService() { @@ -397,7 +406,7 @@ @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); - //inflater.inflate(R.menu.menu, menu); + inflater.inflate(R.menu.menu, menu); return true; } @@ -405,9 +414,9 @@ public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { -// case R.id.generate_contacts_XML: -// new GenerateContactsXMLAsyncTask().execute(); -// return true; + case R.id.generate_contacts_XML: + new GenerateContactsXMLAsyncTask().execute(); + return true; default: return super.onOptionsItemSelected(item); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |