On Oct 5, 2009, at 3:54 PM, Andy Spitzer wrote: > Woof! > > I'm about to starting using JAXB 2.0 annotations in my rework of > sipXivr > reading/writing mailboxprefs.xml. > > Anyone have any good reasons why I should NOT do that? My > investigations > so far seem to show that this is nice tech, already part of Java 6, > so no > extra libraries or Eclipse plugins required. > > Some examples: > > To parse a line like this (from mailboxprefs.xml): > > <contact type="email" attachments="yes" synchronize="no" > password="ABCD">[email protected]</contact> > > All that is needed is a class to hold the values, and in this case a > special adapter to convert "yes|no" into boolean. The annotations > do all > the work defining the mapping 'tween member variables and > attributes/values. It's even easier if the names of the member > variables > match the XML, but our naming conventions get in the way. > > /** > * XmlAdaptor to allow JAXB to parse booleans as "yes|no" > * (should be subclass of MailboxPrefereces, but JAXB 2.0 doesn't > allow > that.) > */ > class MailboxPreferencesYesNoAdaptor extends XmlAdapter<String, > Boolean> { > > @Override > public String marshal(Boolean v) throws Exception { > if (v != null) { > return v.booleanValue() ? "yes" : "no"; > } else return "no"; > } > > @Override > public Boolean unmarshal(String v) throws Exception { > return v.toLowerCase().equals("yes") || > v.toLowerCase().equals("true"); > } > > } > > > /** > * Holds the Mailbox Preferences Contact information. > * (should be a subclass of MailboxPreferences, but JAXB 2.0 doesn't > allow > that.) > */ > class MailboxPreferencesContact { > @XmlAttribute( name = "type") String m_type = "email"; > @XmlAttribute( name = "attachments") > @XmlJavaTypeAdapter(MailboxPreferencesYesNoAdaptor.class) Boolean > m_attachments = false; > @XmlAttribute( name = "synchronize") > @XmlJavaTypeAdapter(MailboxPreferencesYesNoAdaptor.class) Boolean > m_synchronize = false; > @XmlAttribute( name = "password") String m_password; > @XmlValue String m_emailAddress; > > @XmlTransient > public String getPassword() { > if (m_password != null) { > return new String(Base64.decodeBase64(m_password.getBytes > ())); > } > return null; > } > } > > So far it's been straight forward and simple, so I'm looking for the > downside before continuing down this path. > > Comments? > > --Woof! > _______________________________________________ > sipx-dev mailing list [email protected] > List Archive: http://list.sipfoundry.org/archive/sipx-dev > Unsubscribe: http://list.sipfoundry.org/mailman/listinfo/sipx-dev > sipXecs IP PBX -- http://www.sipfoundry.org/
I prefer schema binding over annotations. If automated as part of the build process, it is easier to maintain and less error prone than manual injection of annotations. -Mardy _______________________________________________ sipx-dev mailing list [email protected] List Archive: http://list.sipfoundry.org/archive/sipx-dev Unsubscribe: http://list.sipfoundry.org/mailman/listinfo/sipx-dev sipXecs IP PBX -- http://www.sipfoundry.org/
