📄 staxutil.java
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2002 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/package com.ibm.staf.service.stax;import java.util.StringTokenizer;import java.io.BufferedReader;import java.io.StringReader;import java.io.IOException;import org.python.core.PyCode;import org.python.core.__builtin__;import org.python.core.PyException;// STAXUtil - This class provides STAX utility functionspublic class STAXUtil { // Accepts Python code as input and removes any white space from the // beginning of new lines and compiles the Python code to validate its // syntax and returns valid Python code. // // Arguments: // - value: A string containing python code to be parsed for Python // and compiled by to validate its syntax. // - errorInfo: Additional error information to be pre-pended // to the error message if a STAXPythonCompileException // occurs. Allows you to identify the xml element (and // attribute if applicable) containing the invalid // Python code. // Returns: // - A string containing valid Python code with white space removed // from the beginning of new lines public static String parseAndCompileForPython(String value, String errorInfo) throws STAXPythonCompileException { String parsedCode = parseForPython(value); try { compileForPython(parsedCode); } catch (STAXPythonCompileException e) { throw new STAXPythonCompileException(errorInfo + "\n" + e.getMessage()); } return parsedCode; } // Accepts Python code as input and removes any white space from the // beginning of new lines and returned the parsed Python code. // // Arguments: // - value: A string containing python code to be parsed for Python // Returns: // - A string containing Python code with white space removed // from the beginning of new lines public static String parseForPython(String value) { StringBuffer parsedValue = new StringBuffer(); int pos = 0; // Position of first non-whitespace character // in 1st line, taking into account tabs. // Skip any beginning lines that are just white space // (including tabs, carriage returns, etc.) String line; boolean firstLine = true; BufferedReader br = new BufferedReader(new StringReader(value)); StringTokenizer st; try { while ((line = br.readLine()) != null) { if (firstLine) { // Find the number of leading whitespace characters. // Go through the string character by character, and expand // each tab to the appropriate number of spaces required to // reach the next tab stop (set at intervals of 8 characters). st = new StringTokenizer(line); if (st.hasMoreTokens()) { //Found first line that isn't just whitespace firstLine = false; pos = 0; int len = line.length(); int i = 0; // Append 8 spaces for each leading tab character. for (; i < len && line.charAt(i) == '\t'; i++) pos+=8; // For remaining tabs, add enough spaces to get to the next // multiple of 8 (until reach a non-whitespace character). for (; i < len; i++) { char c = line.charAt(i); if (c == ' ') pos++; else if (c == '\t') { do pos++; while (pos % 8 != 0); } else // non-whitespace character found break; } parsedValue.append(line.substring(i)); } } else { // Remove the number of leading white space characters // found in the 1st line (pos) from each additional line // if possible (but don't remove any non-whitespace chars). int pos2 = 0; int len = line.length(); int i = 0; for (; i < len && pos2 <= pos; i++) { char c = line.charAt(i); if (c == ' ') pos2++; else if (c == '\t') { do pos2++; while (pos2 % 8 != 0); } else // non-whitespace character found break; } parsedValue.append("\n"); // Add leading blanks, if any. while (pos2 > pos) { parsedValue.append(" "); pos2--; } parsedValue.append(line.substring(i)); } } } catch (Exception e) { System.out.println(e); return value; } finally { try { br.close(); } catch (IOException e) { System.out.println(e); return value; } } return parsedValue.toString(); } // Accepts parsed Python code as input and compiles the Python code to // validate its syntax. Throws a STAXPythonCompileException if an error // occurs compiling the Python code. // // Arguments: // - value: A string containing parsed python code to be compiled by // Python to validate its syntax. public static void compileForPython(String parsedCode) throws STAXPythonCompileException { try { PyCode code = __builtin__.compile(parsedCode, "<string>", "exec"); } catch (Exception e) { throw new STAXPythonCompileException( "\nPython code compile failed for:\n" + parsedCode + "\n\n" + e.toString()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -