Saturday, 15 May 2010

xml - How to use Ant echo task to append to file when what I'm adding contains CDATA? -



xml - How to use Ant echo task to append to file when what I'm adding contains CDATA? -

i need create ant task add together lines @ start , @ end of .txt files.

i need add together start of each file:

<ac:structured-macro ac:name="html"><ac:plain-text-body><![cdata[

and need add together end of each file:

></ac:plain-text-body></ac:structured-macro><p>&nbsp;</p><p>&nbsp;</p><p><ac:structured-macro ac:name="anchor"><ac:parameter ac:name="">_goback</ac:parameter></ac:structured-macro></p><p>&nbsp;</p><p>&nbsp;</p>

i understand can utilize echo append these files. seems ![cdata how phone call out content added code rather executing part of in ant script. problem piece need add together contains ![cdata.

that is, need file @ end 2 lines above added verbatim. can explain how this, if not echo, there other way?

i'm new ant i've created build files other ant tasks week work perfectly.

thanks

update: elaborate, here's simplified illustration of need result:

<ac:structured-macro ac:name="html"><ac:plain-text-body><![cdata[ <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html> </html> ]]></ac:plain-text-body></ac:structured-macro><p>&nbsp;</p><p><ac:structured-macro ac:name="anchor"><ac:parameter ac:name="">_goback</ac:parameter></ac:structured-macro></p><p>&nbsp;</p><p>&nbsp;</p>

what i'm creating text files create pages in confluence. opening , closing sections create "html include macro" in confluence allows me utilize html, instead of different scheme confluence uses. between <html> , </html> in above illustration normal html page markup lives.

if have here in .txt file set in confluence folder, create confluence page box, within of can utilize html.

what xml publication (from oxygen) outputs part starts <!doctype html , ends </html>

it’s not cdata cause problems here, if seek include xml syntax in section want treat text build.xml won’t valid.

for example, looking @ text want add together start of file, without <!cdata[. utilize concat task this, , first effort might this:

class="lang-xml prettyprint-override"><concat destfile="target.xml> <string><ac:structured-macro ac:name="html"><ac:plain-text-body></string> <file file="generated-doc.html"/> </concat>

this isn’t valid, error (something the prefix "ac" element "ac:structured-macro" not bound.) because ant’s xml parser trying interpret tags part of build file, not text info re-create target.

the solution escape content isn’t parsed xml. 1 way replace < &lt; , & &amp;:

class="lang-xml prettyprint-override"><concat destfile="target.xml> <string>&lt;ac:structured-macro ac:name="html">&lt;ac:plain-text-body></string> <file file="generated-doc.html"/> </concat>

you can extend thought content includes <![cdata[, escape <s:

class="lang-xml prettyprint-override"><concat destfile="target.xml> <string>&lt;ac:structured-macro ac:name="html">&lt;ac:plain-text-body>&lt;![cdata[</string> <file file="generated-doc.html"/> </concat>

this can bit unwieldy, solution utilize cdata section, within section don’t have escape < , &, won’t treated xml markup:

class="lang-xml prettyprint-override"><concat destfile="target.xml> <string><![cdata[<ac:structured-macro ac:name="html"><ac:plain-text-body><![cdata[]]></string> <file file="generated-doc.html"/> </concat>

note inner <![cdata[ escaped because within cdata section. opening <![cdata[ closing ]]> escaped.

in fact closing ]]> need add together end of file causes bigger problem. if seek repeat did above end section might (including start section, , i’ve shortened end snippet clarity):

class="lang-xml prettyprint-override"><concat destfile="target.xml> <string><![cdata[<ac:structured-macro ac:name="html"><ac:plain-text-body><![cdata[]]></string> <file file="generated-doc.html"/> <string><![cdata[]]></ac:plain-text-body></ac:structured-macro>]]></string> </concat>

this wrong though, since cdata section closed ]]> want part of content. there no way escape ]]> part of cdata section. 1 way round utilize &lt; type of escaping instead of using cdata:

class="lang-xml prettyprint-override"><string>]]&gt;&lt;/ac:plain-text-body>&lt;/ac:structured-macro></string>

note have replace > in ]]> &gt; – don’t need replace >, when appears ]]> do.

another way round break ]]> two, each 1 beingness in different cdata section. bit cumbersome, allows leave < in rest of string. works having 1 section ends in ]], , after starts >. this:

class="lang-xml prettyprint-override"><concat destfile="target.xml> <string><![cdata[<ac:structured-macro ac:name="html"><ac:plain-text-body><![cdata[]]></string> <file file="generated-doc.html"/> <string><![cdata[]]]]><![cdata[></ac:plain-text-body></ac:structured-macro>]]></string> </concat>

in case, since ]]> @ start of content string, cleanest result might combine 2 escaping styles:

class="lang-xml prettyprint-override"><concat destfile="target.xml> <string><![cdata[<ac:structured-macro ac:name="html"><ac:plain-text-body><![cdata[]]></string> <file file="generated-doc.html"/> <string>]]&gt;<![cdata[</ac:plain-text-body></ac:structured-macro>]]></string> </concat>

the other issue need out if file content contains cdata section, since closing ]]> of close outer section. if that’s possible you’ll need filter file contents replace ]]> ]]]]><![cdata[>.

xml ant

No comments:

Post a Comment