How can I leverage String constants in an XML file?

I'd like to enforce standardized keys by storing them as static final String variables on a Java class, and either referencing or statically importing them, to use them as values in either XML, Strings, Methods, Annotations, etc.

Does anyone know a good way to have Maven insert (like filtering) values like StringKeys.SOME_KEY into an XML file? e.g. something like

<element value="${StringKeys.SOME_KEY}"/>

or similar - the main idea is to enforce commonality and prevent key mis-alignment. Or an alternative solution to accomplish the same - with some semantic that if a non-existant String is referenced, that it fails during build? Bonus points if it works in C# as well.


You can use Xslt to generate your POM , probably to refer you can use the below

<xsl:stylesheet version="1.0" xmlns:xsl=""  xmlns:dyn="" extension-element-prefixes="dyn"
  xmlns:xsi="" xmlns:dsig=""  xmlns:masterrule="com.Xxxxxx.ClassName" exclude-result-prefixes="masterrule #default">

<xsl:value-of select="masterrule:xxxxx"/>

You can use XSLT and use EL expressions to expand strings.

Sometimes using string constants is more trouble than it is worth. And IMO, this is one such example.

But if you wish to pursue this, here are some ideas that may work for you:

  1. Use a Java DOM library to build the XML in memory, using the Java constants.

  2. Put the constants into a Properties object, then create an XML file template using Velocity, Freemarker or an equivalent Java compatible templating language that expands the properties values into the XML.

    2a. In some circumstances JSPs could be used instead of a generic Java-compatible templating language.

  3. Generate the XML with placeholders for the named constants, then use XSLT to replace the placeholders with the actual values.

But in each case, the steps you take in order to use the named constants leads to fragility in other areas, not to mention more code and extra processing. Which brings me back to my original point ...

EDIT I think that the real reason that there is no really good solution to this problem is that it is a really hard problem.

To illustrate, using JSPs to generate the XML (and discipline) would ensures that you only use valid Java constants, and is fail-fast with respect to the generated Java classes. However, the JSP approach does not guarantee that you generate well-formed XML.

The root of the problem is that you need the expressiveness of embedding Java constructs in XML templates, combined with static checking of the well-formedness of both the Java and the XML aspects of the templates. You really need proper linguistic support for this, but in reality the available solutions just output characters without regard for (in this case) XML syntax rules.

Can't you use XML enumeration restriction of string?

<xsd:restriction base = "xsd:string">
  <xsd:enumeration value = "AUD"/><!-- Australian Dollar -->
  <xsd:enumeration value = "BRL"/><!-- Brazilian Real -->
  <xsd:enumeration value = "CAD"/><!-- Canadian Dollar -->

Velocity or similar is your best bet, but it would require writing your own plugin.

I'd generally do this in the other direction - store the keys and values in a properties file and filter that into XML as well as reading them into the Java / C# class. You can find an example in the free sample chapter from our book:

Need Your Help

iOS Gesture Handling: where to add a gesture recognizer when using a custom UIView

ios5 uigesturerecognizer

I have a view controller where I display a grid/array of images, where every image view is a custom nib (custom nib because images have a name &amp; like/dislike icon too). So I displayed the grid of

How to make a sort array and count array button?

javascript arrays

I am trying to create a code that sorts products into alphabetical order and then count the number of them for class. I know how to create a program that does one or the other, but when I try to pu...