Skip to main content

Problem with annotations in super class

1 reply [Last post]
amarkel
Offline
Joined: 2004-05-13
Points: 0

I am trying to define the annotations in a superclass
and have the sub-classes not have to define the methods
implemented in the superclass.

The following is the error I am getting:

Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'property' must have no character or element information item [children], because the type's content type is empty.]

(This is as if there is an expectation to find the
annotations only on declared methods)

The xsd:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

/* @(#)package-info.java 1.3 05/09/26 */
@XmlAccessorType(PROPERTY)
package com.sun.scn.asset.maps.impl;

import javax.xml.bind.annotation.XmlAccessorType;
import static javax.xml.bind.annotation.AccessType.PROPERTY;

Node.java:

import java.util.Collection;

import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlTransient;

@XmlType( name = "node" )
public class Node{

private String imName;
private String daqName;

public Node() {}

public Node( String theIMName, String theDAQName ){
imName = theIMName;
daqName = theDAQName;
}

@XmlAttribute( name = "InfoModel", required = true )
public String getIMName() { return imName;}

public void setIMName( String theName ) {
imName = theName; }

@XmlAttribute( name = "DAQ" )
public String getDAQName() { return daqName; }

public void setDAQName( String theName ) {
daqName = theName; }
}

Property.java:

import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType( name = "property" )
public class Property extends Node {

private String defValue;

public Property() { super(); }

public Property( String theIMName, String theDAQName,
String theDefault ) {
super( theIMName, theDAQName );
defValue = theDefault;
}

@XmlAttribute( name = "default" )
public String getDefaultValue() { return defValue; }

public void setDefaultValue( String theDefValue ) {
defValue = theDefValue; }
}

Definition.java:

import java.util.Collection;

import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "DAQIMMap")
public class Definition {

protected Collection properties;

protected String assetType;

public Definition() { }

public Definition( String theType,Collection prop )
{
this();
assetType = theType;
properties = prop;
}

@XmlAttribute( name = "assetType", required = true )
public String getAssetType() { return assetType; }

public void setAssetType( String theType ) {
assetType = theType; }

@XmlElement( type = Property.class,name = "property" )
public Collection getPropertyMap() {
return properties;
}

public void setPropertyMap(Collection props =
new LinkedHashSet();

props.add(
new Property( "IMName", "tag1.DAQName", "dfNameVal" ) );

props.add(
new Property( "IMTemp", "tag2.DAQTemp", "dfTempVal" ) );

props.add(
new DAQIMPropMapping( "IMHost", "tag3.DAQHost", "dfHostVal" ) );

Definition def = new Definition( path, props );

File f = new File( path );
File of = new File( "res" + path );

JAXBContext ctx1 = JAXBContext.newInstance( Definition.class );
Marshaller m = ctx1.createMarshaller();
m.setSchema( getSchema("daqimmap.xsd"));
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
m.marshal( def, new FileOutputStream( of ));
m.marshal( def, System.out );

ctx1 = JAXBContext.newInstance( DAQIMMapDefinition.class );
Unmarshaller um = ctx1.createUnmarshaller();
um.setSchema( getSchema("daqimmap.xsd"));
Object obj = um.unmarshal( of );
m.marshal( obj, System.out );
}

private Schema getSchema( String schemaResourceName ) throws SAXException {
SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
try {
System.err.println( getClass().getResource(schemaResourceName));
return sf.newSchema(getClass().getResource(schemaResourceName));
} catch (SAXException se) {
// this can only happen if there's a deployment error and the resource is missing.
throw se;
}
}

public static void main( String[] args ) throws Exception
{
MapTest1 rt = new MapTest1( args[0] );

rt.testMarshalRequest();
}
}

----

The exception is not thrown if the @XmlAttribute in
Property is changed to an @XmlElement.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
amarkel
Offline
Joined: 2004-05-13
Points: 0

The original message of this thread is still about
a problem I am facing, but has received no response.

Arieh