-------- Original Message -------- Subject: Re: Remove duplicate JAR file names from an XML file From: <jan.mate...@rzf.fin-nrw.de> To: user@ant.apache.org Date: 27.07.2010 08:32
>> I have an xml file which lists JAR file names in it with space >> specified as a delimiter. There are duplications of JAR files >> name in the file and I was wondering if there is a way to some >> how get ride of the duplication, to get a file which has a >> unique set of JAR file names? >> >> >> Example of file: >> >> antlr-2.7.6.jar antlr-2.7.6.jar antlr-2.7.6.jar >> aopalliance-1.0.jar aopalliance-1.0.jar aopalliance-1.0.jar >> commons-validator-1.0.2.jar commons-validator-1.0.2.jar >> commons-lang-2.2.jar commons-lang-2.2.jar Whenever some kind of xml processing occurs within your ant workflow i recommend the use of the xmltask[1]. Two solutions for your problem >From your first posting i assume you have some xml like : <?xml version="1.0" encoding="UTF-8"?> <jars> <files>antlr-2.7.6.jar antlr-2.7.6.jar antlr-2.7.6.jar aopalliance-1.0.jar aopalliance-1.0.jar aopalliance-1.0.jar commons-validator-1.0.2.jar commons-validator-1.0.2.jar commons-lang-2.2.jar commons-lang-2.2.jar</files> </jars> 1) use xmltask/xpath to get a list of filenames and <script ../> afterwards to get your distinct list. <?xml version="1.0" encoding="UTF-8"?> <project> <!-- Import XMLTask --> <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/> <target name="depends"> <xmltask source="./jars.xml"> <copy path="//files/text()" append="true" property="alljars" /> </xmltask> <echo>${alljars}</echo> <script language="beanshell"> <![CDATA[ String[] jars = project.getProperty("alljars").split(" "); Set set = new HashSet(Arrays.asList(jars)); String[] distinct = (set.toArray(new String[set.size()])); project.setProperty("uniquejars", Arrays.toString(distinct)); ]]> </script> </target> <target name="main" depends="depends"> <echo>$${uniquejars} = ${uniquejars}${line.separator}</echo> <echo>$${alljars} = ${alljars}</echo> </target> </project> Buildfile: /workspace/ant/foobar.xml depends: [xmltask] Cannot append values to properties [echo] antlr-2.7.6.jar antlr-2.7.6.jar antlr-2.7.6.jar aopalliance-1.0.jar aopalliance-1.0.jar aopalliance-1.0.jar commons-validator-1.0.2.jar commons-validator-1.0.2.jar commons-lang-2.2.jar commons-lang-2.2.jar main: [echo] ${uniquejars} = [antlr-2.7.6.jar, commons-lang-2.2.jar [echo] , aopalliance-1.0.jar, commons-lang-2.2.jar, commons-validator-1.0.2.jar] [echo] ${alljars} = antlr-2.7.6.jar antlr-2.7.6.jar antlr-2.7.6.jar aopalliance-1.0.jar aopalliance-1.0.jar aopalliance-1.0.jar commons-validator-1.0.2.jar commons-validator-1.0.2.jar commons-lang-2.2.jar commons-lang-2.2.jar BUILD SUCCESSFUL Total time: 902 milliseconds 2) take influence on the creation of the xml file if possible, and create a structure that is more xpath suitable and simply make use of xpath, f.e. <?xml version="1.0" encoding="UTF-8"?> <jars> <file>antlr-2.7.6.jar</file> <file>aopalliance-1.0.jar</file> <file>antlr-2.7.6.jar</file> <file>commons-validator-1.0.2.jar</file> <file>commons-validator-1.0.2.jar</file> <file>commons-lang-2.2.jar</file> <file>whatever-0.0.1</file> <file>commons-lang-2.2.jar</file> <file>whatever-0.0.1</file> <file>aopalliance-1.0.jar</file> </jars> with some xpath[2] tricks (sadly XPATH 2.0 => distinct-value(...) doesn't work) : <?xml version="1.0" encoding="UTF-8"?> <project> <!-- Import XMLTask --> <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/> <target name="depends"> <xmltask source="./jars.xml"> <copy path="//file/text()" append="true" property="alljars" propertyseparator="${line.separator}" /> <copy path="//file/text()[not(preceding::file/text() = .)]" append="true" property="jars" propertyseparator="${line.separator}" /> </xmltask> </target> <target name="main" depends="depends"> <echo>--- $${alljars} ---${line.separator}${alljars}</echo> <echo>${line.separator}--- $${jars} ---${line.separator}${jars}</echo> </target> </project> you'll get your distinct list : Buildfile: /workspace/ant/foobar.xml depends: [xmltask] Cannot append values to properties ... don' get annoyed from those messages, simply ignore or do a search in the xmlproperty task sources and comment it out main: [echo] --- ${alljars} --- [echo] antlr-2.7.6.jar [echo] aopalliance-1.0.jar [echo] antlr-2.7.6.jar [echo] commons-validator-1.0.2.jar [echo] commons-validator-1.0.2.jar [echo] commons-lang-2.2.jar [echo] whatever-0.0.1 [echo] commons-lang-2.2.jar [echo] whatever-0.0.1 [echo] aopalliance-1.0.jar [echo] [echo] --- ${jars} --- [echo] antlr-2.7.6.jar [echo] aopalliance-1.0.jar [echo] commons-validator-1.0.2.jar [echo] commons-lang-2.2.jar [echo] whatever-0.0.1 BUILD SUCCESSFUL Total time: 603 milliseconds [1] http://www.oopsconsultancy.com/software/xmltask/ [2] http://www.zvon.org/xxl/XPathTutorial/General/examples.html Regards, Gilbert --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@ant.apache.org For additional commands, e-mail: user-h...@ant.apache.org