how to escape special chars (invalid in xml) in Ant echo? - java

I'm trying to do this in Ant:
<echo message="Success!" />
But it doesn't work:
BUILD FAILED
./build.xml:92: Character reference "&#
How to do it?

The 0x1B character is invalid in XML contents (Invalid Characters in XML). So you need some workaround. I would go with a javascript workaround, but I give also 2 additional solutions:
javascript
<script language="javascript">
project.setNewProperty("esc", "\u001b");
</script>
<echo>${esc}</echo>
native2ascii
If you want the output in a file, then you could first output it using java escape \u001b, then convert it using reverse Native2Ascii routine. Regardless of the selected encoding it always decodes \u sequences.
<echo file="a.enc">\u001b</echo>
<native2ascii includes="a.enc" ext=".txt" dest="${basedir}"
encoding="iso-8859-1" reverse="true" />
property file
Finally you may have the unfortunate string constant in a file:
<property file="prop.txt" />
<echo>myEsc:${myEsc}</echo>
while the prop.txt contents is:
myEsc=\u001b

Simply use CDATA :
<project>
<echo><![CDATA[
Success!
]]>
</echo>
</project>

& is a special character in ant, so you should replace it with &apos;
<echo message="&#27;[44;1;37mSuccess!&#27;[m" />

Related

how can i get a matching string from a xml file using <getxmlproperties> tag using ant and replace the matched string in other xml file

My requirement is to match following tag in data.xml file and replace the content in display.xml file using in ant
data.xml
--------
<data>123456789</data>
display.xml
-----------
<data>abcdefg</data
I need to match the content in data.xml file and replace the it in display.xml file.
my final output should be like:
data.xml
--------
<data>123456789</data>
display.xml
-----------
<data>123456789</data>
How can i solve this Issue? Thanks in advance
I did not find any Ant task named GetXmlProperties, but I think you may have thought about this one, XmlProperty, which composes a sequence of properties from parsing an XML document.
Two ways, to achieve what you want, come to my mind (there may be more):
The most primitive would be to just use the XmlProperty task to retreive the value in question and use a crude Replace task on the destination file, doing a simple string replace, by handling the destination as a plain text file, instead of caring for the XML logic in it. However, doing string match and replace with XML data is no fun and error prone.
Thus I propose a second approach, which is to use the XmlTask, as per the following example. Adding a prefix xml to our newly parsed properties makes it easier to distinguish them from the rest. For demo purposes we also let the build process log all the new properties under the xml prefix to the console by using the EchoProperties task.
<project name="SO63816092" default="default">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" />
<xmlproperty file="data.xml" prefix="xml" />
<echoproperties prefix="xml"/>
<target name="default">
<xmltask source="display.xml" dest="display.xml" failWithoutMatch="true">
<replace path="//data/text()" withText="${xml.data}" />
</xmltask>
</target>
</project>
Using the second approach, while using the following input file data.xml:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
and the following destination file display.xml:
<?xml version="1.0" encoding="UTF-8"?>
<data>abcdefg</data>
I can successfully accomplish what you ask for and get display.xml to become:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
Just remember to place the XmlTask Java jar in the current classpath for your Ant process and note, that you may need to change the XPath expression, we use, //data/text(), to something more refined, should your document structure demand it, because the way, it is now, it would replace the value for all data elements it finds, throughout the whole display.xml document.

Intellij regular expression search in XML atributes

I'm using Intellij and I want to search for XML attributes.
For example this XML line:
<img src="madonna.jpg" alt='Foligno Madonna, by Raphael' />
I want to search my project for any other instances of this line:
<img src="BLABLALBA" alt='BLABLALBA' />
I tried using this regular expression : src="\W" but no results was found.
Any help appriciated.
Try Structural Search & Replace (Edit | Find | Search Structurally...). Use the following pattern:
<img src="$A$" alt="$B$" />
And set the file type to XML or HTML depending on what type of files you are searching.

ant ReplaceRegExp to replace string with double quotes

Using ANT I'm trying to find/replace a string with spaces and double quotes using ANT replaceregexp task.
Actual tag in xml
<ser:debug enabled="false">
Below both failed.
"<replaceregexp file="filepath" match="debug enabled="false"" replace="debug enabled="true"" byline="true" />"
And
"<replaceregexp file="filepath" match="debug\ enabled\=\"false\"" replace="debug\ enabled\=\"true\"" byline="true" />"
I'm getting this error:
Element type "replaceregexp" must be followed by either attribute specifications, ">" or "/>"
However this one works fine without any issue for a different tag...since no such special char.
<replaceregexp file="filepath" match="sql-debug-mode>true" replace="sql-debug-mode>false" byline="true" />
have also tried surrounding with ' single quote...that worked – enthuguy
I need to replace dir="EAR" with "${stage.prj.dir}EAR"
This works for me
<replaceregexp file="${stage.prj.dir}/LMS_HOME/EAR/ExtLib/jar_replace.xml"
match=""EAR""
replace=""${stage.prj.dir}EAR""
byline="true"/>

How to provide two values in query string in LzBrowser.loadURL in openlaszlo

How can i pass two values in a query string using LzBrowser.loadURL() in openlaszlo.
My sample code:
LzBrowser.loadURL("../modules/Index.jsp?base="+ canvas.base +" id=" + canvas.Id,"fraMain");
I am getting number Format exception:
java.lang.NumberFormatException: For input string: "1 base=1"]
Please help in solving this.
Thanks,
Raj
You should add & between two query parameters . You should write like this
LzBrowser.loadURL("../modules/Index.jsp?base="+ canvas.base +"&id=" + canvas.Id,"fraMain");
We have to provide & amp; instead of &. It worked for me.
The number format exception means, that at some point in your code the browser tries to parse a string into a number. Which version of OpenLaszlo did you use, and which runtime?
Based on the use of LzBrowser (starting with OpenLaszlo 4.1 you have to use lz.Browser instead) it seems that you are using either a 3.x or the 4.0 release of OpenLaszlo.
If you want to use the ampersand "&" within methods containing JavaScript code, the XML entity rules apply (everything which starts with an & will be treated as an XML entitity). Adding sections in your methods will let you use the & as you'd do in normal JavaScript or ActionScript code, e.g.
<canvas debug="true">
<button text="Open URL" onclick="canvas.loadUrl()" />
<attribute name="base" type="number" value="99" />
<attribute name="iD" type="number" value="10" />
<method name="loadUrl"><![CDATA[
lz.Browser.loadURL("http://www.google.com?base=" + canvas.base + "&iD=" + canvas.iD, "_blank");
]]></method>
</canvas>
Without the CDATA section, the following compiler error will be thrown:
loadurl.lzx:10:71: The reference to entity "iD" must end with the ';'
delimiter. Look for a misplaced or unescaped ampersand ('&') character
in your source code.
The described workaround of using "&" instead of "&" and no CDATA section is of course a valid option - although it reduced the readability of the JavaScript code in my eyes.
Actually there are two ways to do this, both of the following are correct:
1) Escape the special HTML characters (in this case &) by surrounding the internal code with <![CDATA[ and ]]> tags:
<method name="loadUrl">
<![CDATA[
lz.Browser.loadURL("http://www.google.com?base=" + canvas.base + "&iD=" + canvas.iD, "_blank");
]]>
</method>
2) HTML-entity encode the & character to &
<method name="loadUrl">
lz.Browser.loadURL("http://www.google.com?base=" + canvas.base + "&iD=" + canvas.iD, "_blank");
</method>
Which method you choose is up to your own preference.

Ant string functions?

Does Ant have any way of doing string uppercase/lowercase/captialize/uncaptialize string manipulations? I looked at PropertyRegex but I don't believe the last two are possible with that. Is that anything else?
From this thread, use an Ant <script> task:
<target name="capitalize">
<property name="foo" value="This is a normal line that doesn't say much"/>
<!-- Using Javascript functions to convert the string -->
<script language="javascript"> <![CDATA[
// getting the value
sentence = project.getProperty("foo");
// convert to uppercase
lowercaseValue = sentence.toLowerCase();
uppercaseValue = sentence.toUpperCase();
// store the result in a new property
project.setProperty("allLowerCase",lowercaseValue);
project.setProperty("allUpperCase",uppercaseValue);
]]> </script>
<!-- Display the values -->
<echo>allLowerCase=${allLowerCase}</echo>
<echo>allUpperCase=${allUpperCase}</echo>
</target>
Output
D:\ant-1.8.0RC1\bin>ant capitalize
Buildfile: D:\ant-1.8.0RC1\bin\build.xml
capitalize:
[echo] allLowerCase=this is a normal line that doesn't say much
[echo] allUpperCase=THIS IS A NORMAL LINE THAT DOESN'T SAY MUCH
BUILD SUCCESSFUL
Update for WarrenFaith's comment to separate the script into another target and pass a property from the called target back to the calling target
Use antcallback from the ant-contrib jar
<target name="testCallback">
<antcallback target="capitalize" return="allUpperCase">
<param name="param1" value="This is a normal line that doesn't say much"/>
</antcallback>
<echo>a = ${allUpperCase}</echo>
</target>
and capitalise task uses the passed in param1 thus
<target name="capitalize">
<property name="foo" value="${param1}"/>
Final output
[echo] a = THIS IS A NORMAL LINE THAT DOESN'T SAY MUCH
you could use the script task and use a jsr223-supported script language like javascript, jruby, jython,... to do your string handling

Categories

Resources