Issue #2250 refactor JUtil to be less dependent on certain elements, make DAF easier to use
Change-Id: Ic89f4669529ac13f3c69ede8553cf57bbfd73760 Former-commit-id:312ec66384
[formerlydc160577cc
] [formerlyb3573e90c9
] [formerlyc2d6c2af41
[formerlyb3573e90c9
[formerly 48c392d8000d8cc3a5c8371319fddd776566ef32]]] Former-commit-id:c2d6c2af41
Former-commit-id: 53e526c2f13fdf394ffb581d1c56b5b036a55a63 [formerlya3ec6e58f5
] Former-commit-id:8d3cf15469
This commit is contained in:
parent
dd2bb59742
commit
d41115860f
6 changed files with 513 additions and 127 deletions
|
@ -73,7 +73,7 @@ public class DefaultGeometryData implements IGeometryData {
|
|||
|
||||
protected String locationName;
|
||||
|
||||
protected Map<String, Object> attributes;
|
||||
protected Map<String, Object> attributes = new HashMap<String, Object>();
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String key) {
|
||||
|
@ -277,6 +277,10 @@ public class DefaultGeometryData implements IGeometryData {
|
|||
this.dataMap.put(parameter, data);
|
||||
}
|
||||
|
||||
public void addAttribute(String key, Object value) {
|
||||
attributes.put(key, value);
|
||||
}
|
||||
|
||||
public void setGeometry(Geometry geom) {
|
||||
this.geometry = geom;
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ import com.vividsolutions.jts.geom.Envelope;
|
|||
* Jan 28, 2013 bkowal Initial creation
|
||||
* Feb 14, 2013 1614 bsteffen Refactor data access framework to use
|
||||
* single request.
|
||||
* Oct 30, 2013 mnash Allow for no parameters to be set.
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
|
@ -152,8 +153,7 @@ public class MapsQueryAssembler {
|
|||
* the original request that we are processing
|
||||
* @return the query
|
||||
*/
|
||||
public static String assembleGetAvailableLocationNames(
|
||||
IDataRequest request) {
|
||||
public static String assembleGetAvailableLocationNames(IDataRequest request) {
|
||||
return assembleQuery(request, Boolean.TRUE);
|
||||
}
|
||||
|
||||
|
@ -190,8 +190,10 @@ IDataRequest request) {
|
|||
// add any additional database columns the user has specified as
|
||||
// parameters
|
||||
// for additional information, refer to: http://tinyurl.com/arnayco
|
||||
for (String parameter : request.getParameters()) {
|
||||
columns.add(parameter);
|
||||
if (request.getParameters() != null) {
|
||||
for (String parameter : request.getParameters()) {
|
||||
columns.add(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> constraints = new ArrayList<String>();
|
||||
|
|
|
@ -30,6 +30,7 @@ package com.raytheon.uf.common.python;
|
|||
* Date Ticket# Engineer Description
|
||||
* ------------ ---------- ----------- --------------------------
|
||||
* Aug 22, 2013 mnash Initial creation
|
||||
* Oct 30, 2013 mnash Add method for searching for subclasses
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
|
@ -42,4 +43,8 @@ public class PyJavaUtil {
|
|||
public static boolean isArray(Object obj) {
|
||||
return obj.getClass().isArray();
|
||||
}
|
||||
|
||||
public static boolean isSubclass(Object obj, Class<?> clazz) {
|
||||
return clazz.isAssignableFrom(obj.getClass());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
# #
|
||||
# This software was developed and / or modified by Raytheon Company,
|
||||
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
|
||||
#
|
||||
# U.S. EXPORT CONTROLLED TECHNICAL DATA
|
||||
# This software product contains export-restricted data whose
|
||||
# export/transfer/disclosure is restricted by U.S. law. Dissemination
|
||||
# to non-U.S. persons whether in the United States or abroad requires
|
||||
# an export license or other authorization.
|
||||
#
|
||||
# Contractor Name: Raytheon Company
|
||||
# Contractor Address: 6825 Pine Street, Suite 340
|
||||
# Mail Stop B8
|
||||
# Omaha, NE 68106
|
||||
# 402.291.0100
|
||||
#
|
||||
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
|
||||
# further licensing information.
|
||||
# #
|
||||
|
||||
|
||||
from com.vividsolutions.jts.io import WKTReader
|
||||
from com.vividsolutions.jts.geom import Geometry
|
||||
from shapely.geometry.base import BaseGeometry
|
||||
from shapely import wkt
|
||||
|
||||
from com.raytheon.uf.common.python import PyJavaUtil
|
||||
|
||||
#
|
||||
# Handler for geometries from Python to Java and back.
|
||||
#
|
||||
#
|
||||
#
|
||||
# SOFTWARE HISTORY
|
||||
#
|
||||
# Date Ticket# Engineer Description
|
||||
# ------------ ---------- ----------- --------------------------
|
||||
# 10/14/13 2250 mnash Initial creation of JUtil handler
|
||||
#
|
||||
#
|
||||
|
||||
def shapelyToJTS(val):
|
||||
valtype = type(val)
|
||||
if issubclass(valtype, BaseGeometry):
|
||||
reader = WKTReader()
|
||||
retObj = reader.read(val.to_wkt())
|
||||
return True, retObj
|
||||
return False, val
|
||||
|
||||
def jtsToShapely(obj, customConverter=None):
|
||||
if PyJavaUtil.isSubclass(obj, Geometry):
|
||||
return True, _toShapely(obj)
|
||||
return False, obj
|
||||
|
||||
def _toShapely(obj):
|
||||
return wkt.loads(obj.toText())
|
|
@ -18,25 +18,29 @@
|
|||
# further licensing information.
|
||||
# #
|
||||
|
||||
|
||||
from java.lang import Integer, Float, Long, Boolean, String, Object, Double
|
||||
from java.util import HashMap, LinkedHashMap, ArrayList
|
||||
from java.util import Collections
|
||||
from java.util import Date
|
||||
from java.lang.reflect import Array
|
||||
from collections import OrderedDict
|
||||
|
||||
from shapely.geometry.base import BaseGeometry
|
||||
from shapely import wkt
|
||||
|
||||
import jep
|
||||
import datetime
|
||||
from com.raytheon.uf.common.python import PyJavaUtil
|
||||
from java.util import HashMap, LinkedHashMap, ArrayList
|
||||
from java.lang import String
|
||||
|
||||
#
|
||||
# Provides convenience methods for Java-Python bridging
|
||||
#
|
||||
# For utilization of the basics, doing the following will work just fine :
|
||||
#
|
||||
# import JUtil
|
||||
# JUtil.pyValToJavaObj(val)
|
||||
#
|
||||
# However, to do more advanced conversion, you can register a handler and it will then be
|
||||
# available to do the necessary conversion.
|
||||
#
|
||||
# For example, Geometry objects are not currently handled. The GeometryHandler module will
|
||||
# handle them however. Code would be written as follows :
|
||||
#
|
||||
# import JUtil
|
||||
# from GeometryHandler import jtsToShapely, shapelyToJTS
|
||||
# JUtil.registerJavaToPython(jtsToShapely)
|
||||
# JUtil.registerPythonToJava(shapelyToJTS)
|
||||
# JUtil.pyValToJavaObj(val)
|
||||
#
|
||||
# SOFTWARE HISTORY
|
||||
#
|
||||
|
@ -46,12 +50,58 @@ from com.raytheon.uf.common.python import PyJavaUtil
|
|||
# 03/12/13 1759 dgilling Extend Java List types handled
|
||||
# by javaObjToPyVal().
|
||||
# 08/20/13 2250 mnash Handle Dates, doubles, and arrays
|
||||
# 10/15/13 mnash Refactor to reduce dependencies and clean up
|
||||
#
|
||||
#
|
||||
|
||||
JEP_ARRAY_TYPE = type(jep.jarray(0,Object))
|
||||
|
||||
|
||||
def registerPythonToJava(method):
|
||||
'''
|
||||
We want to register new handlers per PythonInterpreter.
|
||||
This allows us to utilize different conversions that may not
|
||||
be needed for some PythonInterpreters, but to use them in others
|
||||
where they are required.
|
||||
'''
|
||||
pythonHandlers.append(method)
|
||||
|
||||
def registerJavaToPython(method):
|
||||
javaHandlers.append(method)
|
||||
|
||||
def pyValToJavaObj(val):
|
||||
'''
|
||||
The main method for Python to Java conversion in JUtil. If no conversion
|
||||
method can be found, a string representation of the python value will be
|
||||
returned as we do not want to crash the JVM.
|
||||
'''
|
||||
retVal = val
|
||||
if retVal is not None :
|
||||
for handleMethod in pythonHandlers:
|
||||
success, obj = handleMethod(val)
|
||||
if success:
|
||||
retVal = obj
|
||||
break
|
||||
return retVal
|
||||
|
||||
def javaObjToPyVal(obj, customConverter=None):
|
||||
'''
|
||||
The main method for Java to Python conversion in JUtil. If no conversion
|
||||
method can be found, the PyJObject will be returned and may still be able
|
||||
to be used that way from within Python.
|
||||
'''
|
||||
retVal = obj
|
||||
if retVal is not None :
|
||||
for handleMethod in javaHandlers:
|
||||
success, val = handleMethod(obj, customConverter)
|
||||
if success:
|
||||
retVal = val
|
||||
break
|
||||
return retVal
|
||||
|
||||
def javaStringListToPylist(jlist):
|
||||
'''
|
||||
Going forward should use javaObjToPyVal instead.
|
||||
'''
|
||||
pylist = []
|
||||
size = jlist.size()
|
||||
for i in range(size):
|
||||
|
@ -59,30 +109,19 @@ def javaStringListToPylist(jlist):
|
|||
return pylist
|
||||
|
||||
def pylistToJavaStringList(pylist):
|
||||
'''
|
||||
Going forward should use pyValToJavaObj instead.
|
||||
'''
|
||||
jlist = ArrayList();
|
||||
for i in pylist:
|
||||
jlist.add(String(i))
|
||||
return jlist
|
||||
|
||||
def javaStringMapToPyDict(javaMap):
|
||||
keys = javaMap.keySet()
|
||||
itr = keys.iterator()
|
||||
pyDict = {}
|
||||
while itr.hasNext():
|
||||
key = itr.next()
|
||||
val = javaMap.get(key)
|
||||
fval = str(val)
|
||||
if fval.find('[') > -1:
|
||||
exec "fval = " + fval
|
||||
else:
|
||||
try:
|
||||
fval = float(fval)
|
||||
except:
|
||||
pass
|
||||
pyDict[str(key)] = fval
|
||||
return pyDict
|
||||
|
||||
def javaMapToPyDict(javaMap, customConverter=None):
|
||||
'''
|
||||
Going forward should use javaObjToPyVal instead.
|
||||
'''
|
||||
keys = javaMap.keySet()
|
||||
itr = keys.iterator()
|
||||
if javaMap.jclassname == "java.util.LinkedHashMap":
|
||||
|
@ -96,6 +135,9 @@ def javaMapToPyDict(javaMap, customConverter=None):
|
|||
return pyDict
|
||||
|
||||
def pyDictToJavaMap(pyDict):
|
||||
'''
|
||||
Going forward should use pyValToJavaObj instead.
|
||||
'''
|
||||
if pyDict is None :
|
||||
return None
|
||||
|
||||
|
@ -108,98 +150,13 @@ def pyDictToJavaMap(pyDict):
|
|||
jmap.put(pyValToJavaObj(key), pyValToJavaObj(pyDict[key]))
|
||||
return jmap
|
||||
|
||||
def pyValToJavaObj(val):
|
||||
retObj = val
|
||||
valtype = type(val)
|
||||
# since Python on 64 bit has larger ints, we need to do a check
|
||||
# for compatibility with Java Integers
|
||||
if valtype is int and val <= Integer.MAX_VALUE and val >= Integer.MIN_VALUE :
|
||||
retObj = Integer(val)
|
||||
elif valtype is float:
|
||||
retObj = Float(val)
|
||||
elif valtype is long or valtype is int:
|
||||
retObj = Long(val)
|
||||
elif valtype is bool:
|
||||
retObj = Boolean(val)
|
||||
elif valtype is list:
|
||||
retObj = ArrayList()
|
||||
for i in val:
|
||||
retObj.add(pyValToJavaObj(i))
|
||||
elif valtype is tuple:
|
||||
tempList = ArrayList()
|
||||
for i in val:
|
||||
tempList.add(pyValToJavaObj(i))
|
||||
retObj = Collections.unmodifiableList(tempList)
|
||||
elif valtype is datetime.datetime:
|
||||
epoch = datetime.datetime.utcfromtimestamp(0)
|
||||
delta = val - epoch
|
||||
retObj = Date(long(delta.total_seconds()) * 1000)
|
||||
elif issubclass(valtype, dict):
|
||||
retObj = pyDictToJavaMap(val)
|
||||
elif issubclass(valtype, JavaWrapperClass):
|
||||
retObj = val.toJavaObj()
|
||||
elif issubclass(valtype, BaseGeometry):
|
||||
from com.vividsolutions.jts.io import WKTReader
|
||||
reader = WKTReader()
|
||||
retObj = reader.read(val.to_wkt())
|
||||
return retObj
|
||||
|
||||
def javaObjToPyVal(obj, customConverter=None):
|
||||
retVal = None
|
||||
if obj is None:
|
||||
return retVal
|
||||
# handle pyjobjects
|
||||
if hasattr(obj, 'jclassname'):
|
||||
objtype = obj.jclassname
|
||||
if objtype == "java.lang.Integer":
|
||||
retVal = obj.intValue()
|
||||
elif objtype == "java.lang.Float":
|
||||
retVal = obj.floatValue()
|
||||
elif objtype == "java.lang.Long":
|
||||
retVal = obj.longValue()
|
||||
elif objtype == "java.lang.Boolean":
|
||||
retVal = bool(obj.booleanValue())
|
||||
elif objtype == "java.lang.Double":
|
||||
retVal = obj.doubleValue()
|
||||
elif objtype == "java.util.Date":
|
||||
retVal = datetime.datetime.fromtimestamp(obj.getTime() / 1000)
|
||||
elif objtype in ["com.vividsolutions.jts.geom.Geometry", "com.vividsolutions.jts.geom.GeometryCollection",
|
||||
"com.vividsolutions.jts.geom.Polygon", "com.vividsolutions.jts.geom.MultiPolygon",
|
||||
"com.vividsolutions.jts.geom.LineString", "com.vividsolutions.jts.geom.MultiLineString",
|
||||
"com.vividsolutions.jts.geom.Point", "com.vividsolutions.jts.geom.MultiPoint",
|
||||
"com.vividsolutions.jts.geom.LinearRing"] :
|
||||
retVal = wkt.loads(obj.toText())
|
||||
elif objtype in ["java.util.ArrayList", "java.util.Arrays$ArrayList"]:
|
||||
retVal = []
|
||||
size = obj.size()
|
||||
for i in range(size):
|
||||
retVal.append(javaObjToPyVal(obj.get(i), customConverter))
|
||||
elif objtype == "java.util.Collections$UnmodifiableRandomAccessList":
|
||||
tempList = []
|
||||
size = obj.size()
|
||||
for i in range(size):
|
||||
tempList.append(javaObjToPyVal(obj.get(i), customConverter))
|
||||
retVal = tuple(tempList)
|
||||
elif objtype == "java.util.HashMap":
|
||||
retVal = javaMapToPyDict(obj, customConverter)
|
||||
elif PyJavaUtil.isArray(obj):
|
||||
retVal = []
|
||||
size = Array.getLength(obj)
|
||||
for i in range(size):
|
||||
retVal.append(javaObjToPyVal(Array.get(obj, i), customConverter))
|
||||
elif customConverter is not None:
|
||||
retVal = customConverter(obj)
|
||||
if retVal is None:
|
||||
retVal = str(obj)
|
||||
# test for jep array type
|
||||
elif isinstance(obj, JEP_ARRAY_TYPE):
|
||||
retVal = []
|
||||
size = len(obj)
|
||||
for i in range(size):
|
||||
retVal.append(javaObjToPyVal(obj[i], customConverter))
|
||||
return retVal
|
||||
|
||||
class JavaWrapperClass(object):
|
||||
def toJavaObj(self):
|
||||
raise NotImplementedError, "Subclasses must override this method."
|
||||
|
||||
# this initializes the basic handlers for Java->Python conversion and Python->Java conversion
|
||||
|
||||
from JUtilHandler import javaBasicsToPyBasics, pyBasicsToJavaBasics, javaCollectionToPyCollection, pyCollectionToJavaCollection, javaClassToPyClass, pyClassToJavaClass
|
||||
|
||||
pythonHandlers = [pyBasicsToJavaBasics, pyCollectionToJavaCollection, pyClassToJavaClass]
|
||||
javaHandlers = [javaBasicsToPyBasics, javaCollectionToPyCollection, javaClassToPyClass]
|
||||
|
|
|
@ -0,0 +1,362 @@
|
|||
# #
|
||||
# This software was developed and / or modified by Raytheon Company,
|
||||
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
|
||||
#
|
||||
# U.S. EXPORT CONTROLLED TECHNICAL DATA
|
||||
# This software product contains export-restricted data whose
|
||||
# export/transfer/disclosure is restricted by U.S. law. Dissemination
|
||||
# to non-U.S. persons whether in the United States or abroad requires
|
||||
# an export license or other authorization.
|
||||
#
|
||||
# Contractor Name: Raytheon Company
|
||||
# Contractor Address: 6825 Pine Street, Suite 340
|
||||
# Mail Stop B8
|
||||
# Omaha, NE 68106
|
||||
# 402.291.0100
|
||||
#
|
||||
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
|
||||
# further licensing information.
|
||||
# #
|
||||
|
||||
import JUtil
|
||||
|
||||
#
|
||||
# Basic handler for Python to Java and back (this handles all the main things
|
||||
# that JUtil needs to do, but none of the special ones that should and can be
|
||||
# added if the code wants to be more powerful.
|
||||
#
|
||||
# Note : All methods that convert from Java to Python should take a custom converter, even if it is not used.
|
||||
#
|
||||
#
|
||||
#
|
||||
# SOFTWARE HISTORY
|
||||
#
|
||||
# Date Ticket# Engineer Description
|
||||
# ------------ ---------- ----------- --------------------------
|
||||
# 10/14/13 2250 mnash Initial creation of JUtil handler
|
||||
#
|
||||
#
|
||||
|
||||
|
||||
from java.lang import Integer, Float, Long, Boolean, String, Double, Number
|
||||
from java.util import Date
|
||||
from com.raytheon.uf.common.python import PyJavaUtil
|
||||
import datetime
|
||||
|
||||
# Java -> Python conversion
|
||||
|
||||
def javaBasicsToPyBasics(obj, customConverter=None):
|
||||
'''
|
||||
Determines the correct method to call out of the dict to convert Java basic
|
||||
objects to Python
|
||||
'''
|
||||
if hasattr(obj, 'jclassname'):
|
||||
classname = obj.jclassname
|
||||
if classname in javaBasics :
|
||||
return True, javaBasics[classname](obj)
|
||||
else :
|
||||
for javaClass in fallbackBasics :
|
||||
if PyJavaUtil.isSubclass(obj, javaClass) :
|
||||
return True, fallbackBasics[javaClass](obj, customConverter)
|
||||
return False, obj
|
||||
|
||||
def _toPythonInt(obj, customConverter=None):
|
||||
'''
|
||||
Turns a Java Integer to a Python int
|
||||
'''
|
||||
return obj.intValue()
|
||||
|
||||
def _toPythonLong(obj, customConverter=None):
|
||||
'''
|
||||
Turns a Java Long to a Python long, or int,
|
||||
depending on the architecture
|
||||
'''
|
||||
return obj.longValue()
|
||||
|
||||
def _toPythonFloat(obj, customConverter=None):
|
||||
'''
|
||||
Turns a Java Float to a Python float
|
||||
'''
|
||||
return obj.floatValue()
|
||||
|
||||
def _toPythonDouble(obj, customConverter=None):
|
||||
'''
|
||||
Turns a Java Double to a Python float
|
||||
'''
|
||||
return obj.doubleValue()
|
||||
|
||||
def _toPythonBool(obj, customConverter=None):
|
||||
'''
|
||||
Turns a Java Boolean to a Python bool
|
||||
'''
|
||||
return bool(obj.booleanValue())
|
||||
|
||||
def _toPythonString(obj, customConverter=None):
|
||||
'''
|
||||
Turns a Java String to a Python str
|
||||
'''
|
||||
return obj.toString()
|
||||
|
||||
def _toPythonDatetime(obj, customConverter=None):
|
||||
'''
|
||||
Turns a Java Date to a Python datetime
|
||||
'''
|
||||
return datetime.datetime.fromtimestamp(obj.getTime() / 1000)
|
||||
|
||||
# Python -> Java conversion
|
||||
|
||||
def pyBasicsToJavaBasics(val):
|
||||
'''
|
||||
Method registered with JUtil to figure out any conversion of Python to Java.
|
||||
Returns a default of String of that value if nothing else can be found.
|
||||
'''
|
||||
valtype = type(val)
|
||||
if valtype in pythonBasics :
|
||||
return True, pythonBasics[valtype](val)
|
||||
return False, str(val)
|
||||
|
||||
def _toJavaInt(val):
|
||||
'''
|
||||
Turns a Python int to a Java Integer, or a Long, depending the range
|
||||
of the value.
|
||||
'''
|
||||
# since Python on 64 bit has larger ints, we need to do a check
|
||||
# for compatibility with Java Integers
|
||||
if val <= Integer.MAX_VALUE and val >= Integer.MIN_VALUE :
|
||||
return Integer(val)
|
||||
# outside the Java bounds for Integer, so make it a Java Long
|
||||
return Long(val)
|
||||
|
||||
def _toJavaFloat(val):
|
||||
'''
|
||||
Turns a Python float to a Java Float
|
||||
'''
|
||||
return Float(val)
|
||||
|
||||
def _toJavaLong(val):
|
||||
'''
|
||||
Turns a Python long to a Java Long
|
||||
'''
|
||||
return Long(val)
|
||||
|
||||
def _toJavaBoolean(val):
|
||||
'''
|
||||
Turns a Python bool to a Java Boolean
|
||||
'''
|
||||
return Boolean(val)
|
||||
|
||||
def _toJavaString(val):
|
||||
'''
|
||||
Turns a Python str to a Java String
|
||||
'''
|
||||
return String(str(val))
|
||||
|
||||
def _toJavaDate(val):
|
||||
'''
|
||||
Turns a Python datetime to a Java Date
|
||||
'''
|
||||
epoch = datetime.datetime.utcfromtimestamp(0)
|
||||
delta = val - epoch
|
||||
return Date(long(delta.total_seconds()) * 1000)
|
||||
|
||||
# the dict that registers the Python data type to the method for conversion
|
||||
pythonBasics = {int:_toJavaInt, float:_toJavaFloat, long:_toJavaLong, bool:_toJavaBoolean, str:_toJavaString, unicode:_toJavaString, datetime.datetime:_toJavaDate}
|
||||
# the dict that registers the Java String of type to the method for conversion
|
||||
javaBasics = {'java.lang.Integer':_toPythonInt, 'java.lang.Float':_toPythonFloat, 'java.lang.Double':_toPythonDouble, 'java.lang.Long':_toPythonLong, 'java.lang.Boolean':_toPythonBool, 'java.lang.String':_toPythonString, 'java.util.Date':_toPythonDatetime}
|
||||
fallbackBasics = {Number:_toPythonFloat}
|
||||
'''
|
||||
The following methods will handle Python and Java collection conversion.
|
||||
'''
|
||||
from java.lang import Object
|
||||
from java.util import Collections, HashMap, LinkedHashMap, ArrayList
|
||||
from java.util import Date
|
||||
from java.lang.reflect import Array
|
||||
from java.util import List, Set, Map
|
||||
|
||||
from collections import OrderedDict
|
||||
import jep
|
||||
|
||||
# make a jarray to find out if we have that
|
||||
JEP_ARRAY_TYPE = type(jep.jarray(0, Object))
|
||||
|
||||
# Java -> Python conversion
|
||||
|
||||
def javaCollectionToPyCollection(obj, customConverter=None):
|
||||
'''
|
||||
Main method to register with JUtil for conversion of Java
|
||||
collections to Python collections.
|
||||
'''
|
||||
if hasattr(obj, 'jclassname'):
|
||||
classname = obj.jclassname
|
||||
if classname in javaCollections :
|
||||
return True, javaCollections[classname](obj, customConverter)
|
||||
elif PyJavaUtil.isArray(obj):
|
||||
return True, _fromJavaArray(obj, customConverter)
|
||||
else :
|
||||
# we have some fallback capability, if we don't specifically handle a class, we
|
||||
# want to try some of the more common types and see if those are available for
|
||||
# conversion
|
||||
for javaClass in fallbackCollections :
|
||||
if PyJavaUtil.isSubclass(obj, javaClass):
|
||||
return True, fallbackCollections[javaClass](obj, customConverter)
|
||||
elif isinstance(obj, JEP_ARRAY_TYPE):
|
||||
return True, _fromJepArray(obj, customConverter)
|
||||
return False, obj
|
||||
|
||||
|
||||
def _toPythonList(obj, customConverter=None):
|
||||
'''
|
||||
Converts to a Python list.
|
||||
'''
|
||||
retVal = []
|
||||
size = obj.size()
|
||||
for i in range(size):
|
||||
retVal.append(JUtil.javaObjToPyVal(obj.get(i), customConverter))
|
||||
return retVal
|
||||
|
||||
def _toPythonTuple(obj, customConverter=None):
|
||||
'''
|
||||
Converts to a Python tuple.
|
||||
'''
|
||||
return tuple(_toPythonList(obj, customConverter))
|
||||
|
||||
def _toPythonDict(obj, customConverter=None):
|
||||
'''
|
||||
Converts to a Python dict.
|
||||
'''
|
||||
return __toPythonDict(obj, {}, customConverter)
|
||||
|
||||
def _toPythonSet(obj, customConverter=None):
|
||||
'''
|
||||
Converts to a Python set.
|
||||
'''
|
||||
retVal = set()
|
||||
size = obj.size()
|
||||
for i in range(size):
|
||||
retVal.add(JUtil.javaObjToPyVal(obj.get(i), customConverter))
|
||||
return retVal
|
||||
|
||||
def _toPythonOrderedDict(obj, customConverter=None):
|
||||
'''
|
||||
Converts to a Python OrderedDict.
|
||||
'''
|
||||
return __toPythonDict(obj, OrderedDict(), customConverter)
|
||||
|
||||
def _fromJavaArray(obj, customConverter=None):
|
||||
'''
|
||||
Converts from a Java array to a Python list.
|
||||
'''
|
||||
retVal = []
|
||||
for i in range(size):
|
||||
retVal.append(JUtil.javaObjToPyVal(Array.get(obj, i), customConverter))
|
||||
return retVal
|
||||
|
||||
def _fromJepArray(obj, customConverter=None):
|
||||
'''
|
||||
Converts from a Jep array to a Python list.
|
||||
'''
|
||||
retVal = []
|
||||
size = len(obj)
|
||||
for i in range(size):
|
||||
retVal.append(JUtil.javaObjToPyVal(obj[i], customConverter))
|
||||
return retVal
|
||||
|
||||
def __toPythonDict(javaMap, pyDict, customConverter=None):
|
||||
'''
|
||||
Converts to a Python dict. Passed in the dict type, and then handles the key conversion.
|
||||
'''
|
||||
keys = javaMap.keySet()
|
||||
itr = keys.iterator()
|
||||
while itr.hasNext() :
|
||||
key = itr.next()
|
||||
obj = javaMap.get(key)
|
||||
pyDict[JUtil.javaObjToPyVal(key)] = JUtil.javaObjToPyVal(obj, customConverter)
|
||||
return pyDict
|
||||
|
||||
# Python -> Java conversion
|
||||
|
||||
def pyCollectionToJavaCollection(val):
|
||||
'''
|
||||
Main method registered with JUtil for conversion of collections in Python
|
||||
to Java collections.
|
||||
'''
|
||||
valtype = type(val)
|
||||
if valtype in pythonCollections :
|
||||
return True, pythonCollections[valtype](val)
|
||||
# not directly in the dict, so lets check whether they are subclasses
|
||||
for pytype in pythonCollections :
|
||||
if issubclass(pytype, valtype):
|
||||
return True, pythonCollections[valtype](val)
|
||||
return False, str(val)
|
||||
|
||||
def _toJavaList(val):
|
||||
'''
|
||||
Turns a Python list to a Java List
|
||||
'''
|
||||
retObj = ArrayList()
|
||||
for i in val :
|
||||
retObj.add(JUtil.pyValToJavaObj(i))
|
||||
return retObj
|
||||
|
||||
def _toJavaUnmodifiableList(val):
|
||||
'''
|
||||
Turns a Python tuple to a Java UnmodifiableList
|
||||
'''
|
||||
return Collections.unmodifiableList(_toJavaList(val))
|
||||
|
||||
def _toJavaLinkedMap(val):
|
||||
'''
|
||||
Turns a Python OrderedDict to a Java LinkedHashMap
|
||||
'''
|
||||
return __toJavaMap(val, LinkedHashMap())
|
||||
|
||||
def _toJavaMap(val):
|
||||
'''
|
||||
Turns a Python dict to a Java HashMap
|
||||
'''
|
||||
return __toJavaMap(val, HashMap())
|
||||
|
||||
def __toJavaMap(pyDict, jmap):
|
||||
'''
|
||||
Does the actual conversion of the elements inside of the dict to Map
|
||||
'''
|
||||
for key in pyDict:
|
||||
jmap.put(JUtil.pyValToJavaObj(key), JUtil.pyValToJavaObj(pyDict[key]))
|
||||
return jmap
|
||||
|
||||
javaCollections = {'java.util.ArrayList':_toPythonList, 'java.util.Arrays$ArrayList':_toPythonList, 'java.util.Collections$UnmodifiableRandomAccessList':_toPythonTuple, 'java.util.HashMap':_toPythonDict, 'java.util.LinkedHashMap':_toPythonOrderedDict}
|
||||
pythonCollections = { list:_toJavaList, tuple:_toJavaUnmodifiableList, OrderedDict:_toJavaLinkedMap, dict:_toJavaMap }
|
||||
fallbackCollections = { List:_toPythonList, Map:_toPythonDict, Set:_toPythonSet }
|
||||
|
||||
'''
|
||||
Handles other types of Java to Python conversion and back.
|
||||
'''
|
||||
|
||||
def javaClassToPyClass(obj, customConverter=None):
|
||||
'''
|
||||
Main method to convert Java classes to Python classes that aren't already defined above.
|
||||
Registers with JUtil
|
||||
'''
|
||||
if customConverter is not None :
|
||||
return True, customConverter(obj)
|
||||
return False, obj
|
||||
|
||||
def pyClassToJavaClass(val):
|
||||
'''
|
||||
Main method that registers with JUtil to convert Python classes to Java classes.
|
||||
'''
|
||||
valtype = type(val)
|
||||
for pyType in pythonClasses :
|
||||
if issubclass(valtype, pyType):
|
||||
return True, pythonClasses[pyType](val)
|
||||
return False, str(val)
|
||||
|
||||
def _toJavaClass(val):
|
||||
'''
|
||||
Utilizes the JUtil.JavaWrapperClass object and its corresponding
|
||||
toJavaObj() method that returns a Java object.
|
||||
'''
|
||||
return val.toJavaObj()
|
||||
|
||||
# registers the data type for conversion to a Java class.
|
||||
pythonClasses = {JUtil.JavaWrapperClass:_toJavaClass}
|
Loading…
Add table
Reference in a new issue