Hi,
thanks for your advice. I was quite disappointed that I
can't use scala xml support. However I have another xml which I would
like to process with scala. It is classical maven pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
http://maven.apache.org/POM/4.0.0" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>myproject</artifactId>
<packaging>war</packaging>
<version>6.2.0</version>
<name>myproject</name>
<!-- </repository> -->
<!-- <repository> -->
<!-- <id>jboss-repo</id> -->
<!-- <url>
http://repository.jboss.org/nexus/content/groups/public</url> -->
<!-- </repository> -->
<!-- </repositories> -->
<dependencies>
<!-- WICKET DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>6.2.0</version>
</dependency>
</dependencies>
<!-- ... -->
</project>
And
I need to increase version of 'myproject'. So instead of 6.2.0 will be
6.2.1 and version of wicket remains 6.2.0. I wrote simple app which
works good but code doesn't look nice and clean:
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import scala.io.Source
object App4 extends App {
val Pom =
"""(.*)<packaging>war</packaging>(.*)<version>(\d+)\.(\d+)\.(\d+)</version>(.*)<name>myproject</name>(.*)""".r
val partStart = "<packaging>war</packaging>\n\t<version>";
val partEnd = "</version>\n\t<name>myproject</name>";
processPom(new File("pom.xml"))
def processPom(file: File) {
val fileContents = Source.fromFile(file.getAbsolutePath()).mkString
if (fileContents != null && !fileContents.isEmpty) {
val Pom(_, _, f, s, t, _, _) = fileContents.filter(_ != '\n')
val oldVersion = f + "." + s + "." + t
val newVersion = f + "." + s + "." + (t.toInt + 1)
chnageVersion(file, fileContents, oldVersion, newVersion);
}
def chnageVersion(file: File, fileContents: String, oldVersion: String, newVersion: String) {
val regex = (partStart + oldVersion + partEnd).r
val replc = (partStart + newVersion + partEnd);
val newFileContents = regex.replaceAllIn(fileContents, replc)
changeFileContent(file, newFileContents);
def changeFileContent(file: File, fileContents: String) {
val newFile = new File(file.getName + ".conv")
val writer = new BufferedWriter(new FileWriter(newFile))
writer.write(fileContents, 0, fileContents.length)
writer.close
file.delete
newFile.renameTo(file)
}
}
}
}
Could you please hint me how to solve it more elegant and scala way?
Thanks in advance!
Dne čtvrtek, 15. listopadu 2012 21:47:00 UTC+1 danisevsky napsal(a):