Re: EscapeStrategy for in-text html tags not working
"Brenner, Mike" <[email protected]> Wed, 9 Jan 2013 13:50:01 +0000
| Newsgroups | gmane.comp.java.jdom.general |
|---|---|
| Message-ID | <[email protected]> |
--===============0259014688== Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_264449A6A521A14593F58479F46B34EB014E7BF5IMCMBX03MITREOR_" --_000_264449A6A521A14593F58479F46B34EB014E7BF5IMCMBX03MITREOR_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Rolf, Although you are technically right that we should always generate socially = 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 attri= bute "target" to generate a popup). Second, there are many workarounds needed (for example,   and other sym= bols don't exist in xhtml due to unfortunate choices of version numbers of = included documents in the standards). Third, you don't always have control over how the process using your xml wi= ll 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 javas= cript block inside an xhtml, etc.) The key to making xml useful is being flexible enough to work around the bu= gs in all of these well-established tools (especially xml itself with its n= eedless vestiges of sgml and its inability to contain unparsed tags or use = xml fragments). Therefore, sometimes it make sense to ask if an xml tool li= ke jdom could provide some of those required workarounds. The alternative in cases like these might not suffice to demand more goodne= ss in our output xml; the alternative might turn out to be to use json or y= aml or some other alternative to xml itself, when the tools are not flexibl= e 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 worki= ng Hi there. Escaping "<" and other the other characters '>', '&', and '\r' is always do= ne (with one exception...). The '\n' char is also sometimes replaced with \= 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 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 to make the actual HTML content valid XHTML = and to parse it and then add the Element content as JDOM objects to the JDO= M tree, and then output the complete JDOM document normally. The wrong way to do it wold be to override/extend the AbstractXMLOutputProc= essor and to 'hack' the code that does the escaping. 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: Element emt =3D 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 ta= gs. The thing is that when outputting these to an XML file the characters "<" a= nd ">" are changed into "<" and ">" 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 reache= d, but the method is executed, so it seems that these characters are conver= ted before the EscapeStrategy gets into action, or maybe I am doing somethi= ng 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 wh= at 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] --_000_264449A6A521A14593F58479F46B34EB014E7BF5IMCMBX03MITREOR_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable <html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr= osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" = xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:= //www.w3.org/TR/REC-html40"> <head> <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"= > <meta name=3D"Generator" content=3D"Microsoft Word 14 (filtered medium)"> <style><!-- /* Font Definitions */ @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;} @font-face {font-family:Tahoma; panose-1:2 11 6 4 3 5 4 4 2 4;} @font-face {font-family:Consolas; panose-1:2 11 6 9 2 2 4 3 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman","serif"; color:black;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;} pre {mso-style-priority:99; mso-style-link:"HTML Preformatted Char"; margin:0in; margin-bottom:.0001pt; font-size:10.0pt; font-family:"Courier New"; color:black;} span.apple-tab-span {mso-style-name:apple-tab-span;} span.HTMLPreformattedChar {mso-style-name:"HTML Preformatted Char"; mso-style-priority:99; mso-style-link:"HTML Preformatted"; font-family:Consolas; color:black;} span.EmailStyle20 {mso-style-type:personal-reply; font-family:"Calibri","sans-serif"; color:#1F497D;} .MsoChpDefault {mso-style-type:export-only; font-size:10.0pt;} @page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in;} div.WordSection1 {page:WordSection1;} --></style><!--[if gte mso 9]><xml> <o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" /> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext=3D"edit"> <o:idmap v:ext=3D"edit" data=3D"1" /> </o:shapelayout></xml><![endif]--> </head> <body bgcolor=3D"white" lang=3D"EN-US" link=3D"blue" vlink=3D"purple"> <div class=3D"WordSection1"> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D">Hi Rolf,<o:p></o:p></span= ></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></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.<o:p></o:p></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></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 “target”= to generate a popup).<o:p></o:p></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></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’t exis= t in xhtml due to unfortunate choices of version numbers of included documents in the standards).<o:p></o:p></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D">Third, you don’t al= ways have control over how the process using your xml will parse it, if at = all.<o:p></o:p></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></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.)<o:p></o:p></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></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.<o:p></o:p></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></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.<o:p></o:p></span></p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D"><o:p> </o:p></span><= /p> <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca= libri","sans-serif";color:#1F497D">Mike Brenner<o:p></o:p></= span></p> <p class=3D"MsoNormal"><a name=3D"_MailEndCompose"><span style=3D"font-size= :11.0pt;font-family:"Calibri","sans-serif";color:#1F497= D"><o:p> </o:p></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"> jdom-interest-bounces@= jdom.org [mailto:[email protected]] <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> [email protected]<br> <b>Subject:</b> Re: [jdom-interest] EscapeStrategy for in-text html tags no= t working<o:p></o:p></span></p> </div> </div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">Hi there.<br> <br> Escaping "<" and other the other characters '>', '&', a= nd '\r' is always done (with one exception...). The '\n' char is also somet= imes replaced with \r\n.<br> <br> if (ch =3D=3D '<= ;' || ch =3D=3D '>' || ch =3D=3D '&' || ch =3D=3D '\r' || ch =3D=3D = '\n'<br> = || strategy.shouldEscape(ch)) {<br> = ......<br> }<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> Element emt =3D new Element("tag");<br> emt.addContent(new ProcessingInstruction(javax.xml.trans= form.Result.PI_DISABLE_OUTPUT_ESCAPING));<br> emt.addContent(new Text("<html><body>&l= t;p></body></html>"));<br> emt.addContent(new ProcessingInstruction(javax.xml.trans= form.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:<o:p></o:p></p> </div> <blockquote style=3D"margin-top:5.0pt;margin-bottom:5.0pt"> <p class=3D"MsoNormal" style=3D"margin-left:.5in">Hello. <o:p></o:p></p> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></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.<o:p></o:p></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.<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">I tried using this Escape= Strategy:<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">------------------<o:p></= o:p></p> </div> <div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>class EscapeSimbolos implements EscapeStrategy {<o:p></o:p></p= > </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>public boolean shouldEscape(char car) {<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>switch (car) {<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>case '<':<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>return false;<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>case '>':<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>return false;<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>default:<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>return false;<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>}<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>}<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>}<o:p></o:p></p> </div> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">------------------<o:p></= o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></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.<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">This is the code to set t= he format:<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">---------------------<o:p= ></o:p></p> </div> <div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>Format formato =3D Format.getPrettyFormat();<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>formato.setTextMode(TextMode.PRESERVE);<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>formato.setEscapeStrategy(new EscapeSimbolos());<o:p></o:p></p= > </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>formato.setIgnoreTrAXEscapingPIs(true);<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><span class=3D"apple-tab-= span"></span>XMLOutputter outputter =3D new XMLOutputter(formato);<o:p></o:= p></p> </div> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">---------------------<o:p= ></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">I tried setting setI= gnoreTrAXEscapingPIs to false, because I am not sure what this does exactly= , but nothing seems to change.<o:p></o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> </div> <div> <p class=3D"MsoNormal" style=3D"margin-left:.5in">Any ideas?<o:p></o:p></p> </div> </div> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><br> <br> <br> <o:p></o:p></p> <pre style=3D"margin-left:.5in">___________________________________________= ____<o:p></o:p></pre> <pre style=3D"margin-left:.5in">To control your jdom-interest membership:<o= :p></o:p></pre> <pre style=3D"margin-left:.5in"><a href=3D"http://www.jdom.org/mailman/opti= ons/jdom-interest/[email protected]">http://www.jdom.org/mailman/option= s/jdom-interest/[email protected]</a><o:p></o:p></pre> </blockquote> <p class=3D"MsoNormal" style=3D"margin-left:.5in"><o:p> </o:p></p> </div> </body> </html> --_000_264449A6A521A14593F58479F46B34EB014E7BF5IMCMBX03MITREOR_-- --===============0259014688== 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] --===============0259014688==--