users@jaxb.java.net

Re: JaxB and DOM dictionary & JAXB Customization

From: Ed Mooney <Ed.Mooney_at_sun.com>
Date: Fri, 06 Jun 2003 12:42:49 -0400

Hi J?r?me,

Yes, it is. Your base class will also have to declare the methods you
want to wrap with toString(). These get overridden by the subclasses.

For example, BaseClass.java wraps USAddress.getName() with toString().
Main.java calls address.toString(). If you drop these in
vendor-extensions and run ant, you see:

     ant
     Buildfile: build.xml

     compile:
           .
           .
     run:
          [echo] Running the sample application...
          [java] Alice Smith

Regards,
--
Ed Mooney         |Sun Microsystems, Inc.|Time flies like
Java Web Services |UBUR02-201            |an arrow, but
Ed.Mooney_at_Sun.COM |1 Network Drive       |fruit flies like
781-442-0459      |Burlington, MA  01803 |a banana. Groucho
Jerome Candat wrote:
>>You can either write a superclass that all JAXB-generated classes
>>extend, or write a subclasses that extend a JAXB-generated classes. See
>>samples/vendor-extensions (examples/vendor-extensions in 1.0) for the
>>first option.
>>
>
>
> That sounds great. I have looked at the vendor-extensions examples given with the jwsdp package.
> That's near of what I need.
> In fact, all my JAXB-generated classes have a _Name property.
> I would like the toString method in my superclass to return this information. Is it possible to do that ?
>
> Thanks in advance,
> J?r?me.
>


/*
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package primer;


/**
 * This is the superclass vendor extension
 * that have this class as their super class
 */

public class BaseClass {

    protected java.lang.String _Name;

    public String toString() {
        return getName();
    }

    public String getName() {
        return _Name;
    }
}



/*
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

// import java content classes generated by binding compiler
import primer.myPo.*;

/*
 * $Id: Main.java,v 1.2 2003/05/14 18:35:28 lschwenk Exp $
 *
 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
 *
 * This software is the proprietary information of Sun Microsystems, Inc.
 * Use is subject to license terms.
 *
 */

public class Main {

    // This sample application demonstrates how to unmarshal an instance
    // document into a Java content tree and access data contained within it.

    public static void main( String[] args ) {
        try {
            JAXBContext jc = JAXBContext.newInstance( "primer.myPo" );
            Unmarshaller u = jc.createUnmarshaller();
            PurchaseOrder po =
                (PurchaseOrder)u.unmarshal( new FileInputStream( "poInput.xml" ) );
            USAddress address = po.getShipTo();
            System.out.println(address.toString());
        } catch( JAXBException je ) {
            je.printStackTrace();
        } catch( IOException ioe ) {
            ioe.printStackTrace();
        }
    }
}