users@jaxb.java.net

Re: JaxB and DOM dictionary

From: ritika maheshwari <runjhun1_at_yahoo.com>
Date: Thu, 05 Jun 2003 10:31:46 -0700

What you have done. I did that too but not by using the Bean Introspector but by using low level reflection api's on the root class generated by JAXB. But the property descriptors do not solve the problem of trying to get the XPath for a property.For that you would still need to navigate the dom tree.

thanks

Ganga Sah <ganga_sah_at_yahoo.com> wrote:
Here is the futher deatils on my approach to this
problem.
I am attaching PropertyUtil class which has methods to
find property value(JAXB Java content object) inside
any base object(also JAXB Java content object, which
could be the root of Java content tree) with a given
one propertyName or an array of propertyName(s).
The methods returns the first object matching the
propertyName, which can be enhanced to return a list
of all matching objects.
Hope this helps.

(I have extraced these methods with some twicking out
of my application, and have not tested it seperately).

--ganga

--- ritika maheshwari wrote:
> Could you please send further details on the
> alternative
>
> thanks
>
>
>
> CANDAT Jerome wrote:
> Hello everyone,
>
> I'm currently using JAXB for one of my projects and
> I have the same problem
> : cannot get XPath of the elements.
>
> I'm really interested in your alternative though I
> don't know how to do
> this. Please send further details on your
> alternative.
>
>
> Thanks in advance,
> J?r?me.
> ----- Original Message -----
> From: "Ganga Sah"
> To:
> Sent: Wednesday, June 04, 2003 7:56 AM
> Subject: Re: JaxB and DOM dictionary
>
>
> > There is an alternative to accessing DOM tree thru
> > JAXB generated gettter(readMethod) by recursively
> > using Java reflection & PropertyDescriptor API
> from
> > the root of JAXB generated class. If this approach
> > helps you, I could send futher details if needed.
> > thanks,
> > ganga
> > --- SUBSCRIBE JAXB-INTEREST Annonymous
> > wrote:
> > > does jaxb generate some kind of a dictionary
> while
> > > generating the java classes from xsd. A
> dictionary
> > > which contains information about each element in
> the
> > > xsd. For example from the generated classes I
> cannot
> > > get the XPath of a certain element in xsd. So
> for
> > > this I would have to navigate the dom tree. So
> > > instead of my navigating the dom tree I could
> use
> > > the dictionary to get any details about the
> element
> > > which I cannot get from the java classes
> produced.If
> > > JAXB does not do something like this would you
> know
> > > any other tool that would.
> >
> >
> > __________________________________
> > Do you Yahoo!?
> > Yahoo! Calendar - Free online calendar with sync
> to Outlook(TM).
> > http://calendar.yahoo.com
> >
>
> ---------------------------------
> Do you Yahoo!?
> Free online calendar with sync to Outlook(TM).


__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com
import java.util.Iterator;
import java.util.List;
import java.beans.PropertyDescriptor;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class PropertyUtil {
public static PropertyDescriptor[] getPropertyDescriptors(Object obj) {
if (obj == null) return null;
Class bean = obj.getClass();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(bean, Object.class);
} catch(IntrospectionException e) {
System.err.println("Couldn't introspect " + bean.getName());
return null;
}
return beanInfo.getPropertyDescriptors();
}

public static Object getPropertyByName(Object obj, String propertyName) {
if (obj == null) return null;
PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(obj);
for (int i = 0; i < propertyDescriptors.length; i++) {
if (propertyName.startsWith("primaryInterface")) continue;//unwanted property
String thisPropertyName = propertyDescriptors[i].getName();
if (thisPropertyName.compareTo(propertyName) == 0) {
Method readMethod = propertyDescriptors[i].getReadMethod();
Object propertyValue = null;
try {
propertyValue = readMethod.invoke(obj, null);
} catch (IllegalAccessException illAccEx) {
illAccEx.printStackTrace();
} catch (InvocationTargetException invTarEx) {
invTarEx.printStackTrace();
}
return propertyValue;
}
}
return null;
}

public static Object getPropertyByName(List objList, String propertyName) {
for (Iterator objListIter = objList.iterator(); objListIter.hasNext();) {
Object nextObj = objListIter.next();
Object propertyValue = getPropertyByName(nextObj, propertyName) ;
if (propertyValue != null) return propertyValue;
}
return null;
}

public static Object getPropertyByNames(Object obj, String[] propertyNames) {
Object propertyValue = obj;
for (int i = 0; i < propertyNames.length; i++) {
propertyValue = getPropertyByName(propertyValue, propertyNames[i]) ;
if (propertyValue != null) {
continue;
} else {
return null;
}
}
return null;
}

}


---------------------------------
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).