Re: EscapeStrategy for in-text html tags not working
SoTaNeZ <[email protected]> Thu, 10 Jan 2013 09:22:35 +0100
| Newsgroups | gmane.comp.java.jdom.general |
|---|---|
| Message-ID | <CAJypjWcqOQ6v0Cm2_Pnr4E8gzdzP3h1J=vbPkQurFVYLngTPrg@mail.gmail.com> |
--===============2085667857== Content-Type: multipart/alternative; boundary=047d7b339ceb8df77604d2eae1a1 --047d7b339ceb8df77604d2eae1a1 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Thank you both of you for your answers. 2013/1/9 Brenner, Mike <[email protected]> > Hi Rolf,**** > > ** ** > > Although you are technically right that we should always generate sociall= y > acceptable xml, I would like to add some caveats to your response.**** > > ** ** > > First, not all html can be translated to xhtml (for example, the html > attribute =93target=94 to generate a popup).**** > > ** ** > > Second, there are many workarounds needed (for example,   and other > symbols don=92t exist in xhtml due to unfortunate choices of version numb= ers > of included documents in the standards).**** > > ** ** > > Third, you don=92t always have control over how the process using your xm= l > will parse it, if at all.**** > > ** ** > > Fourth, xml is often the lowest or second lowest protocol in a large > number of protocols (for example, a URI inside a javascript string inside= a > javascript block inside an xhtml, etc.)**** > > ** ** > > The key to making xml useful is being flexible enough to work around the > bugs in all of these well-established tools (especially xml itself with i= ts > needless vestiges of sgml and its inability to contain unparsed tags or u= se > xml fragments). Therefore, sometimes it make sense to ask if an xml tool > like jdom could provide some of those required workarounds.**** > > ** ** > > The alternative in cases like these might not suffice to demand more > goodness in our output xml; the alternative might turn out to be to use > json or yaml or some other alternative to xml itself, when the tools are > not flexible enough.**** > > ** ** > > Mike Brenner**** > > ** ** > > *From:* [email protected] [mailto: > [email protected]] *On Behalf Of *Rolf Lear > *Sent:* Wednesday, January 09, 2013 7:49 AM > *To:* SoTaNeZ > *Cc:* [email protected] > *Subject:* Re: [jdom-interest] EscapeStrategy for in-text html tags not > working**** > > ** ** > > Hi there. > > Escaping "<" and other the other characters '>', '&', and '\r' is always > done (with one exception...). The '\n' char is also sometimes replaced wi= th > \r\n. > > if (ch =3D=3D '<' || ch =3D=3D '>' || ch =3D=3D '&' || ch =3D= =3D '\r' || ch =3D=3D > '\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 th= e > 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 =3D new Element("tag"); > emt.addContent(new > ProcessingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPI= NG)); > emt.addContent(new Text("<html><body><p></body></html>")); > emt.addContent(new > ProcessingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPIN= G)); > > 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 "<" and ">" preventing its correct posteri= or > 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 ar= e > converted before the EscapeStrategy gets into action, or maybe I am doing > something wrong.**** > > ** ** > > This is the code to set the format:**** > > ** ** > > ---------------------**** > > Format formato =3D Format.getPrettyFormat();**** > > formato.setTextMode(TextMode.PRESERVE);**** > > formato.setEscapeStrategy(new EscapeSimbolos());**** > > formato.setIgnoreTrAXEscapingPIs(true);**** > > XMLOutputter outputter =3D 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]**= ** > > ** ** > --047d7b339ceb8df77604d2eae1a1 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Thank you both of you for your answers.<br><br><div class=3D"gmail_quote">2= 013/1/9 Brenner, Mike <span dir=3D"ltr"><<a href=3D"mailto:[email protected]= rg" target=3D"_blank">[email protected]</a>></span><br><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"> <div bgcolor=3D"white" lang=3D"EN-US" link=3D"blue" vlink=3D"purple"> <div> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">Hi Rolf,<u></u><u></u></s= pan></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">Although you are technica= lly right that we should always generate socially acceptable xml, I would l= ike to add some caveats to your response.<u></u><u></u></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">First, not all html can b= e translated to xhtml (for example, the html attribute =93target=94 to gene= rate a popup).<u></u><u></u></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">Second, there are many wo= rkarounds needed (for example, &nbsp and other symbols don=92t exist in= xhtml due to unfortunate choices of version numbers of included documents in the standards).<u></u><u></u></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">Third, you don=92t always= have control over how the process using your xml will parse it, if at all.= <u></u><u></u></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">Fourth, xml is often the = lowest or second lowest protocol in a large number of protocols (for exampl= e, a URI inside a javascript string inside a javascript block inside an xhtml, etc.)<u></u><u></u></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">The key to making xml use= ful is being flexible enough to work around the bugs in all of these well-e= stablished tools (especially xml itself with its needless vestiges of sgml and its inability to contain unparsed tags or use xml fra= gments). Therefore, sometimes it make sense to ask if an xml tool like jdom= could provide some of those required workarounds.<u></u><u></u></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">The alternative in cases = like these might not suffice to demand more goodness in our output xml; the= alternative might turn out to be to use json or yaml or some other alternative to xml itself, when the tools are not flexible enou= gh.<u></u><u></u></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d"><u></u>=A0<u></u></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1f497d">Mike Brenner<u></u><u></u= ></span></p> <p class=3D"MsoNormal"><a name=3D"13c1f93255ab7cbb__MailEndCompose"><span s= tyle=3D"font-size:11.0pt;font-family:"Calibri","sans-serif&q= uot;;color:#1f497d"><u></u>=A0<u></u></span></a></p> <div> <div style=3D"border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in = 0in 0in"> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><b><span style=3D"font-si= ze:10.0pt;font-family:"Tahoma","sans-serif";color:windo= wtext">From:</span></b><span style=3D"font-size:10.0pt;font-family:"Ta= homa","sans-serif";color:windowtext"> <a href=3D"mailto:jdom= [email protected]" target=3D"_blank">[email protected]= g</a> [mailto:<a href=3D"mailto:[email protected]" target=3D"_blank= ">[email protected]</a>] <b>On Behalf Of </b>Rolf Lear<br> <b>Sent:</b> Wednesday, January 09, 2013 7:49 AM<br> <b>To:</b> SoTaNeZ<br> <b>Cc:</b> <a href=3D"mailto:[email protected]" target=3D"_blank">jdom= [email protected]</a><br> <b>Subject:</b> Re: [jdom-interest] EscapeStrategy for in-text html tags no= t working<u></u><u></u></span></p> </div> </div><div><div class=3D"h5"> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">Hi there.<br> <br> Escaping "<" and other the other characters '>', &#= 39;&', and '\r' is always done (with one exception...). The= '\n' char is also sometimes replaced with \r\n.<br> <br> =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if (ch =3D=3D '<' || ch =3D=3D = 9;>' || ch =3D=3D '&' || ch =3D=3D '\r' || ch = =3D=3D '\n'<br> =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 || strategy.shouldEscape(= ch)) {<br> =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 ......<br> =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }<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 pro= duce broken XML. Really the best way to solve your problem is to do the rig= ht handling of your output. The code using your XML should parse the data f= rom your element and the parsing process will un-escape the characters. The next best way to do things is t= o 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 comp= lete JDOM document normally.<br> <br> The wrong way to do it wold be to override/extend the AbstractXMLOutputProc= essor and to 'hack' the code that does the escaping.<br> <br> TrAXEscapePI concept is designed to support obscure functionality in the XM= LTransformation process (XSLT). Technically I think you *can* use this conc= ept to support not-escaping by having the following JDOM content:<br> <br> =A0=A0=A0 Element emt =3D new Element("tag");<br> =A0=A0=A0 emt.addContent(new ProcessingInstruction(javax.xml.transform.Resu= lt.PI_DISABLE_OUTPUT_ESCAPING));<br> =A0=A0=A0 emt.addContent(new Text("<html><body><p>&l= t;/body></html>"));<br> =A0=A0=A0 emt.addContent(new ProcessingInstruction(javax.xml.transform.Resu= lt.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:<u></u><u></u></p> </div> <blockquote style=3D"margin-top:5.0pt;margin-bottom:5.0pt"> <p class=3D"MsoNormal" style=3D"margin-left:.5in">Hello. <u></u><u></u></p> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">I have some XML elements = which text is not plain, but contains some HTML tags.<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">The thing is that when ou= tputting these to an XML file the characters "<" and ">= ;" are changed into "&lt;" and "&gt;" prev= enting its correct posterior HTML processing.<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">I tried using this Escape= Strategy:<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">------------------<u></u>= <u></u></p> </div> <div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>class Escape= Simbolos implements EscapeStrategy {<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>public boole= an shouldEscape(char car) {<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>switch (car)= {<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>case '&l= t;':<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>return false= ;<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>case '&g= t;':<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>return false= ;<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>default:<u><= /u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>return false= ;<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>}<u></u><u><= /u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>}<u></u><u><= /u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>}<u></u><u><= /u></p> </div> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">------------------<u></u>= <u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">but with no effect. The c= ode within the two case statements is never reached, but the method is exec= uted, so it seems that these characters are converted before the EscapeStra= tegy gets into action, or maybe I am doing something wrong.<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">This is the code to set t= he format:<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">---------------------<u><= /u><u></u></p> </div> <div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>Format forma= to =3D Format.getPrettyFormat();<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>formato.setT= extMode(TextMode.PRESERVE);<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>formato.setE= scapeStrategy(new EscapeSimbolos());<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>formato.setI= gnoreTrAXEscapingPIs(true);<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span></span>XMLOutputter= outputter =3D new XMLOutputter(formato);<u></u><u></u></p> </div> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">---------------------<u><= /u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">I tried setting=A0setIgno= reTrAXEscapingPIs to false, because I am not sure what this does exactly, b= ut nothing seems to change.<u></u><u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">Any ideas?<u></u><u></u><= /p> </div> </div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><br> <br> <br> <u></u><u></u></p> <pre style=3D"margin-left:.5in">___________________________________________= ____<u></u><u></u></pre> <pre style=3D"margin-left:.5in">To control your jdom-interest membership:<u= ></u><u></u></pre> <pre style=3D"margin-left:.5in"><a href=3D"http://www.jdom.org/mailman/opti= ons/jdom-interest/[email protected]" target=3D"_blank">http://www.jdom.= org/mailman/options/jdom-interest/[email protected]</a><u></u><u></u></= pre> </blockquote> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><u></u>=A0<u></u></p> </div></div></div> </div> </blockquote></div><br> --047d7b339ceb8df77604d2eae1a1-- --===============2085667857== 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] --===============2085667857==--