COBOL code to replace characters by html entities

瘦欲@ 提交于 2020-01-06 06:52:17

问题


I want to replace the characters '<' and '>' by &lt; and &gt; with COBOL. I was wondering about INSPECT statement, but it looks like this statement just can be used to translate one char by another. My intention is to replace all html characters by their html entities.

Can anyone figure out some way to do it? Maybe looping over the string and testing each char is the only way?

GnuCOBOL or IBM COBOL examples are welcome.

My best code is something like it: (http://ideone.com/MKiAc6)

IDENTIFICATION DIVISION.
PROGRAM-ID. HTMLSECURE.

ENVIRONMENT DIVISION.

DATA DIVISION.
WORKING-STORAGE SECTION.
77 INPTXT PIC X(50).
77 OUTTXT PIC X(500).
77 I PIC 9(4) COMP VALUE 1.
77 P PIC 9(4) COMP VALUE 1.

PROCEDURE DIVISION.
    MOVE 1 TO P
    MOVE '<SCRIPT> TEST TEST </SCRIPT>' TO INPTXT

    PERFORM VARYING I FROM 1 BY 1
            UNTIL I EQUAL LENGTH OF INPTXT

        EVALUATE INPTXT(I:1)
            WHEN '<'
                MOVE "&lt;" TO OUTTXT(P:4)
                ADD 4 TO P
            WHEN '>'
                MOVE "&gt;" TO OUTTXT(P:4)
                ADD 4 TO P
            WHEN OTHER
                MOVE INPTXT(I:1) TO OUTTXT(P:1)
                ADD 1 TO P
        END-EVALUATE
    END-PERFORM

    DISPLAY OUTTXT

    STOP RUN
    .

回答1:


COBOL is a language of fixed-length fields. So no, INSPECT is not going to be able to do what you want.

If you need this for an IBM Mainframe, your SORT product (assuming sufficiently up-to-date) can do this using FINDREP.

If you look at the XML processing possibilities in Enterprise COBOL, you will see that they do exactly what you want (I'd guess). GnuCOBOL can also readily interface with lots of other things. If you are writing GnuCOBOL for running on a non-Mainframe, I'd suggest you ask on the GnuCOBOL part of SourceForge.

Otherwise, yes, it would come down to looping through the data. Once you clarify what you want a bit more, you may get examples of that if you still need them.




回答2:


GnuCOBOL (yes, another name branding change) has an intrinsic function extension, FUNCTION SUBSTITUTE.

move function substitute(inptxt, ">", "&gt;", "<", "&lt;") to where-ever-including-inptxt

Takes a subject string, and pairs of patterns and replacements. (This is not regex patterns, straight up text matching). See http://opencobol.add1tocobol.com/gnucobol/#function-substitute for some more details. The patterns and replacements can all be different lengths.

As intrinsic functions return anonymous COBOL fields, the result of the function can be used to overwrite the subject field, without worry of sliding overlap or other "change while reading" problems.



来源:https://stackoverflow.com/questions/26089663/cobol-code-to-replace-characters-by-html-entities

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!