📄 misc.java
字号:
}
/**
** Method : optionFilter (String)
** Purpose: Converts all '+'s in a String to ' 's, required for converting
** XML Description values to regular Strings.
**/
public static String optionFilter(String in){
String result = "";
int len = (in!=null)?in.length():0;
for (int i=0; i<len; i++) {
char ch = in.charAt(i);
if (ch == '+')
result+=" ";
else
result+=ch;
}
return result;
}
/** generates a number between 10000-99999
**/
public static int random(){
return 10000+(int)(89999*Math.random());
}
/**
* Replace all the occurrences of "target" with "newStr".
* @return java.lang.String
* @param str java.lang.String
* @param target java.lang.String
*/
public static String replace(String str, String target, String newStr) {
int i = str.indexOf(target);
if (i == -1)
return str;
else
return str.substring(0, i) + newStr + Misc.replace(str.substring(i + target.length()),target,newStr);
}
/**
* This method was created in VisualAge.
* @return java.lang.String
* @param str java.lang.String
* @param target java.lang.String
*/
public static String replaceStr(String str, String target, String newStr) {
int i = str.indexOf(target);
if (i == -1)
return str;
else
return str.substring(0, i) + newStr + Misc.replace(str.substring(i + target.length()),target,newStr);
}
/** change '<' to '<' and '>' to '>'
** required to interpret templates
**/
public static String rmvIneqs(String s){
String result = replaceStr(s, "<", "<");
return replaceStr(result, ">", ">");
}
public static String[] shrink(String[] myStr, int drop) {
String[] result = new String[myStr.length-1];
for (int i=0; i<drop; i++)
result[i]=myStr[i];
for (int i=drop+1; i<myStr.length; i++)
result[i-1]=myStr[i];
return result;
}
/**
** Method : sortTable (String[][])
** Purpose: Sorts a 2D array of Strings by rows, using Misc.toString(String[]) as the
** comparison ordering. In general, rows will be sorted by the values in
** their first column. The sorting algorithm used is a bubble-sort, though
** a Q-sort would have been more efficient.
**/
public static String[][] sortTable(String[][] inputData){
int tableLength = inputData.length;
int tableWidth = (tableLength==0)?0:inputData[0].length;
String[][] result = new String[tableLength][tableWidth];
String[] mappingKeys = new String[tableLength];
int[] mappingArray = new int[tableLength];
for (int a=0; a<tableLength; a++) {
mappingKeys[a] = Misc.toString(inputData[a]);
mappingArray[a]=a;
}
// perform a bubble-sort using 'mappingArray' as
for (int a=0; a<tableLength; a++)
for (int b=0; b<tableLength-a-1; b++)
if (mappingKeys[b].compareTo(mappingKeys[b+1])>0) {
String tempS=mappingKeys[b];
mappingKeys[b]=mappingKeys[b+1];
mappingKeys[b+1]=tempS;
int tempI = mappingArray[b];
mappingArray[b]=mappingArray[b+1];
mappingArray[b+1]=tempI;
}
for (int a=0; a<tableLength; a++)
result[a] = inputData[mappingArray[a]];
return result;
}
/** Write a string s into a file with the name fileName.
**/
public static void string2file(String s, String fileName){
try{
File outfile;
outfile = new File(fileName);
FileOutputStream fout;
fout = new FileOutputStream(outfile);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
bw.write(s);
bw.close();
fout.close();
}catch (Exception e){e.printStackTrace();
}
}
/** Write a string s into a file with the name fileName, in the directory dirName.
**/
public static void string2file(String s, String dirName, String fileName){
try{
File outfile;
outfile = new File(dirName);
outfile.mkdirs();
outfile = new File(dirName + fileName);
FileOutputStream fout;
fout = new FileOutputStream(outfile);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
bw.write(s);
bw.flush();
bw.close();
fout.close();
}catch (Exception e){e.printStackTrace();
}
}
/** transform the Vector to an array.
**/
public static Object[] toArray(Vector v){
int size = v.size();
Object[] result=new Object[size];
int count = 0;
for (Enumeration enum=v.elements(); enum.hasMoreElements();){
result[count]=enum.nextElement();
count++;
}
return result;
}
public static Element[] toElementArray(Vector v){
int size = v.size();
Element[] result=new Element[size];
int count = 0;
for (Enumeration enum=v.elements(); enum.hasMoreElements();){
Object o = enum.nextElement();
if(o instanceof Element){
result[count]=(Element)o;
}else message("Misc.java: Element type expected for the vector.");
count++;
}
return result;
}
/** transform an array of Strings to a string.
**/
public static String toString(String[] array){
if (array==null) return "NULL";
String result = "";
for (int i=0; i<array.length; i++){
String item=(array[i]==null)?"NULL":array[i];
item=(item.equals(""))?"EMPTY":item;
if (i==0) {
result = result +item;
}else result = result + ", " +item;
}
return result;
}
public static String toString(Vector v) {
if (v == null)
return null;
String result = "";
int count = 0;
for (Enumeration e = v.elements(); e.hasMoreElements();) {
Object elem = e.nextElement();
if (count>0) result+=", ";
if (elem instanceof String) {
result += (String) elem;
} else {
result += elem.toString();
}
count++;
}
return result;
}
/**
** Method : toStrings (Vector)
** Purpose: Converts a Vector of String objects to an array of Strings.
**/
public static String[] toStrings(Vector inputV) {
if (inputV == null)
return null;
String[] result = new String[inputV.size()];
int count=0;
for(Enumeration e=inputV.elements();e.hasMoreElements();){
Object elem=e.nextElement();
if(elem instanceof String){
result[count]=(String)elem;
}else {
//System.out.println("Not string vector;");
result[count]=elem.toString();
}
count++;
}
return result;
}
/** converts an array of strings to a vector object
**/
public static Vector toVector(String[] strings) {
Vector result = new Vector();
for (int i=0; i<strings.length; i++)
result.addElement(strings[i]);
return result;
}
/** transpose the two dimensional array.
**/
public static Object[][] transpose(Object[][]array){
if(array==null) {return null;
}else if (array.length==0) {return null;
}else if (array[0].length==0) return null;
Object result[][] = new Object[array[0].length][array.length];
for (int i=0; i<array.length; i++){
for (int j=0; j<array[0].length;j++){
result[j][i]=array[i][j];
}
}
return result;
}
/** transpose a 2D string array
**/
public static String[][] transpose(String[][] array){
if(array==null) {
return null;
} else if (array.length==0) {
return null;
} else if (array[0].length==0)
return null;
String result[][] = new String[array[0].length][array.length];
for (int i=0; i<array.length; i++){
for (int j=0; j<array[0].length;j++){
result[j][i]=array[i][j];
}
}
return result;
}
/** Filter used to converter url special character to normal strings.
<XMP>
'+' needs to be converted to a single whitespace
'=' separate variable and value pairs, eg. "var1=val1&var2=val2&..."
'%2F' needs to be converted to '/'
'%3A' needs to be converted to ':'
'%40' needs to be converted to '@'
'%0D%0A' are created when the XSL version is used, these are CR/LFs, and must be removed
</XMP>
*/
public static String urlFilter(String s){
String result="";
for (int j=0; j<s.length(); j++) {
char ch = s.charAt(j);
switch (ch){
case '+': result+=' ';
case '=':
case '%':
if (s.charAt(j+1)=='2' && s.charAt(j+2)=='F') {
result+="/";
} else if (s.charAt(j+1)=='3' && s.charAt(j+2)=='A') {
result+=":";
} else if (s.charAt(j+1)=='4' && s.charAt(j+2)=='0') {
result+="@";
} else if (s.charAt(j+1)=='0' && s.charAt(j+2)=='D' &&
s.charAt(j+3)=='%' && s.charAt(j+4)=='0' && s.charAt(j+5)=='A') {
}
default: result+=ch;
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -