使用Lotus Forms的FCI功能实现一个数字货币到中文货币的转换

FCI是Lotus Forms提供的一种扩展机制,通过这种机制能够扩展Forms Viewer和Forms Server的能力。

前一段时间有个合作伙伴问我Lotus Forms能不能支持将数字货币例如 ¥12,344,325.00自动转换成 壹仟贰佰叁拾肆万肆仟叁佰贰拾伍圆整。这两天抽时间采用FCI写了一个例子,发布出来给大家参考。其中转换的算法不要问我,我也是从网上找的。

package com.ibm.eform;

import com.PureEdge.error.UWIException;
import com.PureEdge.ifx.Extension;
import com.PureEdge.ifx.ExtensionImplBase;
import com.PureEdge.ifx.IFX;

public class FCIExtension extends ExtensionImplBase implements Extension {

	public void extensionInit(IFX ifxMan) throws UWIException {
		new MoneyConvertorFunctionCall(ifxMan);
	}

}


package com.ibm.eform;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.PureEdge.IFSSingleton;
import com.PureEdge.IFSUserDataHolder;
import com.PureEdge.ShortHolder;
import com.PureEdge.ShortListHolder;
import com.PureEdge.StringHolder;
import com.PureEdge.StringListHolder;
import com.PureEdge.error.UWIException;
import com.PureEdge.ifx.IFX;
import com.PureEdge.xfdl.FormNodeP;
import com.PureEdge.xfdl.FunctionCall;
import com.PureEdge.xfdl.FunctionCallImplBase;
import com.PureEdge.xfdl.FunctionCallManager;


public class MoneyConvertorFunctionCall extends FunctionCallImplBase implements FunctionCall
{

	private final String[] strArray1 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};  
    private final String[][] strArray2 = {{"仟","佰","拾","亿"},{"仟","佰","拾","万"},{"仟","佰","拾","圆"}};  
    private final String[] strArray3 = {"角","分"};  
    
    public static final int CONVERTMONEY = 1000;
	/**
	 * @param args
	 */

	public MoneyConvertorFunctionCall(IFX ifxMan) throws UWIException
	{
		FunctionCallManager	theFCM;

		if ((theFCM = IFSSingleton.getFunctionCallManager()) == null)
			throw new UWIException("Needed Function Call Manager");

		ifxMan.registerInterface(this,
			FunctionCall.FUNCTIONCALL_INTERFACE_NAME,
			FunctionCall.FUNCTIONCALL_CURRENT_VERSION,
			FunctionCall.FUNCTIONCALL_MIN_VERSION_SUPPORTED,
			0x01000301, 0, null, theFCM.getDefaultListener( ));

		theFCM.registerFunctionCall(this, "convert_package",
			"convertMoney", MoneyConvertorFunctionCall.CONVERTMONEY,
			FunctionCall.FCI_FOLLOWS_STRICT_CALLING_PARAMETERS,
			"S", 0x01000301, "Converts a money to Chinese style");
	}
    
	private String preProcessString(String str)  
    {  
//		str = NumberFormat.getCurrencyInstance().format(Double.valueOf(str)).toString();
		Pattern pattern = Pattern.compile(",|¥");
		Matcher mat = pattern.matcher(str); 
		return mat.replaceAll("");
    }  
	
	private String renderCurrency(String str)  
	{  
		str = this.preProcessString(str);  
		System.out.println(str);      
//		return String.valueOf(str.indexOf("."));
		if(str.indexOf(".") != -1)
		{
			String str2 = str.substring(0,str.indexOf("."));     //Integer part of str  
			String str3 = str.substring(str.indexOf(".")+1,str.length());   //Decimal part of str  
			
			StringBuffer buffer = new StringBuffer(100);     //Stringbuffer contains currency string  
			processIntegerPart(str2, buffer);  
			processDecimalPart(str3, buffer);  
			return (buffer.toString());  
		}
		return "";
	  }

	private void processIntegerPart(String str2, StringBuffer buffer) {
		int sectionnumb = getStringSectionNumber(str2); 
		for(int i=0;i<sectionnumb;i++){  
			String tmp = getIntegerSubPart(str2, sectionnumb, i);  
			if(Integer.parseInt(tmp) == 0){  
				if(i == (sectionnumb - 1)){  
					if(buffer.charAt(buffer.length() - 1) == '零')  
						buffer.deleteCharAt(buffer.length() - 1);  
					buffer.append('圆');  
					continue;  
				}  
				else{  
					if(buffer.charAt(buffer.length() - 1) == '零')  
						buffer.deleteCharAt(buffer.length() - 1);  
					else  
						buffer.append(strArray1[0]);  
					continue;  
				}     
			}
			
			int intTmp = 0; 
			for(int j=0;j<tmp.length();j++){  
				char c = tmp.charAt(j);  
				if(j == 0){  
					if(c == '0'){  
						if(i>0&&buffer.charAt(buffer.length()-1) == '零')  
							continue;  
						else  
							buffer.append(strArray1[0]);  
					}  
					else{  
						intTmp = Integer.parseInt(""+c);  
						buffer.append(strArray1[intTmp]).append(strArray2[3-sectionnumb+i][4-tmp.length()+j]);  
					}  
				}  
				else if(j!=0&&j<tmp.length()-1){  
					if(c=='0'){  
						if(tmp.charAt(j-1)=='0')  
							continue;  
						else  
							buffer.append(strArray1[0]);  
					}  
					else{  
						intTmp = Integer.parseInt(""+c);  
						buffer.append(strArray1[intTmp]).append(strArray2[3-sectionnumb+i][4-tmp.length()+j]);  
					}  
				}  
				else{  
					if(c=='0'){  
						if(tmp.charAt(j-1)=='0'){  
							buffer.deleteCharAt(buffer.length()-1);  
							buffer.append(strArray2[3-sectionnumb+i][4-tmp.length()+j]);  
						}  
						else  
							buffer.append(strArray2[3-sectionnumb+i][4-tmp.length()+j]);  
					}  
					else{  
						intTmp = Integer.parseInt(""+c);  
						buffer.append(strArray1[intTmp]).append(strArray2[3-sectionnumb+i][4-tmp.length()+j]);  
					}  
				}  
			}  
	}
	}

	private String getIntegerSubPart(String str2, int sectionnumb, int i) {
		String tmp = "";
		if(i == 0)  
			tmp = str2.substring(0,str2.length() - (sectionnumb - 1)*4);  
		else if(sectionnumb > 0)  
			tmp = str2.substring(str2.length()-(sectionnumb-i)*4,str2.length()-(sectionnumb-i-1)*4);
		return tmp;
	}

	private void processDecimalPart(String str3, StringBuffer buffer) {
		if("00".equals(str3))
			buffer.append("整");
		else
			for(int i=0;i<2;i++){  
				char c = str3.charAt(i)   ;  
				int intTmp = Integer.parseInt(""+c);  
				buffer.append(strArray1[intTmp]).append(strArray3[i]);  
			}
	}

	private int getStringSectionNumber(String str2) {
		int intTmp = str2.length()%4;  
		int k = 0;       //k:   parts   of   str   which   divided   by   4  
		if(intTmp == 0)  
			k = str2.length()/4;  
		else  
			k = (int)(str2.length()/4+1);
		return k;
	}

	public void evaluate(String arg0, String arg1, int theFunctionID, int arg3,
			short theCommand, FormNodeP arg5, FormNodeP arg6, IFSUserDataHolder arg7,
			IFSUserDataHolder arg8, FormNodeP[] theArgList, FormNodeP theResult)
			throws UWIException {
		String result = renderCurrency(theArgList[0].getLiteralEx(null));
		theResult.setLiteralEx(null, result);
	}

	public void help(String arg0, String arg1, int arg2,
			IFSUserDataHolder arg3, StringHolder arg4, StringHolder arg5,
			StringHolder arg6, StringListHolder arg7, StringListHolder arg8,
			ShortListHolder arg9, StringHolder arg10, ShortHolder arg11)
			throws UWIException {
		// TODO Auto-generated method stub
		
	}	
}


Manifest-Version: 1.0

Name: com/ibm/eform/FCIExtension.class
IFS-Extension: True



需要注意的是这个代码需要使用jdk 1.4编译,编译完成后打包为jar包,使用MANIFEST.MF进行描述。

打包完成后可以将jar包拷贝到viewer安装目录下的extension目录下,或者直接通过jar附件的方式附加在表单中。

几点需要注意的:
1.MANIFEST.MF文件每行的冒号后需要有一个空格
2.MANIFEST.MF文件最后一行下应该有一个空行
3.
theFCM.registerFunctionCall(this, "convert_package",
			"convertMoney", MoneyConvertorFunctionCall.CONVERTMONEY,
			FunctionCall.FCI_FOLLOWS_STRICT_CALLING_PARAMETERS,
			"S", 0x01000301, "Converts a money to Chinese style");


convert_package命名需要注意有一个下横线

你可能感兴趣的:(C++,c,算法,IBM,Lotus)