对函数的调用不对应于任何重载

the call to a function does not correspond to any overload of

我正在开发一个 java 库来管理 Crystal 报告中的不同种类的条形码。 Crystal Report 为 Eclipse 提供了一个 sdk,在一个新项目中有用于开发 ERP 和 CR 之间集成的库。这个库中有一个管理条形码,但它是一个糟糕的库,因为我只能管理一种条形码。我找到了一个实现UFL的文档。该文档提供了一个简单的示例,感谢我写了一些 class 来管理三种条形码,但是有一些 Crystal 带有两个参数的报告函数,并且该示例没有显示如何获取正确的实施。我试图开发新的 classes 来管理那种功能,但我总是收到错误消息:"the call to a function does not correspond to any overload of 'name of function'".

对于这个函数,我尝试只传递一个参数,应用程序 运行 很好。我对该方法进行了重载,传递了两个参数,但我总是收到相同的错误消息。

这是我的代码的一部分

public final FormulaValue evaluate(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
  {

       StringValue dataStringArg = (StringValue)arguments[0].getFormulaValue();
       StringValue opzArg =(StringValue)arguments[1].getFormulaValue();

     String dataString = dataStringArg.getString();
     String opz=opzArg.getString();



    String returnVal = doTranslation(dataString,opz);

    return StringValue.fromString(returnVal);
   }

  protected abstract String translate(String inputData,String opz)
   throws InvalidBarcodeDataException;


public class CodeEAN13
  extends CodeEAN13Base
{


   public String getIdentifier()
   {
    return "barcodeean13";
  }

protected String translate(String inputData, String opz) throws InvalidBarcodeDataException {
    Integer checkDigitSubtotal;
.....
}

我除了显示带有那种条形码的报告。 CodeEAN13 class 应该 运行 一种计算条形码字体字符串的算法。

这是程序 运行 时库中的代码,并显示在堆栈跟踪中。对于 stracktrace 的每一行,我都放置了相应的代码。

//com.crystaldecisions.reports.formulas.FormulaService.compile(SourceFile:347)

public void compile(FormulaDefinitionBase paramFormulaDefinitionBase, FormulaContext paramFormulaContext)
    throws FormulaException
  {
    if (paramFormulaDefinitionBase == null) {
      throw new NullPointerException("A formula must be given.");
    }
    paramFormulaDefinitionBase.getFormulaInfo().a(paramFormulaContext);
  }

//com.crystaldecisions.reports.formulas.FormulaInfo.a(SourceFile:570)

void a(FormulaContext paramFormulaContext)
    throws FormulaException
  {
    if (this.jdField_try)
    {
      this.o = true;

      throw new FormulaException("JRC00000433", "", FormulaResources.a(), "RecursiveCompileDetected", this);
    }
    this.o = false;
    if (paramFormulaContext == null) {
      throw new NullPointerException("A formula context must be given.");
    }
    try
    {
      this.jdField_try = true;
      jdMethod_if(paramFormulaContext);
      this.t = this.jdField_long.getFormulaValueType();
      this.jdField_do.a();//(SourceFile:570)
    }
    catch (FormulaException localFormulaException)
    {
      if ((localFormulaException.getResourceKey().equals("RecursiveCompileDetected")) && (this.o)) {
        throw new FormulaException("JRC00000434", "", FormulaResources.a(), "RecursionNotAllowed", null, localFormulaException);
      }
      throw localFormulaException;
    }
    finally
    {
      this.jdField_try = false;
      jdMethod_if();
    }
  }

//com.crystaldecisions.reports.formulas.r.a(SourceFile:90)
public void a()
    throws FormulaException
  {
    FormulaInfo localFormulaInfo = this.a.getFormulaInfo();
    FormulaDefinitionBase localFormulaDefinitionBase = this.a.getFormula();
    String str1 = "Formula";
    String str2 = "";
    if ((localFormulaDefinitionBase instanceof CustomFunctionDefinition))
    {
      str1 = "Custom function";
      str2 = "'";
    }
    if (if.isDebugEnabled()) {
      if.debug("Compiling " + StringUtil.toLowerCaseEng(str1) + " " + str2 + localFormulaDefinitionBase.getFormulaForm() + str2 + "...");
    }
    localFormulaInfo.invalidate();
    try
    {
      ExpressionNode localExpressionNode = o.a(this.a).a();
      localExpressionNode = this.jdField_do.jdMethod_void(localExpressionNode);//(SourceFile:90)
      localExpressionNode = this.jdField_for.c(localExpressionNode);
      localFormulaInfo.a(localExpressionNode);

      localFormulaDefinitionBase.formulaInfoUpdated();
      if (if.isInfoEnabled()) {
        if.info(str1 + " " + str2 + localFormulaDefinitionBase.getFormulaForm() + str2 + " compiled.");
      }
    }
    catch (FormulaException localFormulaException)
    {
      if (if.isInfoEnabled()) {
        if.info(str1 + " " + str2 + localFormulaDefinitionBase.getFormulaForm() + str2 + " did not compile: " + localFormulaException);
      }
      localFormulaInfo.invalidate();

      throw localFormulaException;
    }
  }
}
//com.crystaldecisions.reports.formulas.c.void(SourceFile:70)
 public ExpressionNode jdMethod_void(ExpressionNode paramExpressionNode)
    throws FormulaException
  {
    ExpressionNode localExpressionNode = paramExpressionNode;

    this.jdField_int = 0;
    this.jdField_for = 0;

    FormulaDefinitionBase localFormulaDefinitionBase = this.a.getFormula();
    FormulaInfo localFormulaInfo = this.a.getFormulaInfo();

    localExpressionNode = jdMethod_do(localExpressionNode);//(SourceFile:70)

    FormulaValueType localFormulaValueType = a(localFormulaDefinitionBase, localFormulaInfo, localExpressionNode);
    if ((localExpressionNode != null) && (!localFormulaValueType.equals(localExpressionNode.getFormulaValueType())))
    {
      localExpressionNode = a(localExpressionNode, localFormulaValueType);
      if ((!new) && (!localExpressionNode.getFormulaValueType().equals(localFormulaValueType))) {
        throw new AssertionError("Coercion failed!!! This should never happen.");
      }
    }
    localFormulaInfo.a(localFormulaValueType);

    return localExpressionNode;
  }

  //com.crystaldecisions.reports.formulas.j.do(SourceFile:74)
  final ExpressionNode jdMethod_do(ExpressionNode paramExpressionNode)
    throws FormulaException
  {
    try
    {
      return a(paramExpressionNode);//(SourceFile:74)
    }
    catch (WhosebugError localWhosebugError)
    {
      throw new FormulaException("JRC00000461", "", FormulaResources.a(), "Whosebug", this.a);
    }
  }

  //com.crystaldecisions.reports.formulas.j.a(SourceFile:250)
  ExpressionNode a(ExpressionNode paramExpressionNode)
    throws FormulaException
  {
    if (paramExpressionNode == null) {
      return null;
    }
    switch (paramExpressionNode.jdField_for.value())
    {
        ......
        case 30: 
      return jdMethod_for((MultiOperatorNode)paramExpressionNode);


//com.crystaldecisions.reports.formulas.c.for(SourceFile:1225)
 ExpressionNode jdMethod_for(MultiOperatorNode paramMultiOperatorNode)
    throws FormulaException
  {
    int i = paramMultiOperatorNode.size();
    if (i == 0) {
      return null;
    }
    for (int j = 0; j < i; j++)
    {
      ExpressionNode localExpressionNode = a(paramMultiOperatorNode.getOperand(j));//(SourceFile:1225)
      if (localExpressionNode == null) {
        return null;
      }
      paramMultiOperatorNode.setOperand(j, localExpressionNode);
    }
    paramMultiOperatorNode.jdField_try = paramMultiOperatorNode.getOperand(i - 1).jdField_try;

    return paramMultiOperatorNode;
  } 

  //com.crystaldecisions.reports.formulas.j.a(SourceFile:295)
  case 8: 
      return a((FunctionNode)paramExpressionNode);


//com.crystaldecisions.reports.formulas.j.a(SourceFile:3148)
 ExpressionNode a(FunctionNode paramFunctionNode)
    throws FormulaException
  {
    int i = paramFunctionNode.size();
    ExpressionNode[] arrayOfExpressionNode = ArrayPool.a(i, this.a);
    try
    {
      for (int j = 0; j < i; j++)
      {
        ExpressionNode localExpressionNode2 = a(paramFunctionNode.get(j), arrayOfExpressionNode);
        if (localExpressionNode2 == null) {
          return null;
        }
        arrayOfExpressionNode[j] = localExpressionNode2;
      }
      return a(paramFunctionNode, arrayOfExpressionNode);//(SourceFile:3148)
    }
    finally
    {
      ArrayPool.a(arrayOfExpressionNode, this.a);
    }
  }

 //com.crystaldecisions.reports.formulas.c.a(SourceFile:1809)



//com.crystaldecisions.reports.formulas.j.a(SourceFile:3507)
 FormulaException a(String paramString1, String paramString2, String paramString3, String[] paramArrayOfString, ExpressionNode paramExpressionNode)
  {
    return a(paramString1, paramString2, paramString3, paramArrayOfString, null, paramExpressionNode);
  }

  //com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)
  FormulaException a(String paramString1, String paramString2, String paramString3, String[] paramArrayOfString, Throwable paramThrowable, ExpressionNode paramExpressionNode)
  {
    return new FormulaException(paramString1, paramString2,FormulaResources.a(), paramString3, paramArrayOfString, paramThrowable, this.a, paramExpressionNode);//(SourceFile:3528)
  }


   public FormulaException(String paramString1, String paramString2, CrystalResourcesFactory paramCrystalResourcesFactory, String paramString3, String[] paramArrayOfString, Throwable paramThrowable, FormulaEnvironment paramFormulaEnvironment, ExpressionNode paramExpressionNode)
  {
    super(paramString1, paramString2, paramCrystalResourcesFactory, paramString3, paramArrayOfString, paramThrowable);

    a(paramFormulaEnvironment, paramExpressionNode);
  }

这是完整的代码

import com.crystaldecisions.reports.formulas.FormulaFunction;
import com.crystaldecisions.reports.formulas.FormulaFunctionLibrary;



 public class BarcodeLibrary
   implements FormulaFunctionLibrary
 {
   private final FormulaFunction[] functionArray = {new Code39(), new Code39Ascii(), new CodeI2of5(), new Code2of5()
                                                    ,new CodeC128SetA(),new CodeEAN13()};

   public FormulaFunction getFunction(int functionNumber)
   {
     return this.functionArray[functionNumber];
   }

   public int size()
   {
    return this.functionArray.length;
  }
 }

*************************************************************************
import com.crystaldecisions.reports.common.CrystalResourcesFactory;
import com.crystaldecisions.reports.common.value.FormulaValue;
import com.crystaldecisions.reports.common.value.FormulaValueType;
import com.crystaldecisions.reports.common.value.StringValue;
import com.crystaldecisions.reports.formulas.FormulaFunction;
import com.crystaldecisions.reports.formulas.FormulaFunctionArgumentDefinition;
import com.crystaldecisions.reports.formulas.FormulaFunctionCallException;
import com.crystaldecisions.reports.formulas.FormulaValueReference;
import com.crystaldecisions.reports.formulas.SimpleFormulaFunctionArgumentDefinition;

public abstract class CodeEAN13Base
   implements FormulaFunction
 {
   FormulaFunctionArgumentDefinition[] myArguments = { SimpleFormulaFunctionArgumentDefinition.string };

protected CrystalResourcesFactory resCrystal;



  public CodeEAN13Base()
   {
     this.resCrystal = new CrystalResourcesFactory("Messages");

   }


  protected final String doTranslation(String inputData,String opz)

   {
    String returnVal = "";

     returnVal = returnVal + translateCharWrapper(inputData,opz);



    return returnVal;
   }


   @Override
   public final FormulaValue evaluate(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
  {

       StringValue dataStringArg = (StringValue)arguments[0].getFormulaValue();
       StringValue opzArg =(StringValue)arguments[1].getFormulaValue();

     String dataString = dataStringArg.getString();
     String opz=opzArg.getString();



    String returnVal = doTranslation(dataString,opz);

    return StringValue.fromString(returnVal);
   }

   @Override
   public final FormulaFunctionArgumentDefinition[] getArguments()
  {
     return this.myArguments;
  }

 @Override  
public final FormulaValueType getReturnType()
  {
     return FormulaValueType.string;
   }


   protected abstract String translate(String inputData,String opz)
   throws InvalidBarcodeDataException;

//protected abstract String translateChar(String inputData)
//throws InvalidBarcodeDataException;

 // private String translateCharWrapper(String inputData)
   private String translateCharWrapper(String inputData,String opz)
  {
     String returnString;

     try
    {
        // returnString = translateChar(inputData);
      returnString = translate(inputData,opz);
     } catch (InvalidBarcodeDataException e) {
      returnString = "";
     }

    return returnString;
  }

   @Override
   public void validateArgumentValues(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
   {}
 }

*************************************************************************
public class CodeEAN13
  extends CodeEAN13Base
{

    protected String opz;



   @Override
   public String getIdentifier()
   {
    return "barcodeean13";
  }



  private static String oddBar(String code) {

        return String.valueOf((char)(65+Integer.parseInt(code)));

    }

  private static String evenBar(String code) {

        return String.valueOf((char)(75+Integer.parseInt(code)));

    }





@Override
protected String translate(String inputData, String opz) throws InvalidBarcodeDataException {
    // TODO Auto-generated method stub
    Integer checkDigitSubtotal;
  String checkDigit;
      String temp = null;
      String code=inputData;
      String azalea_EAN13 = null;
      Object opzM=opz;

      if(!opzM.equals(null)) {
       code=code+opzM;
      }

      //Calculate the EAN-13 check digit.

      checkDigitSubtotal=3*(Integer.parseInt(code.substring(1, 2))+Integer.parseInt(code.substring(3, 4))+Integer.parseInt(code.substring(5, 6))
                           +Integer.parseInt(code.substring(7, 8))+Integer.parseInt(code.substring(9, 10))
                           +Integer.parseInt(code.substring(code.length()-1)));


  checkDigitSubtotal=checkDigitSubtotal+(Integer.parseInt(code.substring(0,1))+Integer.parseInt(code.substring(2, 3))
                                       +Integer.parseInt(code.substring(4, 5))+Integer.parseInt(code.substring(6, 7))
                                       +Integer.parseInt(code.substring(8, 9))+Integer.parseInt(code.substring(10, 11)));

  checkDigit=String.valueOf(300-checkDigitSubtotal);
  checkDigit=checkDigit.substring(checkDigit.length()-1);



 // Begin building the output string with the 1st character's human-readable, L guard bars & odd parity of 1st digit
   temp=(char)(194+Integer.parseInt(code.substring(0,1)))+"x"+oddBar(code.substring(1, 2));  

  // Build the remainder of left half of symbol's parity is based on 1st digit
   switch (code.substring(0,1)) {
   case "0":
      {
         temp=temp+oddBar(code.substring(2, 3));
         temp=temp+oddBar(code.substring(3, 4));
         temp=temp+oddBar(code.substring(4, 5));
         temp=temp+oddBar(code.substring(5, 6));
         temp=temp+oddBar(code.substring(6, 7));         
      }
    break;
   case "1":
      {
          temp=temp+oddBar(code.substring(2, 3)); 
          temp=temp+evenBar(code.substring(3, 4));
          temp=temp+oddBar(code.substring(4, 5));
          temp=temp+evenBar(code.substring(5, 6));
          temp=temp+evenBar(code.substring(6, 7));
      }
      break;
   case "2":
   {
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));
   }
   break;
   case "3":
   {
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));
   }
    break;
   case "4":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "5":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "6":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "7":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));



   }
   break;
   case "8":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "9":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;

    default:
   }

////Add the center guard bars & the build right half of symbol using set C (0-9)

    return azalea_EAN13;
   }


 }


部分日志:

1922 [TSLVReader #1] DEBUG com.crystaldecisions.reports.formulas.Compiler  - Compiling formula {@ean13}...
1922 [TSLVReader #1] INFO com.crystaldecisions.reports.formulas.Compiler  - Formula {@ean13} did not compile: com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1922 [TSLVReader #1] WARN com.crystaldecisions.reports.reportdefinition.ReportDefinition  - com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1923 [TSLVReader #1] ERROR com.crystaldecisions.reports.dataengine  - Formula error: recompile formulas failed
com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3507)
    at com.crystaldecisions.reports.formulas.c.a(SourceFile:1809)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3148)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:295)
    at com.crystaldecisions.reports.formulas.c.for(SourceFile:1225)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:250)
    at com.crystaldecisions.reports.formulas.j.do(SourceFile:74)
    at com.crystaldecisions.reports.formulas.c.void(SourceFile:70)
    at com.crystaldecisions.reports.formulas.r.a(SourceFile:90)
    at com.crystaldecisions.reports.formulas.FormulaInfo.a(SourceFile:570)
    at com.crystaldecisions.reports.formulas.FormulaService.compile(SourceFile:347)
1922 [TSLVReader #1] DEBUG com.crystaldecisions.reports.formulas.Compiler  - Compiling formula {@ean13}...
1922 [TSLVReader #1] INFO com.crystaldecisions.reports.formulas.Compiler  - Formula {@ean13} did not compile: com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1922 [TSLVReader #1] WARN com.crystaldecisions.reports.reportdefinition.ReportDefinition  - com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1923 [TSLVReader #1] ERROR com.crystaldecisions.reports.dataengine  - Formula error: recompile formulas failed
com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3507)
    at com.crystaldecisions.reports.formulas.c.a(SourceFile:1809)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3148)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:295)
    at com.crystaldecisions.reports.formulas.c.for(SourceFile:1225)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:250)
    at com.crystaldecisions.reports.formulas.j.do(SourceFile:74)
    at com.crystaldecisions.reports.formulas.c.void(SourceFile:70)
    at com.crystaldecisions.reports.formulas.r.a(SourceFile:90)
    at com.crystaldecisions.reports.formulas.FormulaInfo.a(SourceFile:570)
    at com.crystaldecisions.reports.formulas.FormulaService.compile(SourceFile:347)

堆栈跟踪告诉您您需要知道的内容,但是我无法提供更具体的信息,因为 Crystal 报告似乎是付费墙并且他们不提供文档(至少是免费的)所以我不能查看jar的来源。
无论如何,这个片段告诉你大部分你需要知道的:

1923 [TSLVReader #1] ERROR com.crystaldecisions.reports.dataengine  - Formula error: recompile formulas failed
com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)

这一行:

com.crystaldecisions.reports.formulas.FormulaException  

告诉您抛出的异常来自 Crystal 报告源代码,而不是 Java 特定错误。因此,如果可以,您应该查阅文档(如果您有权访问)或检查源代码以查看导致抛出的条件.如果您不确定如何操作,您可以在问题中添加一些此类信息,我或其他人可以尝试提供帮助。

at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)

括号中的部分告诉您在哪里查找异常源 SourceFile,可能但不一定在第 3528 行。此行号在实践中可能不同,特别是如果您正在查看的代码是反编译,所以如果事情没有按预期排列,您可能需要四处寻找确切的行。

遗憾的是,如果没有文档或源代码,将很难为您提供进一步的帮助。

问题出在这条语句上:

FormulaFunctionArgumentDefinition[] myArguments = {SimpleFormulaFunctionArgumentDefinition.string };

参数的正确配置是:

FormulaFunctionArgumentDefinition[] myArguments = { SimpleFormulaFunctionArgumentDefinition.string,SimpleFormulaFunctionArgumentDefinition.string };

显然在我的Crystal 报表公式中有两种字符串类型或一种字符串类型,具体取决于公式。 下面是解决具有两个参数的条形码函数的 UFL 的完整代码。此代码为 Crystal 报告设置 UFL 库。

import com.crystaldecisions.reports.common.CrystalResourcesFactory;
import com.crystaldecisions.reports.common.value.FormulaValue;
import com.crystaldecisions.reports.common.value.FormulaValueType;

import com.crystaldecisions.reports.common.value.StringValue;
import com.crystaldecisions.reports.formulas.FormulaFunction;
import com.crystaldecisions.reports.formulas.FormulaFunctionArgumentDefinition;
import com.crystaldecisions.reports.formulas.FormulaFunctionCallException;
import com.crystaldecisions.reports.formulas.FormulaValueReference;
import com.crystaldecisions.reports.formulas.SimpleFormulaFunctionArgumentDefinition;

public abstract class CodeEAN13Base
   implements FormulaFunction
 {
   FormulaFunctionArgumentDefinition[] myArguments = { SimpleFormulaFunctionArgumentDefinition.string,SimpleFormulaFunctionArgumentDefinition.string };

protected CrystalResourcesFactory resCrystal;



  public CodeEAN13Base()
   {
     this.resCrystal = new CrystalResourcesFactory("Messages");

   }


  protected final String doTranslation(String inputData,String opz)
 // protected final String doTranslation(String inputData)
   {
    String returnVal = "";

   //  returnVal = returnVal + translateCharWrapper(inputData);
     returnVal = returnVal + translateCharWrapper(inputData,opz);


    return returnVal;
   }



   @Override
   public final FormulaValue evaluate(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
  {

       StringValue dataStringArg = (StringValue)arguments[0].getFormulaValue();
       StringValue opzArg =(StringValue)arguments[1].getFormulaValue();

     String dataString = dataStringArg.getString();
     String opz=opzArg.getString();





    String returnVal = doTranslation(dataString,opz);

    return StringValue.fromString(returnVal);
   }

   @Override
   public final FormulaFunctionArgumentDefinition[] getArguments()
  {
     return this.myArguments;
  }

 @Override  
public final FormulaValueType getReturnType()
  {
     return FormulaValueType.string;
   }


   protected abstract String translate(String inputData,String opz)
   throws InvalidBarcodeDataException;

//protected abstract String translate(String inputData)
//throws InvalidBarcodeDataException;

 // private String translateCharWrapper(String inputData)
   private String translateCharWrapper(String inputData,String opz)
  {
     String returnString;

     try
    {
        // returnString = translate(inputData);
      returnString = translate(inputData,opz);
     } catch (InvalidBarcodeDataException e) {
      returnString = "";
     }

    return returnString;
  }



   @Override
   public void validateArgumentValues(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
   {}
 }

public class CodeEAN13
  extends CodeEAN13Base
{

    //protected String opz;



   @Override
   public String getIdentifier()
   {
    return "barcodeean13";
  }



  private static String oddBar(String code) {

        return String.valueOf((char)(65+Integer.parseInt(code)));

    }

  private static String evenBar(String code) {

        return String.valueOf((char)(75+Integer.parseInt(code)));

    }


@Override

    protected String translate(String inputData, String opz) throws InvalidBarcodeDataException {   

     Integer checkDigitSubtotal;
     String checkDigit;
      String temp = null;
      String code=inputData;
      String azalea_EAN13 = null;
      String opzM=opz;


//    if(!opzM.equals(null)) {
//     code=code+opzM;
//    }

      //Calculate the EAN-13 check digit.

      checkDigitSubtotal=3*(Integer.parseInt(code.substring(1, 2))+Integer.parseInt(code.substring(3, 4))+Integer.parseInt(code.substring(5, 6))
                           +Integer.parseInt(code.substring(7, 8))+Integer.parseInt(code.substring(9, 10))
                           +Integer.parseInt(code.substring(code.length()-1)));


      checkDigitSubtotal=checkDigitSubtotal+(Integer.parseInt(code.substring(0,1))+Integer.parseInt(code.substring(2, 3))
                                       +Integer.parseInt(code.substring(4, 5))+Integer.parseInt(code.substring(6, 7))
                                       +Integer.parseInt(code.substring(8, 9))+Integer.parseInt(code.substring(10, 11)));

  checkDigit=String.valueOf(300-checkDigitSubtotal);
  checkDigit=checkDigit.substring(checkDigit.length()-1);



 // Begin building the output string with the 1st character's human-readable, L guard bars & odd parity of 1st digit
   temp=(char)(194+Integer.parseInt(code.substring(0,1)))+"x"+oddBar(code.substring(1, 2));  

  // Build the remainder of left half of symbol's parity is based on 1st digit
   switch (code.substring(0,1)) {
   case "0":
      {
         //temp="U|x"; 
         temp=temp+oddBar(code.substring(2, 3));
         temp=temp+oddBar(code.substring(3, 4));
         temp=temp+oddBar(code.substring(4, 5));
         temp=temp+oddBar(code.substring(5, 6));
         temp=temp+oddBar(code.substring(6, 7));         
      }
    break;
   case "1":
      {
          //temp="[|x";
          temp=temp+oddBar(code.substring(2, 3)); 
          temp=temp+evenBar(code.substring(3, 4));
          temp=temp+oddBar(code.substring(4, 5));
          temp=temp+evenBar(code.substring(5, 6));
          temp=temp+evenBar(code.substring(6, 7));
      }
      break;
   case "2":
   {
       //temp="V|x";
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));
   }
   break;
   case "3":
   {
       //temp="W|x";
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));
   }
    break;
   case "4":
   {
       //temp="X|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "5":
   {
       //temp="Y|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "6":
   {
       //temp="Z|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "7":
   {
       //temp="u|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));



   }
   break;
   case "8":
   {
       //temp="\|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "9":
   {
       //temp="]|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;

    default:
   }

////Add the center guard bars & the build right half of symbol using set C (0-9)


    return azalea_EAN13+opzM;
   }




 }