Whitespace trimming in metadata
adrian.brock Jun 16, 2008 9:39 AMCurrently we have a failing test because whitespace is not getting
trimmed from a jndi-name. NOTE there is an extra space after SessionA
2008-06-16 14:38:33,596 DEBUG [org.jboss.test.naming.ejb.TestEjbLinkBean] (WorkerThread#0[127.0.0.1:54701]) failed javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: SessionA not bound] at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1254) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:767) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:776) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:629) at javax.naming.InitialContext.lookup(InitialContext.java:351) at org.jboss.test.naming.ejb.TestEjbLinkBean.testEjbLinkCaller(TestEjbLinkBean.java:74)
This is coming from the following jboss.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd"> <jboss> <enterprise-beans> <session> <ejb-name>SessionB</ejb-name> <jndi-name>naming/SessionB</jndi-name> <ejb-ref> <ejb-ref-name>ejb/NoLinkSessionA</ejb-ref-name> <jndi-name>naming/SessionA </jndi-name> </ejb-ref> <ejb-local-ref> <ejb-ref-name>ejb/NoLinkLocalSessionA</ejb-ref-name> <local-jndi-name>naming/local/SessionA</local-jndi-name> </ejb-local-ref> </session> </enterprise-beans> </jboss>
In our jboss.xml schema, jndi-name inhertis eventually from the simple xml type
xsd:name
which has a facet defined that says to normalise space.
However, in the dtd (used above), jndi-name is just defined as #PCDATA
But correcting the schema/dtd types wouldn't be the complete fix if
xml validation is turned off since then it wouldn't look at the dtd/schema anyway.
What I'd suggest is that we introduce a convenience annotation
so you can specify whether whitespace should be preserved at the shema level,
e.g. something like
@XmlRootElement(name="jboss", namespace=JavaEEMetaDataConstants.JBOSS_NS) @JBossXmlSchema( xmlns={@XmlNs(namespaceURI = JavaEEMetaDataConstants.JAVAEE_NS, prefix = "jee")}, ignoreUnresolvedFieldOrClass=false, namespace=JavaEEMetaDataConstants.JBOSS_NS, elementFormDefault=XmlNsForm.QUALIFIED, // HERE normaliseSpace=true) @XmlType(name="jbossType", namespace=JavaEEMetaDataConstants.JBOSS_NS) public class JBoss50MetaData extends JBossMetaData
With the option on paricular elements/attributes to do
@XmlPreserveWhitespace
This would then more easily map to what we used to do in the old parsing
where it trimmed everything (from the old org.jboss.metadata.MetaData utility class)
public static String getElementContent(Element element, String defaultStr, boolean replace) { if (element == null) return defaultStr; NodeList children = element.getChildNodes(); String result = ""; for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.TEXT_NODE || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) { result += children.item(i).getNodeValue(); } else if( children.item(i).getNodeType() == Node.COMMENT_NODE ) { // Ignore comment nodes } else { result += children.item(i).getFirstChild(); } } // HERE if (replace) return StringPropertyReplacer.replaceProperties(result.trim()); else return result.trim(); }