Topic
  • 9 replies
  • Latest Post - ‏2013-05-10T05:10:27Z by Adamarla
SystemAdmin
SystemAdmin
3180 Posts

Pinned topic Recover special characters ('\n', '\t', etc) from Field.

‏2013-04-04T12:12:25Z |
Hey all, I am having some troubles being able to retain special characters when I use a field DBE. I want to be able to input things like newlines, tabs, etc. when appending attribute values but all I'm getting as an output is just the text itself (so "\n6512" instead of a newline+"6512"). I know I could search for them (each individually) using regex and replace all them manually, but is there an easier way?? Thanks!

- nm3210
Updated on 2013-04-05T15:41:58Z at 2013-04-05T15:41:58Z by SystemAdmin
  • Tony_Goodman
    Tony_Goodman
    270 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-04-04T12:58:42Z  
    Maybe you should use a richText DBE instead?

    Tony Goodman, www.smartdxl.com
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-04-04T13:14:05Z  
    Maybe you should use a richText DBE instead?

    Tony Goodman, www.smartdxl.com
    Well then I'm left with all the rich text junk: "{\rtf1\fbidis\ansi\ansicpg936\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\ltrpar\lang1033\f0\fs20 TEST\par}"

    I figured that if there was an easy way to do this you (or another of the experienced guys here) would probably know off the top of your head, so maybe it's just not easily doable? Like a lot of things, it's not completely necessary and you can skirt your way around it. Thanks for the response Tony! I definitely got a lot of help from your website when I first started coding DXL.
  • Mathias Mamsch
    Mathias Mamsch
    1969 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-04-04T17:46:02Z  
    Well then I'm left with all the rich text junk: "{\rtf1\fbidis\ansi\ansicpg936\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\ltrpar\lang1033\f0\fs20 TEST\par}"

    I figured that if there was an easy way to do this you (or another of the experienced guys here) would probably know off the top of your head, so maybe it's just not easily doable? Like a lot of things, it's not completely necessary and you can skirt your way around it. Thanks for the response Tony! I definitely got a lot of help from your website when I first started coding DXL.

    Well maybe there is a misunderstanding. First of all, the field DBE can only contain a one line string, so newline characters, etc. are simply not rendered there, but the text appears to be in one line. There is a text DBE and a richText DBE which both support multi line strings the latter one richText formatted.

    What I do not understand is that you write, that in your field you see "\n1234" instead of "newline 1234". What exactly is it, you are setting in your field? Can you post a little example to make more obvious what the problem is, e.g.
     

    DB x = centered "Test" 
     
    DBE y = text(x, "", "", 80, 100, false) 
    realize x
     
    set (y, "abc\ndef")
     
    show x
    

     


    Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Updated on 2014-01-06T10:09:10Z at 2014-01-06T10:09:10Z by iron-man
  • llandale
    llandale
    2972 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-04-04T18:03:18Z  
    Well then I'm left with all the rich text junk: "{\rtf1\fbidis\ansi\ansicpg936\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\ltrpar\lang1033\f0\fs20 TEST\par}"

    I figured that if there was an easy way to do this you (or another of the experienced guys here) would probably know off the top of your head, so maybe it's just not easily doable? Like a lot of things, it's not completely necessary and you can skirt your way around it. Thanks for the response Tony! I definitely got a lot of help from your website when I first started coding DXL.

    Don't know so I came up with the following. Enter some text into the Rich Text box, bold some, then hit Check. It appears you need to manually prefix the string with "{rtf1 " and suffix it with "}". The "richText(text)" command didn't work. Very odd, clearly I'm missing something.

    DB db
    DBE dbeT, dbeR, dbeX
     
    string colourstring = 
    "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharse
    t0 Arial;}} 
    {\\colortbl 
    ;\\red255\\green0\\blue0;\\red255\\green0\\blue255;\\red0\\green0\\blue128;} 
    \\viewkind4\\uc1\\pard\\f0\\fs20 Some\\cf1\\b  text \\b0\\cf0 with \\cf2 different\\cf0  
    \\cf3 colors\\cf0  in it.\\par 
    \\par 
    }"      // string from DXL manual bottom of "Common element operations"
     
    void applyCheck(DB dbXX)
    {
       string Text = get(dbeR)
       string Set = ("{\\rtf1 == Raw >>" (get(dbeT))"<<\n== Rich >>" Text "<<}")
       //Set = richText("== Raw >>" (get(dbeT))"<<\n== Rich >>" Text "<<")
       print Set
       set(dbeX, Set)
    }
     
    db = create("Compare RawText with RichText DBEs")
    dbeT = text    (db, "Raw Text", "", 300, 100, false)
    dbeR = richText(db, "RichText", richText(""), 300, 100, false)
    dbeX = richText(db, "Result", "", 300, 150, true)
     
    apply(db, "Check", applyCheck)
    realize(db)
    set(dbeX, colourstring)
     
    show(db)
    


    -Louie

    Updated on 2014-01-06T10:08:46Z at 2014-01-06T10:08:46Z by iron-man
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-04-05T15:41:58Z  

    Well maybe there is a misunderstanding. First of all, the field DBE can only contain a one line string, so newline characters, etc. are simply not rendered there, but the text appears to be in one line. There is a text DBE and a richText DBE which both support multi line strings the latter one richText formatted.

    What I do not understand is that you write, that in your field you see "\n1234" instead of "newline 1234". What exactly is it, you are setting in your field? Can you post a little example to make more obvious what the problem is, e.g.
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">DB x = centered "Test" DBE y = text(x, "", "", 80, 100, false) realize x set (y, "abc\ndef") show x </pre>

     


    Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    > Mathias Mamsch wrote:
    > Well maybe there is a misunderstanding. First of all, the field DBE can only contain a one line string, so newline characters, etc. are simply not rendered there, but the text appears to be in one line. There is a text DBE and a richText DBE which both support multi line strings the latter one richText formatted.
    >
    > What I do not understand is that you write, that in your field you see "\n1234" instead of "newline 1234". What exactly is it, you are setting in your field? Can you post a little example to make more obvious what the problem is, e.g.
    >
    > Regards, Mathias

    I'm not trying to get the newline to be show up in the input text field, that would be silly. I want to input general text (which would include some of the ASCII ctrl characters Char<31 including '\n', '\t', etc) and place that text into an object's attribute with a Text/String basetype (which WOULD show you the newline, tab, etc).
     

    DB DialogBox = create("Replace Object Text",styleCentered|styleFloating)
    DBE TextField = field(DialogBox,"New text:","Testing newline:\\n\\tNope.",30)
    void toApply(DB db){(current Object)."Object Text" = get(TextField);refresh(current Module)}
    void toNewLine(DB db){(current Object)."Object Text" = "Testing newline:\n\tYes.";refresh(current Module)}
    apply(DialogBox,"New Line",toNewLine)
    apply(DialogBox,"Apply Text",toApply)
    show DialogBox
    

     


    I guess the question really becomes how to un-escape text so that you may retain/recover/reinterpret special characters. Once placed, the text that includes the literal "\n" is indeed just Char92+Char110 whereas the interpreted text replaced that with a Char10. Again, I understand how to replace something like that with regex pretty trivially, but I would rather not have to run a different regex/replace for all the ASCII control characters (realistically you might only need to use a few, but that's not really my point).

     

    Updated on 2014-01-06T10:08:19Z at 2014-01-06T10:08:19Z by iron-man
  • Adamarla
    Adamarla
    75 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-05-08T07:26:10Z  

    > Mathias Mamsch wrote:
    > Well maybe there is a misunderstanding. First of all, the field DBE can only contain a one line string, so newline characters, etc. are simply not rendered there, but the text appears to be in one line. There is a text DBE and a richText DBE which both support multi line strings the latter one richText formatted.
    >
    > What I do not understand is that you write, that in your field you see "\n1234" instead of "newline 1234". What exactly is it, you are setting in your field? Can you post a little example to make more obvious what the problem is, e.g.
    >
    > Regards, Mathias

    I'm not trying to get the newline to be show up in the input text field, that would be silly. I want to input general text (which would include some of the ASCII ctrl characters Char<31 including '\n', '\t', etc) and place that text into an object's attribute with a Text/String basetype (which WOULD show you the newline, tab, etc).
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">DB DialogBox = create("Replace Object Text",styleCentered|styleFloating) DBE TextField = field(DialogBox,"New text:","Testing newline:\\n\\tNope.",30) void toApply(DB db){(current Object)."Object Text" = get(TextField);refresh(current Module)} void toNewLine(DB db){(current Object)."Object Text" = "Testing newline:\n\tYes.";refresh(current Module)} apply(DialogBox,"New Line",toNewLine) apply(DialogBox,"Apply Text",toApply) show DialogBox </pre>

     


    I guess the question really becomes how to un-escape text so that you may retain/recover/reinterpret special characters. Once placed, the text that includes the literal "\n" is indeed just Char92+Char110 whereas the interpreted text replaced that with a Char10. Again, I understand how to replace something like that with regex pretty trivially, but I would rather not have to run a different regex/replace for all the ASCII control characters (realistically you might only need to use a few, but that's not really my point).

     

    So, here you go:

     

    string Unescape(string sEscaped)
    {
    if(!null(checkDXL("\"" sEscaped "\""))) { return("") }
    return(eval_("return_(\"" sEscaped "\")"))
    }

    DB DialogBox = create("Replace Object Text",styleCentered|styleFloating)
    DBE TextField = field(DialogBox,"New text:","Testing newline:\\n\\tNope.",30)

    void toPrint(DB db)
    {
    string sEscaped = get(TextField);
    print("sEscaped: " sEscaped "\n")
    string sUnescaped = Unescape(sEscaped)
    if(!null(sEscaped) and null(sUnescaped)) {
      print("ERROR: Incorrectly escaped string\n")
    } else {
      print("sUnescaped: " sUnescaped "\n")
    }
    }

    void toApply(DB db)
    {
    string sEscaped = get(TextField);
    string sUnescaped = Unescape(sEscaped)
    if(!null(sEscaped) and null(sUnescaped)) {
      return()
    }

     (current Object)."Object Text" = sUnescaped;
    refresh(current Module)
    }

    void toNewLine(DB db)
    {
    (current Object)."Object Text" = "Testing newline:\n\tYes.";
    refresh(current Module)
    }
    apply(DialogBox,"New Line", toNewLine)
    apply(DialogBox,"Apply Text", toApply)
    apply(DialogBox,"Print", toPrint)
    show DialogBox

    Updated on 2013-05-10T07:23:23Z at 2013-05-10T07:23:23Z by Adamarla
  • nm3210
    nm3210
    7 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-05-09T12:10:24Z  
    • Adamarla
    • ‏2013-05-08T07:26:10Z

    So, here you go:

     

    string Unescape(string sEscaped)
    {
    if(!null(checkDXL("\"" sEscaped "\""))) { return("") }
    return(eval_("return_(\"" sEscaped "\")"))
    }

    DB DialogBox = create("Replace Object Text",styleCentered|styleFloating)
    DBE TextField = field(DialogBox,"New text:","Testing newline:\\n\\tNope.",30)

    void toPrint(DB db)
    {
    string sEscaped = get(TextField);
    print("sEscaped: " sEscaped "\n")
    string sUnescaped = Unescape(sEscaped)
    if(!null(sEscaped) and null(sUnescaped)) {
      print("ERROR: Incorrectly escaped string\n")
    } else {
      print("sUnescaped: " sUnescaped "\n")
    }
    }

    void toApply(DB db)
    {
    string sEscaped = get(TextField);
    string sUnescaped = Unescape(sEscaped)
    if(!null(sEscaped) and null(sUnescaped)) {
      return()
    }

     (current Object)."Object Text" = sUnescaped;
    refresh(current Module)
    }

    void toNewLine(DB db)
    {
    (current Object)."Object Text" = "Testing newline:\n\tYes.";
    refresh(current Module)
    }
    apply(DialogBox,"New Line", toNewLine)
    apply(DialogBox,"Apply Text", toApply)
    apply(DialogBox,"Print", toPrint)
    show DialogBox

    Awesome!! (These were my posts) Major props to you for figuring this out. I assume it just re-parses the string through eval_ so you get the re-interpreted characters? Seems straightforward enough, I definitely did not think of using eval though!

    Updated on 2013-05-09T12:11:30Z at 2013-05-09T12:11:30Z by nm3210
  • llandale
    llandale
    2972 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-05-09T15:21:32Z  
    Maybe you should use a richText DBE instead?

    Tony Goodman, www.smartdxl.com

    ...or just a "text" DBE.

  • Adamarla
    Adamarla
    75 Posts

    Re: Recover special characters ('\n', '\t', etc) from Field.

    ‏2013-05-10T05:10:27Z  
    • nm3210
    • ‏2013-05-09T12:10:24Z

    Awesome!! (These were my posts) Major props to you for figuring this out. I assume it just re-parses the string through eval_ so you get the re-interpreted characters? Seems straightforward enough, I definitely did not think of using eval though!

    "I assume it just re-parses the string through eval_ so you get the re-interpreted characters?"

    Exactly. You are entering a string which is escaped the same way as writing a string literal in dxl code. I just surround it with quotes and use eval_ to get dxl to do its thing and unescape it.

    Updated on 2013-05-10T07:04:44Z at 2013-05-10T07:04:44Z by Adamarla