My favorites | Sign in
Project Home Downloads Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Project Information
Members

About

This program performs a regular expression search on text input and replaces any match with text taken from an XML file by a XPath expression. The normal usage is:

xmuta < input-template > output-file xml-file regular-expression xpath-expression

The program is written in Java and has up to a recent Java version no dependencies. In order to build the program you need either GNU Make or Apache Ant.

Example

The following example shows how to build a Java Manifest file by extracting the manifest data from a Maven Pom file, in which the data has been embedded.

Embedding data in a Maven Pom file

It is possible to embed anything in a Maven Pom file, because the Maven schema definition contains several nodes, which may contain any data. The most suited candidate is the node /project/properties. It is useful to give the embedded data a own name space, to distinguish it from the rest of the Pom file. It can be done in the following way.

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>example</groupId>
  <artifactId>example</artifactId>
  <version>0.1</version>
  <properties>
    <m:manifest xmlns:m="http://xmuta/maven/manifest">
      <m:version>1.0</m:version>
      <m:created-by>Example &lt;example@example.com&gt;</m:created-by>
      <m:main-class>example</m:main-class>
    </m:manifest>
  </properties>
</project>

Testing XPaths with xmuta

The program xmuta provides an option to test some XPath expressions. For example the following expression dumps the complete XML file:

xmuta -x -p < example.pom /

The -x option tells xmuta to dump XML. Normally xmuta would output only text.

In order to select a specific node it is necessary to specify the correct name space. XPath does not know the concept of default name spaces. Therefor it is necessary to specify an empty name space in order to select a node of the default name space of the XML file. Normally it would be possible to use explicit name spaces, but in this specific case it is necessary to use a default name space, because Maven (I have tested with 3.0.4) is not able to parse the Pom file, if it does not use the Maven schema as the default name space. So the right XPath for selecting the modelVersion is this:

xmuta -p < example.pom /:project/:modelVersion

This displays 4.0.0.

Designing a template syntax

With this knowledge and some knowledge about regular expressions it is possible to build the Manifest file without the need to rely on Maven for this job. Without the -p option xmuta is in substitution mode. In this mode xmuta replaces all regular expression matches with XPath matches. A typical way to use this functionality is to write a template file with some variables, which can be replaced the their values taken from a XML file. This is an example for a template of a Manifest file:

Manifest-Version: ${Version}
Created-By: ${Created-By}
Main-Class: ${Main-Class}

The syntax for the variables in this example is similar to Unix shell syntax. But it does not matter, because the only requirement is, that you are able to write regular expressions, which match the variable names. The right regular expression for ${Version} is \$\{Version\}. It is necessary to quote the dollar sign and the braces, because they have a special meaning in regular expression.

Filling out the template with xmuta

This is the complete call to xmuta to create a Manifest file from the shown Pom file by using the above template file:

xmuta < manifest.in > manifest example.pom \
    '\$\{Version\}'    '/:project/:properties/m:manifest/m:version' \
    '\$\{Created-By\}' '/:project/:properties/m:manifest/m:created-by' \
    '\$\{Main-Class\}' '/:project/:properties/m:manifest/m:main-class'

The quoted newlines are necessary in order to wrap the lines. It is also possible to write all arguments in one line. The example will output the correct Manifest file:

Manifest-Version: 1.0
Created-By: Example <example@example.com>
Main-Class: example
Powered by Google Project Hosting