Skip to main content

abstract base class causing invalid JAXB-generated schema

1 reply [Last post]
gadams00
Offline
Joined: 2006-05-08

I'm using user-annotated classes, and I'm running into a problem with JAXBContext.generateSchema.

Here's my bean:

<br />
package com.foo.xml.stuff;</p>
<p>import javax.xml.bind.annotation.XmlRootElement;<br />
import javax.xml.bind.annotation.XmlType;</p>
<p>import com.foo.xml.XmlBean;</p>
<p>@XmlType(name="Simple", namespace="http://stuff.xml.foo.com")<br />
@XmlRootElement(name="simple", namespace="http://stuff.xml.foo.com")<br />
public class SimpleXml extends XmlBean<br />
		implements Cloneable {</p>
<p>	/**<br />
	 *<br />
	 */<br />
	private static final long serialVersionUID = 2831867800440286221L;<br />
	private String name;<br />
 	private String value;</p>
<p>	/**<br />
    * No-argument class constructor<br />
    */<br />
	public SimpleXml() {<br />
	}</p>
<p>	/**<br />
	 * Clears the internal state of this container.<br />
	 */<br />
	public void clear() {<br />
		name = null;<br />
		value = null;<br />
	}</p>
<p>	/**<br />
	 * Returns a deep copy of this container.<br />
	 */<br />
	public Object clone() throws CloneNotSupportedException {<br />
		SimpleXml newXml = (SimpleXml)super.clone();<br />
		newXml.setName(name);<br />
		newXml.setValue(value);<br />
		return(newXml);<br />
	}</p>
<p>	public String getName() {<br />
		return name;<br />
	}</p>
<p>	public void setName(String pName) {<br />
		name = pName;<br />
	}</p>
<p>	public String getValue() {<br />
		return value;<br />
	}</p>
<p>	public void setValue(String pValue) {<br />
		value = pValue;<br />
	}<br />
}</p>
<p>

and here's the abstract base class for the bean:

<br />
package com.foo.xml;</p>
<p>import java.io.Serializable;</p>
<p>import javax.xml.bind.JAXBException;<br />
import javax.xml.bind.annotation.XmlType;</p>
<p>import org.apache.commons.logging.Log;<br />
import org.apache.commons.logging.LogFactory;</p>
<p>@XmlType(name="XmlBean", namespace="http://xml.foo.com")<br />
public abstract class XmlBean implements Serializable {</p>
<p>	private static Log log = LogFactory.getLog(XmlBean.class);</p>
<p>	public String toString() {<br />
		try {<br />
			return JAXBHelper.marshal(this);<br />
		} catch (JAXBException e) {<br />
			log.error("JAXBException", e);<br />
			throw new RuntimeException(e);<br />
		}<br />
	}</p>
<p>}</p>
<p>

notice that the bean and the abstract base class are in separate packages and separate namespaces. I'm not sure, but I think this is causing the problem.

Here's the code that generates the schema:

<br />
import java.io.IOException;</p>
<p>import javax.xml.bind.JAXBContext;<br />
import javax.xml.bind.JAXBException;</p>
<p>public class SchemaGen {</p>
<p>	public static void main(String[] args) throws JAXBException, IOException {<br />
		JAXBContext jc = JAXBContext.newInstance("com.foo.xml:com.foo.xml.stuff");<br />
		jc.generateSchema(new SimpleSchemaOutputResolver());<br />
	}<br />
}</p>
<p>import java.io.File;<br />
import java.io.FileOutputStream;<br />
import java.io.IOException;<br />
import java.io.OutputStream;<br />
import java.io.OutputStreamWriter;</p>
<p>import javax.xml.bind.SchemaOutputResolver;<br />
import javax.xml.transform.Result;<br />
import javax.xml.transform.stream.StreamResult;</p>
<p>public class SimpleSchemaOutputResolver extends SchemaOutputResolver {</p>
<p>	@Override<br />
	public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {<br />
		File outputFile = new File(suggestedFileName);<br />
		OutputStream outputStream = new FileOutputStream(outputFile);<br />
		Result result = new StreamResult(new OutputStreamWriter(outputStream));<br />
		result.setSystemId(outputFile.toURI().toString());<br />
		return result;<br />
	}</p>
<p>}</p>
<p>

The schema comes out in 2 files, 1 containing the schema for the abstract base class, XmlBean:

<br />
<?xml version="1.0" standalone="yes"?></p>
<p>

this schema is fine.. but schema2.xsd, containing the schema for the bean, SimpleXml, doesn't validate:

<br />
<?xml version="1.0" standalone="yes"?></p>
<p>

the validation error is:

<br />
src-resolve.4.2: Error resolving component 'ns2: XmlBean'. It was detected that 'ns2: XmlBean' is in namespace 'http://xml.foo.com', but components from this namespace are not referenceable from schema document 'file:///J:/tempwork/JAXBTest2/schema2.xsd'. If this is the incorrect namespace, perhaps the prefix of 'ns2: XmlBean' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///J:/tempwork/JAXBTest2/schema2.xsd'.<br />

it appears that for some reason, the import is being ommitted by JAXBContext.generateSchema. I've seen it generate imports other times. This also affects JAX-WS, which is where I initially ran into it.

Greg

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

This looks like a bug. If you can zip those classes up and file an issue in http://jaxb.dev.java.net/issues/, we'll take a look and fix it.

Thanks!