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
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="http://www.w3.org/1999/XSL/Transform" xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 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:
Use a Java DOM library to build the XML in memory, using the Java constants.
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.
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 --> </xsd:restriction>
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: http://brettporter.wordpress.com/2009/09/30/apache-maven-2-effective-implementation-sample-chapter/