Commit d3824844 authored by 王业明's avatar 王业明
Browse files

Fix:组件注册,先读原文件第一行,然后根据编码再读一次具体内容

parent f37d9af2
package cn.com.bankit.ide.bap.python.parser; package cn.com.bankit.ide.bap.python.parser;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import cn.com.bankit.ide.bap.constants.IComponentConstants; import cn.com.bankit.ide.bap.constants.IComponentConstants;
import cn.com.bankit.ide.bap.python.parser.module.PythonFile; import cn.com.bankit.ide.bap.python.parser.module.PythonFile;
import cn.com.bankit.ide.bap.python.parser.module.PythonFunction; import cn.com.bankit.ide.bap.python.parser.module.PythonFunction;
import cn.com.bankit.ide.bap.python.parser.module.PythonParameter; import cn.com.bankit.ide.bap.python.parser.module.PythonParameter;
import cn.com.bankit.ide.bap.python.parser.module.PythonTransition; import cn.com.bankit.ide.bap.python.parser.module.PythonTransition;
public class PythonComponentParser implements PythonComponentConstants public class PythonComponentParser implements PythonComponentConstants
{ {
private File file; private File file;
private PythonFile pythonFile; private PythonFile pythonFile;
private int lineCounter = 0; // 当前处理的行数 private int lineCounter = 0; // 当前处理的行数
private int multiLineCommentCount = 0; // 多行注释出现的次数 private int multiLineCommentCount = 0; // 多行注释出现的次数
private Map<String, String> funName = new HashMap<String, String>(); private Map<String, String> funName = new HashMap<String, String>();
private void parseFileName() private void parseFileName()
{ {
String fileName = file.getName(); String fileName = file.getName();
if (fileName.indexOf(".py") != -1) if (fileName.indexOf(".py") != -1)
{ {
pythonFile.setFileName(fileName.substring(0, fileName.indexOf(".py"))); pythonFile.setFileName(fileName.substring(0, fileName.indexOf(".py")));
} }
else if (fileName.indexOf(".PY") != -1) else if (fileName.indexOf(".PY") != -1)
{ {
pythonFile.setFileName(fileName.substring(0, fileName.indexOf(".PY"))); pythonFile.setFileName(fileName.substring(0, fileName.indexOf(".PY")));
} }
} }
public PythonFile getPythonFile() public PythonFile getPythonFile()
{ {
return pythonFile; return pythonFile;
} }
/** /**
* 处理类头部的注释信息 * 处理类头部的注释信息
* *
* @param pyClassModel * @param pyClassModel
* @param lineContent * @param lineContent
*/ */
private void parseFileHeader(String line) private void parseFileHeader(String line)
{ {
String title = line; String title = line;
if (line.indexOf(":") != -1) if (line.indexOf(":") != -1)
{ {
title = line.substring(0,line.indexOf(":")).trim(); title = line.substring(0,line.indexOf(":")).trim();
} }
String context = line.substring(line.indexOf(":") + 1).trim(); String context = line.substring(line.indexOf(":") + 1).trim();
if (C_FILE_PACKAGE.equals(title.replace(" ", ""))) if (C_FILE_PACKAGE.equals(title.replace(" ", "")))
{ {
pythonFile.setPackageName(context); pythonFile.setPackageName(context);
} }
else if (C_FILE_LEVEL.equals(title.replace(" ", ""))) else if (C_FILE_LEVEL.equals(title.replace(" ", "")))
{ {
for (int i = 0; i < C_FILE_LEVELS.length; i++) for (int i = 0; i < C_FILE_LEVELS.length; i++)
{ {
if (context.equals(C_FILE_LEVELS[i].replace(" ", ""))) if (context.equals(C_FILE_LEVELS[i].replace(" ", "")))
{ {
pythonFile.setComponetLevel(context); pythonFile.setComponetLevel(context);
break; break;
} }
} }
} }
else if (C_FILE_DEPRECATED.equals(title.replace(" ", ""))) else if (C_FILE_DEPRECATED.equals(title.replace(" ", "")))
{ {
pythonFile.setDeprecated(true); pythonFile.setDeprecated(true);
} }
else if (C_FILE_VERSION.equals(title.replace(" ", ""))) else if (C_FILE_VERSION.equals(title.replace(" ", "")))
{ {
pythonFile.setVersion(context); pythonFile.setVersion(context);
} }
else if (C_FILE_PRIORITY.equals(title.replace(" ", ""))) else if (C_FILE_PRIORITY.equals(title.replace(" ", "")))
{ {
pythonFile.setPriority(context); pythonFile.setPriority(context);
} }
} }
private boolean isStartFun(String line) private boolean isStartFun(String line)
{ {
if (multiLineCommentCount % 2 == 1) if (multiLineCommentCount % 2 == 1)
return false; return false;
if (!line.startsWith(FUNCTION_DEF + " ")) if (!line.startsWith(FUNCTION_DEF + " "))
return false; return false;
return true; return true;
} }
private void parseFunction(String line, BufferedReader br) throws IOException private void parseFunction(String line, BufferedReader br) throws IOException
{ {
PythonFunction pyFunction = new PythonFunction(); PythonFunction pyFunction = new PythonFunction();
if (parseFunctionDecleration(pyFunction, line)) if (parseFunctionDecleration(pyFunction, line))
{ {
if (parseFunctionBlock(pyFunction, br)) if (parseFunctionBlock(pyFunction, br))
{ {
if (funName.containsKey(pyFunction.getComponetName())) if (funName.containsKey(pyFunction.getComponetName()))
{ {
pyFunction.setSameNameFun(funName.get(pyFunction.getComponetName())); pyFunction.setSameNameFun(funName.get(pyFunction.getComponetName()));
} }
else else
{ {
pythonFile.getListFunction().add(pyFunction); pythonFile.getListFunction().add(pyFunction);
funName.put(pyFunction.getComponetName(), pyFunction.getFunctionName()); funName.put(pyFunction.getComponetName(), pyFunction.getFunctionName());
} }
} }
} }
} }
private boolean parseFunctionDecleration(PythonFunction pyFunction, String functionDecleration) private boolean parseFunctionDecleration(PythonFunction pyFunction, String functionDecleration)
{ {
String functionSignification = functionDecleration.substring(FUNCTION_DEF.length()).trim(); String functionSignification = functionDecleration.substring(FUNCTION_DEF.length()).trim();
String functionName = functionSignification.substring(0, functionSignification.indexOf("(")).trim(); String functionName = functionSignification.substring(0, functionSignification.indexOf("(")).trim();
//过滤 //过滤
if (!functionName.matches("^((" + IComponentConstants.CMP_PREFIX_P + ")|(" + IComponentConstants.CMP_PREFIX_B if (!functionName.matches("^((" + IComponentConstants.CMP_PREFIX_P + ")|(" + IComponentConstants.CMP_PREFIX_B
+ ")|(" + IComponentConstants.CMP_PREFIX_A + ")).*")) + ")|(" + IComponentConstants.CMP_PREFIX_A + ")).*"))
return false; return false;
String bracketStr = functionDecleration.substring(functionDecleration.indexOf("(") + 1).trim(); String bracketStr = functionDecleration.substring(functionDecleration.indexOf("(") + 1).trim();
String paramStr = bracketStr.substring(0, bracketStr.indexOf(")")).trim(); String paramStr = bracketStr.substring(0, bracketStr.indexOf(")")).trim();
if (!paramStr.equals("")) if (!paramStr.equals(""))
{ {
String args[] = paramStr.split(","); String args[] = paramStr.split(",");
pyFunction.setDeclareParamNum(args.length); pyFunction.setDeclareParamNum(args.length);
for(String arg : args) for(String arg : args)
{ {
pyFunction.addArgs(arg); pyFunction.addArgs(arg);
} }
} }
pyFunction.setFunctionName(functionName); pyFunction.setFunctionName(functionName);
pyFunction.setLineNum(lineCounter); pyFunction.setLineNum(lineCounter);
return true; return true;
} }
private boolean parseFunctionBlock(PythonFunction pyFunction, BufferedReader br) throws IOException private boolean parseFunctionBlock(PythonFunction pyFunction, BufferedReader br) throws IOException
{ {
String line = null; String line = null;
int functionCommentCount = 0; // 记录当前方法的注释数目信息 int functionCommentCount = 0; // 记录当前方法的注释数目信息
while ((line = br.readLine()) != null) while ((line = br.readLine()) != null)
{ {
line = line.trim(); line = line.trim();
lineCounter++; lineCounter++;
// 读到多行注释,则记录总的注释数信息 并记录当前方法的注释数信息 // 读到多行注释,则记录总的注释数信息 并记录当前方法的注释数信息
if (line.equals(MULTI_LINE_COMMENT)) if (line.equals(MULTI_LINE_COMMENT))
{ {
multiLineCommentCount++; multiLineCommentCount++;
if (functionCommentCount++ == 1) if (functionCommentCount++ == 1)
break; break;
else else
continue; continue;
} }
else if (functionCommentCount == 0 && line.trim().length() != 0) else if (functionCommentCount == 0 && line.trim().length() != 0)
{ {
//没有注释注释返回 //没有注释注释返回
return false; return false;
} }
// 如果当前方法的注释数为1,则认为处在当前方法的注释信息内 处理当前方法的注释 // 如果当前方法的注释数为1,则认为处在当前方法的注释信息内 处理当前方法的注释
if (functionCommentCount == 1) if (functionCommentCount == 1)
{ {
parseFunctionComment(pyFunction, line); parseFunctionComment(pyFunction, line);
} }
} }
return true; return true;
} }
int I_NONE = 0x0000; int I_NONE = 0x0000;
int I_C_FUNCTION_NAME = 0x0001; int I_C_FUNCTION_NAME = 0x0001;
int I_C_FUNCTION_DESC = 0x0002; int I_C_FUNCTION_DESC = 0x0002;
int I_C_FUNCTION_IN_ARGS = 0x0003; int I_C_FUNCTION_IN_ARGS = 0x0003;
int I_C_FUNCTION_OUT_ARGS = 0x0004; int I_C_FUNCTION_OUT_ARGS = 0x0004;
int I_C_FUNCTION_TRANSITIONS = 0x0005; int I_C_FUNCTION_TRANSITIONS = 0x0005;
int I_C_FUNCTION_RETURN = 0x0006; int I_C_FUNCTION_RETURN = 0x0006;
int I_C_FUNCTION_DEPRECATED = 0x0007; int I_C_FUNCTION_DEPRECATED = 0x0007;
int I_C_FUNCTION_PRIORITY = 0x0008; int I_C_FUNCTION_PRIORITY = 0x0008;
int I_C_FUNCTION_VERSION = 0x0009; int I_C_FUNCTION_VERSION = 0x0009;
int I_C_FUNCTION_USECASE = 0x000A; int I_C_FUNCTION_USECASE = 0x000A;
int I_C_FUNCTION_PARAM = 0x000B; int I_C_FUNCTION_PARAM = 0x000B;
int I_ARG_INIT = 0x00; int I_ARG_INIT = 0x00;
int I_ARG_IN = 0x01; int I_ARG_IN = 0x01;
int I_ARG_OUT = 0x02; int I_ARG_OUT = 0x02;
int markFlag = 0; int markFlag = 0;
int isInArgs = I_ARG_INIT; int isInArgs = I_ARG_INIT;
boolean isReturnVal = false; boolean isReturnVal = false;
private boolean containRegKey(String line,String key) private boolean containRegKey(String line,String key)
{ {
// if (line.indexOf(key) != -1 ) // if (line.indexOf(key) != -1 )
// return true; // return true;
if (line.trim().startsWith(key)) if (line.trim().startsWith(key))
return true; return true;
return false; return false;
} }
private void parseFunctionComment(PythonFunction function, String line) private void parseFunctionComment(PythonFunction function, String line)
{ {
int curMarkFlag = I_NONE; int curMarkFlag = I_NONE;
if (containRegKey(line,C_FUNCTION_NAME)) if (containRegKey(line,C_FUNCTION_NAME))
{ {
curMarkFlag = I_C_FUNCTION_NAME; curMarkFlag = I_C_FUNCTION_NAME;
} }
else if (containRegKey(line,C_FUNCTION_DESC)) else if (containRegKey(line,C_FUNCTION_DESC))
{ {
curMarkFlag = I_C_FUNCTION_DESC; curMarkFlag = I_C_FUNCTION_DESC;
} }
else if (containRegKey(line,C_FUNCTION_IN_ARGS)) else if (containRegKey(line,C_FUNCTION_IN_ARGS))
{ {
isInArgs = I_ARG_IN; isInArgs = I_ARG_IN;
return; return;
} }
else if (containRegKey(line,C_FUNCTION_OUT_ARGS)) else if (containRegKey(line,C_FUNCTION_OUT_ARGS))
{ {
isInArgs = I_ARG_OUT; isInArgs = I_ARG_OUT;
return; return;
} }
else if (containRegKey(line,C_FUNCTION_PARAM) && I_ARG_INIT != isInArgs) else if (containRegKey(line,C_FUNCTION_PARAM) && I_ARG_INIT != isInArgs)
{ {
curMarkFlag = I_C_FUNCTION_PARAM; curMarkFlag = I_C_FUNCTION_PARAM;
} }
else if (containRegKey(line,C_FUNCTION_TRANSITIONS)) else if (containRegKey(line,C_FUNCTION_TRANSITIONS))
{ {
isReturnVal = true; isReturnVal = true;
return; return;
} }
else if ( containRegKey(line,C_FUNCTION_RETURN) && isReturnVal) else if ( containRegKey(line,C_FUNCTION_RETURN) && isReturnVal)
{ {
curMarkFlag = I_C_FUNCTION_RETURN; curMarkFlag = I_C_FUNCTION_RETURN;
} }
else if ( containRegKey(line,C_FUNCTION_DEPRECATED)) else if ( containRegKey(line,C_FUNCTION_DEPRECATED))
{ {
curMarkFlag = I_C_FUNCTION_DEPRECATED; curMarkFlag = I_C_FUNCTION_DEPRECATED;
} }
else if ( containRegKey(line,C_FUNCTION_PRIORITY)) else if ( containRegKey(line,C_FUNCTION_PRIORITY))
{ {
curMarkFlag = I_C_FUNCTION_PRIORITY; curMarkFlag = I_C_FUNCTION_PRIORITY;
} }
else if ( containRegKey(line,C_FUNCTION_VERSION)) else if ( containRegKey(line,C_FUNCTION_VERSION))
{ {
curMarkFlag = I_C_FUNCTION_VERSION; curMarkFlag = I_C_FUNCTION_VERSION;
} }
else if ( containRegKey(line,C_FUNCTION_USECASE)) else if ( containRegKey(line,C_FUNCTION_USECASE))
{ {
curMarkFlag = I_C_FUNCTION_USECASE; curMarkFlag = I_C_FUNCTION_USECASE;
} }
else else
{ {
//用户自定义的注释,不进行解析。 //用户自定义的注释,不进行解析。
if(line.trim().startsWith("@") && I_C_FUNCTION_USECASE != markFlag) if(line.trim().startsWith("@") && I_C_FUNCTION_USECASE != markFlag)
{ {
markFlag = I_NONE; markFlag = I_NONE;
return; return;
} }
} }
if (I_NONE == markFlag || I_NONE != curMarkFlag) if (I_NONE == markFlag || I_NONE != curMarkFlag)
{ {
markFlag = curMarkFlag; markFlag = curMarkFlag;
} }
if(I_NONE != markFlag) if(I_NONE != markFlag)
{ {
setValues(function,line,curMarkFlag); setValues(function,line,curMarkFlag);
} }
} }
private void getAndSet(PythonFunction function,String attr, String val, int flag) private void getAndSet(PythonFunction function,String attr, String val, int flag)
{ {
try try
{ {
if(null == function || null == attr || null == val) if(null == function || null == attr || null == val)
return; return;
String strVal = val; String strVal = val;
Field field = function.getClass().getDeclaredField(attr); Field field = function.getClass().getDeclaredField(attr);
field.setAccessible(true); field.setAccessible(true);
if(I_NONE == flag) if(I_NONE == flag)
{ {
Object value = field.get(function); Object value = field.get(function);
if(null != value) if(null != value)
{ {
strVal = String.valueOf(value) + "\n" + strVal; strVal = String.valueOf(value) + "\n" + strVal;
} }
} }
field.set(function, strVal); field.set(function, strVal);
} catch (Exception e) } catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* *
* @param function * @param function
* @param line * @param line
* @param flag 如果为0,说明是换行。 * @param flag 如果为0,说明是换行。
*/ */
private void setValues(PythonFunction function, String line, int flag) private void setValues(PythonFunction function, String line, int flag)
{ {
String context = line; String context = line;
if(I_NONE != flag) if(I_NONE != flag)
{ {
if (line.indexOf(":") != -1) if (line.indexOf(":") != -1)
{ {
context = line.substring(line.indexOf(":") + 1).trim(); context = line.substring(line.indexOf(":") + 1).trim();
} }
else else
{ {
context = line.substring(line.indexOf(":") + 1).trim(); context = line.substring(line.indexOf(":") + 1).trim();
} }
} }
if (I_C_FUNCTION_NAME == markFlag) if (I_C_FUNCTION_NAME == markFlag)
{ {
function.setComponetName(context); function.setComponetName(context);
} }
else if (I_C_FUNCTION_DESC == markFlag) else if (I_C_FUNCTION_DESC == markFlag)
{ {
getAndSet(function,"componetDesc",context,flag); getAndSet(function,"componetDesc",context,flag);
} }
else if (I_C_FUNCTION_PARAM == markFlag) else if (I_C_FUNCTION_PARAM == markFlag)
{ {
if (I_ARG_IN == isInArgs)// 入参 if (I_ARG_IN == isInArgs)// 入参
{ {
function.getInArgList().add(parserParam(line)); function.getInArgList().add(parserParam(line));
} }
if (I_ARG_OUT == isInArgs)// 出参 if (I_ARG_OUT == isInArgs)// 出参
{ {
function.getOutArgList().add(parserParam(line)); function.getOutArgList().add(parserParam(line));
} }
} }
else if ( I_C_FUNCTION_RETURN == markFlag && isReturnVal) else if ( I_C_FUNCTION_RETURN == markFlag && isReturnVal)
{ {
function.getReturnType().add(parserReturn(line)); function.getReturnType().add(parserReturn(line));
} }
else if ( I_C_FUNCTION_DEPRECATED == markFlag) else if ( I_C_FUNCTION_DEPRECATED == markFlag)
{ {
function.setDeprecated(true); function.setDeprecated(true);
} }
else if (I_C_FUNCTION_PRIORITY == markFlag) else if (I_C_FUNCTION_PRIORITY == markFlag)
{ {
function.setPriority(context); function.setPriority(context);
} }
else if ( I_C_FUNCTION_VERSION == markFlag) else if ( I_C_FUNCTION_VERSION == markFlag)
{ {
function.setVersion(context); function.setVersion(context);
} }
else if ( I_C_FUNCTION_USECASE == markFlag ) else if ( I_C_FUNCTION_USECASE == markFlag )
{ {
getAndSet(function,"useCase",context,flag); getAndSet(function,"useCase",context,flag);
} }
} }
private PythonParameter parserParam(String line) private PythonParameter parserParam(String line)
{ {
PythonParameter parameter = new PythonParameter(); PythonParameter parameter = new PythonParameter();
String[] strArr = splitBySpace(line, 4); String[] strArr = splitBySpace(line, 4);
String name = strArr[1]; String name = strArr[1];
String type = strArr[2]; String type = strArr[2];
String desc = strArr[3]; String desc = strArr[3];
parameter.setName(name); parameter.setName(name);
parameter.setType(type); parameter.setType(type);
parameter.setDesc(desc); parameter.setDesc(desc);
return parameter; return parameter;
} }
private PythonTransition parserReturn(String line) private PythonTransition parserReturn(String line)
{ {
PythonTransition trans = new PythonTransition(); PythonTransition trans = new PythonTransition();
String[] strArr = splitBySpace(line, 3); String[] strArr = splitBySpace(line, 3);
String name = strArr[1]; String name = strArr[1];
String desc = strArr[2]; String desc = strArr[2];
trans.setName(name); trans.setName(name);
trans.setDesc(desc); trans.setDesc(desc);
return trans; return trans;
} }
public PythonFile parse(File file, String charsetName) throws PythonParseExcption public PythonFile parse(File file, String charsetName) throws PythonParseExcption
{ {
this.file = file; this.file = file;
BufferedReader br = null; BufferedReader br = null;
try charsetName = getCharsetName(file, charsetName);
{ try
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName)); {
pythonFile = new PythonFile(); br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
parseFileName(); pythonFile = new PythonFile();
parseFileName();
String line = null;
while ((line = br.readLine()) != null) String line = null;
{ while ((line = br.readLine()) != null)
line = line.trim(); {
lineCounter++; line = line.trim();
// 出现多行注释标志,就记录下多行注释数,不做处理 lineCounter++;
if (line.equals(MULTI_LINE_COMMENT)) // 出现多行注释标志,就记录下多行注释数,不做处理
{ if (line.equals(MULTI_LINE_COMMENT))
multiLineCommentCount++; {
continue; multiLineCommentCount++;
} continue;
// 第一次出现多行注释,就作为文件注释 }
if (multiLineCommentCount == 1) // 第一次出现多行注释,就作为文件注释
{ if (multiLineCommentCount == 1)
parseFileHeader(line); {
// if (1 == res) parseFileHeader(line);
// { // if (1 == res)
// return pythonFile; // {
// } // return pythonFile;
} // }
// 不在多行注释内,且读到一个function标志,则处理function }
else if (isStartFun(line)) // 不在多行注释内,且读到一个function标志,则处理function
{ else if (isStartFun(line))
parseFunction(line, br); {
} parseFunction(line, br);
} }
}
// 处理过时函数
// pyCompileUnit.handleDeprecatedFunction(); // 处理过时函数
List<String> list = pythonFile.check(); // pyCompileUnit.handleDeprecatedFunction();
if (list.size() > 0) List<String> list = pythonFile.check();
{ if (list.size() > 0)
throw new PythonParseExcption(list); {
} throw new PythonParseExcption(list);
return pythonFile; }
} return pythonFile;
catch (IOException e) }
{ catch (IOException e)
throw new PythonParseExcption(e); {
} throw new PythonParseExcption(e);
finally }
{ finally
if (br != null) {
{ if (br != null)
try {
{ try
br.close(); {
} br.close();
catch (IOException e) }
{ catch (IOException e)
e.printStackTrace(); {
} e.printStackTrace();
} }
} }
}
}
}
private String[] splitBySpace(String source, int size)
{ private String[] splitBySpace(String source, int size)
String[] words = new String[size]; {
String[] words = new String[size];
for (int i = 0; i < size; i++)
{ for (int i = 0; i < size; i++)
int indexOf = source.indexOf(" "); {
if (indexOf != -1) int indexOf = source.indexOf(" ");
{ if (indexOf != -1)
words[i] = source.substring(0, indexOf); {
source = source.substring(indexOf).trim(); words[i] = source.substring(0, indexOf);
} source = source.substring(indexOf).trim();
else }
{ else
words[i] = source.trim(); {
} words[i] = source.trim();
} }
}
return words;
} return words;
} }
public String getCharsetName(File file, String charsetName) throws PythonParseExcption
{
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
String line = br.readLine();
int index = -1;
if (line != null) {
if ((index = line.indexOf("# -*- coding:")) > -1) {
int lastIdx = line.lastIndexOf("-*-");
if (lastIdx > -1) {
charsetName = line.substring(index + 13, lastIdx).trim();
} else {
charsetName = line.substring(index + 13).trim();
}
}
}
return charsetName;
}
catch (IOException e)
{
throw new PythonParseExcption(e);
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment