Re: EscapeStrategy for in-text html tags not working

Rolf Lear <[email protected]> Wed, 09 Jan 2013 07:49:22 -0500
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============0973089252==
Content-Type: multipart/alternative;
 boundary="------------010202000901020102060202"

This is a multi-part message in MIME format.
--------------010202000901020102060202
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi there.

Escaping "<" and other the other characters '>', '&', and '\r' is always 
done (with one exception...). The '\n' char is also sometimes replaced 
with \r\n.

             if (ch == '<' || ch == '>' || ch == '&' || ch == '\r' || ch 
== '\n'
                     || strategy.shouldEscape(ch)) {
                     ......
             }

To do anything else would be to produce broken XML.

What you want is (likelye to be) broken XML and JDOM is not designed to 
produce broken XML. Really the best way to solve your problem is to do 
the right handling of your output. The code using your XML should parse 
the data from your element and the parsing process will un-escape the 
characters. The next best way to do things is to make the actual HTML 
content valid XHTML and to parse it and then add the Element content as 
JDOM objects to the JDOM tree, and then output the complete JDOM 
document normally.

The wrong way to do it wold be to override/extend the 
AbstractXMLOutputProcessor and to 'hack' the code that does the escaping.

TrAXEscapePI concept is designed to support obscure functionality in the 
XMLTransformation process (XSLT). Technically I think you *can* use this 
concept to support not-escaping by having the following JDOM content:

     Element emt = new Element("tag");
     emt.addContent(new 
ProcessingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING));
     emt.addContent(new Text("<html><body><p></body></html>"));
     emt.addContent(new 
ProcessingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING));

Then you will need to set Format.setIgnoreTrAXExcapingPIs to false when 
you output the JDOM Content.

The above is a *hack* and is not what I recommend that you do.

Rolf



On 09/01/2013 3:50 AM, SoTaNeZ wrote:
> Hello.
>
> I have some XML elements which text is not plain, but contains some 
> HTML tags.
> The thing is that when outputting these to an XML file the characters 
> "<" and ">" are changed into "&lt;" and "&gt;" preventing its correct 
> posterior HTML processing.
> I tried using this EscapeStrategy:
>
> ------------------
> class EscapeSimbolos implements EscapeStrategy {
>
> public boolean shouldEscape(char car) {
> switch (car) {
> case '<':
> return false;
> case '>':
> return false;
> default:
> return false;
> }
> }
> }
> ------------------
>
> but with no effect. The code within the two case statements is never 
> reached, but the method is executed, so it seems that these characters 
> are converted before the EscapeStrategy gets into action, or maybe I 
> am doing something wrong.
>
> This is the code to set the format:
>
> ---------------------
> Format formato = Format.getPrettyFormat();
> formato.setTextMode(TextMode.PRESERVE);
> formato.setEscapeStrategy(new EscapeSimbolos());
> formato.setIgnoreTrAXEscapingPIs(true);
> XMLOutputter outputter = new XMLOutputter(formato);
> ---------------------
>
> I tried setting setIgnoreTrAXEscapingPIs to false, because I am not 
> sure what this does exactly, but nothing seems to change.
>
> Any ideas?
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/[email protected]


--------------010202000901020102060202
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Hi there.<br>
      <br>
      Escaping "&lt;" and other the other characters '&gt;', '&amp;',
      and '\r' is always done (with one exception...). The '\n' char is
      also sometimes replaced with \r\n.<br>
      <br>
      &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (ch == '&lt;' || ch == '&gt;' || ch == '&amp;' ||
      ch == '\r' || ch == '\n'<br>
      &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; || strategy.shouldEscape(ch)) {<br>
      &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ......<br>
      &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>
      <br>
      To do anything else would be to produce broken XML.<br>
      <br>
      What you want is (likelye to be) broken XML and JDOM is not
      designed to produce broken XML. Really the best way to solve your
      problem is to do the right handling of your output. The code using
      your XML should parse the data from your element and the parsing
      process will un-escape the characters. The next best way to do
      things is to make the actual HTML content valid XHTML and to parse
      it and then add the Element content as JDOM objects to the JDOM
      tree, and then output the complete JDOM document normally.<br>
      <br>
      The wrong way to do it wold be to override/extend the
      AbstractXMLOutputProcessor and to 'hack' the code that does the
      escaping.<br>
      <br>
      TrAXEscapePI concept is designed to support obscure functionality
      in the XMLTransformation process (XSLT). Technically I think you
      *can* use this concept to support not-escaping by having the
      following JDOM content:<br>
      <br>
      &nbsp;&nbsp;&nbsp; Element emt = new Element("tag");<br>
      &nbsp;&nbsp;&nbsp; emt.addContent(new
ProcessingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING));<br>
      &nbsp;&nbsp;&nbsp; emt.addContent(new
      Text("&lt;html&gt;&lt;body&gt;&lt;p&gt;&lt;/body&gt;&lt;/html&gt;"));<br>
      &nbsp;&nbsp;&nbsp; emt.addContent(new
ProcessingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING));<br>
      <br>
      Then you will need to set Format.setIgnoreTrAXExcapingPIs to false
      when you output the JDOM Content.<br>
      <br>
      The above is a *hack* and is not what I recommend that you do.<br>
      <br>
      Rolf<br>
      <br>
      <br>
      <br>
      On 09/01/2013 3:50 AM, SoTaNeZ wrote:<br>
    </div>
    <blockquote
cite="mid:CAJypjWf_ROGQTcLoi1_HHUMCJaY9tKWdb4NgdC24-VoJLo48-Q@mail.gmail.com"
      type="cite">Hello.
      <div><br>
        <div>I have some XML elements which text is not plain, but
          contains some HTML tags.</div>
        <div>The thing is that when outputting these to an XML file the
          characters "&lt;" and "&gt;" are changed into "&amp;lt;" and
          "&amp;gt;" preventing its correct posterior HTML processing.</div>
        <div>I tried using this EscapeStrategy:</div>
        <div><br>
        </div>
        <div>------------------</div>
        <div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>class
            EscapeSimbolos implements EscapeStrategy {</div>
          <div><br>
          </div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>public
            boolean shouldEscape(char car) {</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>switch
            (car) {</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>case
            '&lt;':</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>return
            false;</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>case
            '&gt;':</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>return
            false;</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>default:</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>return
            false;</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div>
        </div>
        <div>------------------</div>
        <div><br>
        </div>
        <div>but with no effect. The code within the two case statements
          is never reached, but the method is executed, so it seems that
          these characters are converted before the EscapeStrategy gets
          into action, or maybe I am doing something wrong.</div>
        <div><br>
        </div>
        <div>This is the code to set the format:</div>
        <div><br>
        </div>
        <div>---------------------</div>
        <div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>Format
            formato = Format.getPrettyFormat();</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>formato.setTextMode(TextMode.PRESERVE);</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>formato.setEscapeStrategy(new
            EscapeSimbolos());</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>formato.setIgnoreTrAXEscapingPIs(true);</div>
          <div><span class="Apple-tab-span" style="white-space:pre"> </span>XMLOutputter
            outputter = new XMLOutputter(formato);</div>
        </div>
        <div>---------------------</div>
        <div><br>
        </div>
        <div>I tried setting&nbsp;setIgnoreTrAXEscapingPIs to false, because
          I am not sure what this does exactly, but nothing seems to
          change.</div>
        <div><br>
        </div>
        <div>Any ideas?</div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
To control your jdom-interest membership:
<a class="moz-txt-link-freetext" href="http://www.jdom.org/mailman/options/jdom-interest/[email protected]">http://www.jdom.org/mailman/options/jdom-interest/[email protected]</a></pre>
    </blockquote>
    <br>
  </body>
</html>

--------------010202000901020102060202--

--===============0973089252==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
--===============0973089252==--