/**
*
* Copyright 2014 The Darks ORM Project (Liu lihua)
*
* 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 darks.orm.core.aspect.jython;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.python.core.PyNone;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import darks.orm.app.QueryEnumType;
import darks.orm.core.data.xml.AspectData;
import darks.orm.core.data.xml.AspectData.AspectType;
import darks.orm.core.data.xml.QueryAspectWrapper;
import darks.orm.core.data.xml.SimpleAspectWrapper;
import darks.orm.core.factory.JythonFactory;
import darks.orm.exceptions.AspectException;
import darks.orm.exceptions.JythonAspectException;
public class PythonParser
{
public static String JY_ASPECT_DATA = "__DATA";
/**
* Parse python script data
*
* @param aspectData Aspect data
* @param simpleWrapper {@linkplain darks.orm.core.data.xml.SimpleAspectWrapper SimpleAspectWrapper}
* @param queryEnumType {@linkplain darks.orm.app.QueryEnumType QueryEnumType}
* @param methodType ������������ JY_ASPECT_BEFORE or JY_ASPECT_AFTER
* @return �Ƿ����ִ��
*/
public boolean parse(AspectData aspectData, SimpleAspectWrapper simpleWrapper, QueryEnumType queryEnumType,
String methodType)
throws JythonAspectException
{
if (aspectData == null)
return true;
PythonInterpreter pyInter = initPythonInterpreter(simpleWrapper);
if (aspectData.getAspectType() == AspectType.JYTHON)
{
String content = PythonBuilder.buildJython(aspectData, aspectData.getClassName(), aspectData.getContent());
try
{
pyInter.exec(content);
}
catch (Exception e)
{
e.printStackTrace();
throw new JythonAspectException(e);
}
}
else if (aspectData.getAspectType() == AspectType.PYFILE)
{
pyInter.exec(PythonBuilder.getPythonHeader().toString());
pyInter.execfile(getFileInputStream(aspectData.getContent()));
pyInter.exec(PythonBuilder.buildJythonTail(aspectData.getClassName()));
}
else
{
return true;
}
PyObject aspectObj = pyInter.get(PythonBuilder.JY_ASPECT_CLASS);
PyObject retObj = aspectObj.invoke(methodType);
if (retObj == null || retObj instanceof PyNone)
{
return false;
}
else
{
Integer ret = (Integer)JythonFactory.getInstance().pyObjectToObject(retObj, Integer.class);
if (ret == 0)
return false;
else
return true;
}
}
/**
* ��ʼ��Python���沢���PythonInterpreter
*
* @param queryData ��ѯ����
* @param sql SQL���
* @param values ѡ��ֵ
* @param params ����
* @param page ��ǰҳ
* @param pageSize ��ҳ��С
* @return PythonInterpreter ʵ��
*/
public PythonInterpreter initPythonInterpreter(SimpleAspectWrapper simpleWrapper)
{
PythonInterpreter pyInter = JythonFactory.getInstance().getInterpreter();
if (simpleWrapper instanceof QueryAspectWrapper)
{
pyInter.set(JY_ASPECT_DATA, (QueryAspectWrapper)simpleWrapper);
}
else
{
pyInter.set(JY_ASPECT_DATA, simpleWrapper);
}
return pyInter;
}
private InputStream getFileInputStream(String fileName)
{
if (fileName == null || "".equals(fileName.trim()))
{
throw new AspectException("Invalid jython file name " + fileName);
}
InputStream result = null;
InputStream ins = this.getClass().getResourceAsStream(fileName);
if (ins != null)
{
result = new BufferedInputStream(ins);
}
else
{
try
{
result = new BufferedInputStream(new FileInputStream(fileName));
}
catch (FileNotFoundException e)
{
throw new AspectException("Cannot find jython file " + fileName);
}
}
return result;
}
}