private static String preParseCode(String script){
  final String[] searchFor={"= (\"%.2f\",","this.ADBE"," getField(","\ngetField(","\rgetField(","(getField(","this.getField(","this.resetForm(","this.pageNum"," this.getOCGs(","\nthis.getOCGs(","\rthis.getOCGs("," getOCGs(","\ngetOCGs(","\rgetOCGs(",".state="};
  final String[] replaceWith={"= util.z(\"%.2f\",","ADBE"," acro.getField(","\nacro.getField(","\racro.getField(","(acro.getField(","acro.getField(","acro.resetForm(","acro.pageNum"," layers.getOCGs(","\nlayers.getOCGs(","\rlayers.getOCGs("," layers.getOCGs(","\nlayers.getOCGs(","\rlayers.getOCGs(","\rlayers.getOCGs("};
  for (int i=0; i < searchFor.length; i++) {
    script=checkAndReplaceCode(searchFor[i],replaceWith[i],script);
  }
  final int indexs=script.indexOf("printf");
  printf:   if (indexs != -1) {
    final StringBuilder buf=new StringBuilder();
    int indexStart=script.lastIndexOf(';',indexs);
    final int indextmp=script.lastIndexOf('{',indexs);
    if (indexStart == -1 || (indextmp != -1 && indextmp > indexStart)) {
      indexStart=indextmp;
    }
    buf.append(script.substring(0,indexStart + 1));
    int speech=script.indexOf('\"',indexs);
    speech=script.indexOf('\"',speech + 1);
    while (script.charAt(speech - 1) == '\\') {
      speech=script.indexOf('\"',speech);
    }
    final int startArgs=script.indexOf(',',speech);
    final int endArgs=script.indexOf(')',startArgs);
    final String arguments=script.substring(startArgs + 1,endArgs);
    if (arguments.equals("printfArgs")) {
      break printf;
    }
    final StringTokenizer tok=new StringTokenizer(arguments,", ");
    buf.append("var printfArgs=new Array();\n");
    int i=0;
    while (tok.hasMoreTokens()) {
      buf.append("printfArgs[");
      buf.append(i++);
      buf.append("]=");
      buf.append(tok.nextToken());
      buf.append(";\n");
    }
    buf.append(script.substring(indexStart + 1,startArgs + 1));
    buf.append("printfArgs");
    buf.append(script.substring(endArgs));
    script=buf.toString();
  }
  script=checkAndReplaceCode("event.value=AFMakeNumber(acro.getField(\"sum\").value)(8)","",script);
  script=checkAndReplaceCode("calculate = false","calculate = 0",script);
  script=checkAndReplaceCode("calculate = true","calculate = 1",script);
  script=checkAndReplaceCode("calculate=false","calculate=0",script);
  script=checkAndReplaceCode("calculate=true","calculate=1",script);
  return script;
}

private static String preParseCode ( String script ) { final String [ ] searchFor = { "= (\"%.2f\"," , "this.ADBE" , " getField(" , "\ngetField(" , "\rgetField(" , "(getField(" , "this.getField(" , "this.resetForm(" , "this.pageNum" , " this.getOCGs(" , "\nthis.getOCGs(" , "\rthis.getOCGs(" , " getOCGs(" , "\ngetOCGs(" , "\rgetOCGs(" , ".state=" } ; final String [ ] replaceWith = { "= util.z(\"%.2f\"," , "ADBE" , " acro.getField(" , "\nacro.getField(" , "\racro.getField(" , "(acro.getField(" , "acro.getField(" , "acro.resetForm(" , "acro.pageNum" , " layers.getOCGs(" , "\nlayers.getOCGs(" , "\rlayers.getOCGs(" , " layers.getOCGs(" , "\nlayers.getOCGs(" , "\rlayers.getOCGs(" , "\rlayers.getOCGs(" } ; for ( int i = 0 ; i < searchFor . length ; i ++ ) { script = checkAndReplaceCode ( searchFor [ i ] , replaceWith [ i ] , script ) ; } final int indexs = script . indexOf ( "printf" ) ; printf : if ( indexs != - 1 ) { final StringBuilder buf = new StringBuilder ( ) ; int indexStart = script . lastIndexOf ( ';' , indexs ) ; final int indextmp = script . lastIndexOf ( '{' , indexs ) ; if ( indexStart == - 1 || ( indextmp != - 1 && indextmp > indexStart ) ) { indexStart = indextmp ; } buf . append ( script . substring ( 0 , indexStart + 1 ) ) ; int speech = script . indexOf ( '\"' , indexs ) ; speech = script . indexOf ( '\"' , speech + 1 ) ; while ( script . charAt ( speech - 1 ) == '\\' ) { speech = script . indexOf ( '\"' , speech ) ; } final int startArgs = script . indexOf ( ',' , speech ) ; final int endArgs = script . indexOf ( ')' , startArgs ) ; final String arguments = script . substring ( startArgs + 1 , endArgs ) ; if ( arguments . equals ( "printfArgs" ) ) { break printf ; } final StringTokenizer tok = new StringTokenizer ( arguments , ", " ) ; buf . append ( "var printfArgs=new Array();\n" ) ; int i = 0 ; while ( tok . hasMoreTokens ( ) ) { buf . append ( "printfArgs[" ) ; buf . append ( i ++ ) ; buf . append ( "]=" ) ; buf . append ( tok . nextToken ( ) ) ; buf . append ( ";\n" ) ; } buf . append ( script . substring ( indexStart + 1 , startArgs + 1 ) ) ; buf . append ( "printfArgs" ) ; buf . append ( script . substring ( endArgs ) ) ; script = buf . toString ( ) ; } script = checkAndReplaceCode ( "event.value=AFMakeNumber(acro.getField(\"sum\").value)(8)" , "" , script ) ; script = checkAndReplaceCode ( "calculate = false" , "calculate = 0" , script ) ; script = checkAndReplaceCode ( "calculate = true" , "calculate = 1" , script ) ; script = checkAndReplaceCode ( "calculate=false" , "calculate=0" , script ) ; script = checkAndReplaceCode ( "calculate=true" , "calculate=1" , script ) ; return script ; }
replace javascript variables with our own so rhino can easily identify them and pass excution over to us
