Search code examples
oracle-databaseoracle-data-integrator

ODI: KM Java BeanShell - escape double quotes


I want to set a variable inside Knowledge Module's Task, with target technology set to Java BeanShell. The value represents mapping EXPRESSIONs, where source table is inside MSSQL database. Column names are surrounded by double quotes, that causes a problem with templating.

Column expression is:

source_tab."Entry Number"

Task (Java BeanShell)

<$
   String SEL_COLS = "<%=odiRef.getColList(0, "", "[EXPRESSION]\t[ALIAS_SEP] [CX_COL_NAME]", ",\n\t", "", "")%>";
$>

This variable assignment fails, because " in source_tab."Entry Number" is not escaped - code does not compile.

odiRef.getQuotedString does not solve the problem...


Solution

  • odiRef.getQuotedString could help if generated code is executed as a final code in JBS technology. When we use it in the following way (in ?-, $- or @-substitution):

    <$
    String SEL_COLS = <%=odiRef.getQuotedString(odiRef.getColList(0, "", "[EXPRESSION]\t[ALIAS_SEP] [CX_COL_NAME]", ",\n\t", "", ""))%>;
    $>
    

    then result fails like this:

    ... Caused by: org.apache.bsf.BSFException: BeanShell script error: 
    Parse error at line 3, column 37.  Encountered: Entry BSF info: ....
    ... 11 more
    
    Text: <$
       String SEL_COLS = "SOURCE_TAB.\"Entry Number\"      ENTRY_NUMBER";
    $>.
    

    This looks good but does not work. It could work as final code (I mean result of all substitutions) in JBS Technology. Unfortunately any substitutions eats backslashes.

    Ok, if standard odiRef-functtion does not work, lets write our own:

    <%
    String getQuotedStringCustomized(String s){
       return '"'+s.replaceAll('"'.toString(),'"'+"+'"+'"'+"'+"+'"')+'"';
    }
    %>
    -- other code........
    <$
    String SEL_COLS = <%=getQuotedStringCustomized(odiRef.getColList(0, "", "[EXPRESSION]\t[ALIAS_SEP] [CX_COL_NAME]", ",\n\t", "", ""))%>;
    $>
    

    Only the way to put " into a Java literal within the JBS Substitution is contatenation with Char literal '"' or using '"'.toString() expression if it is impossible to use Char type.

    FINALLY:

    In final JBS code you may use \", but within substitutions only +'"'+.