Example usage for javax.xml.bind.annotation XmlAccessOrder ALPHABETICAL

List of usage examples for javax.xml.bind.annotation XmlAccessOrder ALPHABETICAL

Introduction

In this page you can find the example usage for javax.xml.bind.annotation XmlAccessOrder ALPHABETICAL.

Prototype

XmlAccessOrder ALPHABETICAL

To view the source code for javax.xml.bind.annotation XmlAccessOrder ALPHABETICAL.

Click Source Link

Document

The ordering of fields and properties in a class is in alphabetical order as determined by the method java.lang.String.compareTo(String anotherString).

Usage

From source file:org.castor.jaxb.reflection.ClassDescriptorBuilder.java

/**
 * Builds a XMLClassDescriptor from the class information collected in
 * ClassInfo./*w  ww.j ava2 s .  c  o m*/
 * 
 * @param classInfo
 *            all information collected about a certain class
 * @param saveMapKeys
 * @HACK no idea what this is good for...
 * @return the XMLClassDescriptor representing the class
 */
public XMLClassDescriptor buildClassDescriptor(final ClassInfo classInfo, final boolean saveMapKeys) {
    if (classInfo == null) {
        IllegalArgumentException e = new IllegalArgumentException("Argument classInfo must not be null");
        throw e;
    }
    JAXBClassDescriptorImpl classDescriptor = new JAXBClassDescriptorImpl();
    JaxbClassNature jaxbClassNature = new JaxbClassNature(classInfo);

    classDescriptor.setJavaClass(jaxbClassNature.getType());
    classDescriptor.setXMLName(getXMLName(jaxbClassNature));
    classDescriptor.setNameSpacePrefix(getNamespacePrefix(jaxbClassNature));
    classDescriptor.setNameSpaceURI(getNamespaceURI(jaxbClassNature));

    List<JaxbFieldNature> fields = new ArrayList<JaxbFieldNature>();
    fields.addAll(jaxbClassNature.getFields());

    if (jaxbClassNature.getXmlTransient()) {
        classDescriptor.setTransientClass(true);
    }
    if (jaxbClassNature.getXmlAccessorOrder()) {
        XmlAccessOrder accessOrder = jaxbClassNature.getXmlAccessOrder();
        if (accessOrder.equals(XmlAccessOrder.ALPHABETICAL)) {
            Collections.sort(fields, new Comparator<JaxbFieldNature>() {

                public int compare(JaxbFieldNature fieldNature1, JaxbFieldNature fieldNature2) {
                    return getName(fieldNature1).compareTo(getName(fieldNature2));
                }

                private String getName(JaxbFieldNature fieldNature) {
                    String name = "";
                    if (StringUtils.isNotEmpty(fieldNature.getElementName())) {
                        name = fieldNature.getElementName();
                    }
                    if (StringUtils.isNotEmpty(fieldNature.getAttributeName())) {
                        name = fieldNature.getAttributeName();
                    }
                    return name;
                }
            });
        }
    }

    for (JaxbFieldNature jaxbFieldNature : fields) {
        LOG.info("Field info: " + jaxbFieldNature + " now used");
        XMLFieldDescriptor fieldDescriptor = buildFieldDescriptor(jaxbFieldNature,
                jaxbClassNature.getXmlAccessType(), saveMapKeys);
        classDescriptor.addFieldDescriptor(fieldDescriptor);
    }

    return classDescriptor;
}