package com.dbxml.db.common.scripting;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: PythonExtension.java,v 1.3 2006/02/02 18:53:52 bradford Exp $
*/
import java.util.Map;
import com.dbxml.labrador.types.Variant;
import org.python.util.PythonInterpreter;
/**
* PythonExtension is an experimental Extension for executing Python with
* the Jython interpreter.
*/
public final class PythonExtension extends ScriptExtension {
private static final String[] EmptyStrings = new String[0];
public Variant execute(Map args) {
PythonInterpreter pi = new PythonInterpreter();
// Standard Imports
pi.exec("from com.dbxml.db.core import Database");
pi.exec("from com.dbxml.db.core import Collection");
pi.exec("from com.dbxml.db.core import Container");
pi.exec("from com.dbxml.db.common.scripting import *");
pi.exec("from org.w3c.dom import *");
pi.exec("from com.dbxml.xml import *");
// Standard Variables
pi.set("this", this);
pi.set("database", collection.getDatabase());
pi.set("collection", collection);
// Arguments
String[] names = (String[])args.keySet().toArray(EmptyStrings);
for ( int i = 0; i < names.length; i++ ) {
Object obj = args.get(names[i]);
pi.set(names[i], obj);
}
// Now run the actual script
pi.exec(script);
// Return any possible result
Object res = pi.get("result");
if ( res != null )
return new Variant(res);
else
return null;
}
}