users@jaxb.java.net

Re: How to add sgml / an xhtml tree without jaxb bindings

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Tue, 17 Jun 2008 21:00:38 +0200

Hi,

Just for the record: I have implemented the following to achieve what I
was looking for:

final Elements pElement = Elements.el( "p" )
    .add( Elements.val( "h3", "foo" ) )
    .add( Elements.el( "pre" )
        .add( Elements.val( "code", "some code" ) ) );

See the implementation of Elements below.

This is just a start. The value (val) stuff is implemented somehow
naive, as it would not allow s.th. like val( "h3", "my
<em>emphasized</em> header" )...

Any recommendations welcome!

Cheers,
Martin


=========================== Elements.java =====================

public class Elements extends JAXBElement<XhtmlElementType> {

    public static Elements el( String elementName ) {
        return createElement( elementName );
    }

    public static Object val( String elementName, String value ) {
        return createElement( elementName, value );
    }
    
    private static final long serialVersionUID = 1L;
    
    public Elements(QName name, Class<XhtmlElementType> clazz,
            XhtmlElementType element) {
        super( name, clazz, element );
    }
    
    public Elements add( Object ... childNodes ) {
        if ( childNodes != null ) {
            for ( Object childNode : childNodes ) {
                getValue().getChildNodes().add( childNode );
            }
        }
        return this;
    }

    public Elements addChild( Object child ) {
        getValue().getChildNodes().add( child );
        return this;
    }

    private static Elements createElement( final String elementName ) {
        try {
            
            final XhtmlElementType element = new XhtmlElementType();
            
            final Elements jaxbElement = new Elements(
                    new QName( "http://www.w3.org/1999/xhtml", elementName ),
                    XhtmlElementType.class,
                    element );
            
            return jaxbElement;
            
        } catch ( Exception e ) {
            throw new RuntimeException( e );
        }
    }

    private static JAXBElement<XhtmlValueType> createElement( final String elementName, String value ) {
        try {
            
            final XhtmlValueType element = new XhtmlValueType();
            element.value = value;
            
            final JAXBElement<XhtmlValueType> jaxbElement = new JAXBElement<XhtmlValueType>(
                    new QName( "http://www.w3.org/1999/xhtml", elementName ),
                    XhtmlValueType.class,
                    element );
            
            return jaxbElement;
            
        } catch ( Exception e ) {
            throw new RuntimeException( e );
        }
    }

}

===================== XhtmlElementType.java =====================

@XmlAccessorType( XmlAccessType.FIELD )
@XmlType( name = "foo", propOrder = { } )
@XmlRootElement( name = "foo" )
public class XhtmlElementType {
    
    @XmlAnyElement
    protected List<Object> any;

    public List<Object> getChildNodes() {
        if (any == null) {
            any = new ArrayList<Object>();
        }
        return this.any;
    }
    
}

===================== XhtmlElementType.java =====================

@XmlAccessorType( XmlAccessType.FIELD )
@XmlType( name = "valueType", propOrder = { } )
@XmlRootElement( name = "valueType" )
public class XhtmlValueType {

    @XmlValue
    protected String value;
    
}

===================== ObjectFactory.java =====================

@XmlRegistry
public class ObjectFactory {
    
    public XhtmlElementType createXhtmlElementType() {
        return new XhtmlElementType();
    }

    public XhtmlValueType createXhtmlCodeType() {
        return new XhtmlValueType();
    }
    
}


On Tue, 2008-06-17 at 11:32 +0200, Martin Grotzke wrote:
> Hi,
>
> I just want to create an xml document and add some xhtml elements as
> child nodes somewhere (e.g. <p><h3>foo</h3><pre><code>some
> code</code></pre></p>). However, I couldn't find bindings for xhtml, and
> didn't manage to create them from the xsds.
>
> So I see these possibilities:
> 1) Write bindings for xhtml manually
> 2) Find existing bindings for xhtml
> 3) Manage to add some sgml / xhtml tree without beeing forced to
> create jaxb bindings.
>
> I would prefer 3) or 2)
>
> S.th. like this would be nice:
> Elements.el( "p" ).add( Elements.el( "h3" ).value( "foo" ) ).add( Elements.el( "pre" )...
>
> Can anybody help with this?
>
> Thanx && cheers,
> Martin